LCOV - code coverage report
Current view: top level - third_party/protobuf/src/google/protobuf/io - strtod.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 4 23 17.4 %
Date: 2015-10-10 Functions: 1 2 50.0 %

          Line data    Source code
       1             : // Protocol Buffers - Google's data interchange format
       2             : // Copyright 2008 Google Inc.  All rights reserved.
       3             : // https://developers.google.com/protocol-buffers/
       4             : //
       5             : // Redistribution and use in source and binary forms, with or without
       6             : // modification, are permitted provided that the following conditions are
       7             : // met:
       8             : //
       9             : //     * Redistributions of source code must retain the above copyright
      10             : // notice, this list of conditions and the following disclaimer.
      11             : //     * Redistributions in binary form must reproduce the above
      12             : // copyright notice, this list of conditions and the following disclaimer
      13             : // in the documentation and/or other materials provided with the
      14             : // distribution.
      15             : //     * Neither the name of Google Inc. nor the names of its
      16             : // contributors may be used to endorse or promote products derived from
      17             : // this software without specific prior written permission.
      18             : //
      19             : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      20             : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      21             : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      22             : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      23             : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      24             : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      25             : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      26             : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      27             : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      28             : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      29             : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      30             : 
      31             : #include <google/protobuf/io/strtod.h>
      32             : 
      33             : #include <cstdio>
      34             : #include <cstring>
      35             : #include <string>
      36             : 
      37             : #include <google/protobuf/stubs/logging.h>
      38             : #include <google/protobuf/stubs/common.h>
      39             : 
      40             : namespace google {
      41             : namespace protobuf {
      42             : namespace io {
      43             : 
      44             : // ----------------------------------------------------------------------
      45             : // NoLocaleStrtod()
      46             : //   This code will make you cry.
      47             : // ----------------------------------------------------------------------
      48             : 
      49             : namespace {
      50             : 
      51             : // Returns a string identical to *input except that the character pointed to
      52             : // by radix_pos (which should be '.') is replaced with the locale-specific
      53             : // radix character.
      54           0 : string LocalizeRadix(const char* input, const char* radix_pos) {
      55             :   // Determine the locale-specific radix character by calling sprintf() to
      56             :   // print the number 1.5, then stripping off the digits.  As far as I can
      57             :   // tell, this is the only portable, thread-safe way to get the C library
      58             :   // to divuldge the locale's radix character.  No, localeconv() is NOT
      59             :   // thread-safe.
      60             :   char temp[16];
      61           0 :   int size = sprintf(temp, "%.1f", 1.5);
      62           0 :   GOOGLE_CHECK_EQ(temp[0], '1');
      63           0 :   GOOGLE_CHECK_EQ(temp[size-1], '5');
      64           0 :   GOOGLE_CHECK_LE(size, 6);
      65             : 
      66             :   // Now replace the '.' in the input with it.
      67             :   string result;
      68           0 :   result.reserve(strlen(input) + size - 3);
      69           0 :   result.append(input, radix_pos);
      70           0 :   result.append(temp + 1, size - 2);
      71           0 :   result.append(radix_pos + 1);
      72           0 :   return result;
      73             : }
      74             : 
      75             : }  // namespace
      76             : 
      77          33 : double NoLocaleStrtod(const char* text, char** original_endptr) {
      78             :   // We cannot simply set the locale to "C" temporarily with setlocale()
      79             :   // as this is not thread-safe.  Instead, we try to parse in the current
      80             :   // locale first.  If parsing stops at a '.' character, then this is a
      81             :   // pretty good hint that we're actually in some other locale in which
      82             :   // '.' is not the radix character.
      83             : 
      84             :   char* temp_endptr;
      85          33 :   double result = strtod(text, &temp_endptr);
      86          33 :   if (original_endptr != NULL) *original_endptr = temp_endptr;
      87          33 :   if (*temp_endptr != '.') return result;
      88             : 
      89             :   // Parsing halted on a '.'.  Perhaps we're in a different locale?  Let's
      90             :   // try to replace the '.' with a locale-specific radix character and
      91             :   // try again.
      92           0 :   string localized = LocalizeRadix(text, temp_endptr);
      93           0 :   const char* localized_cstr = localized.c_str();
      94             :   char* localized_endptr;
      95           0 :   result = strtod(localized_cstr, &localized_endptr);
      96           0 :   if ((localized_endptr - localized_cstr) >
      97           0 :       (temp_endptr - text)) {
      98             :     // This attempt got further, so replacing the decimal must have helped.
      99             :     // Update original_endptr to point at the right location.
     100           0 :     if (original_endptr != NULL) {
     101             :       // size_diff is non-zero if the localized radix has multiple bytes.
     102           0 :       int size_diff = localized.size() - strlen(text);
     103             :       // const_cast is necessary to match the strtod() interface.
     104             :       *original_endptr = const_cast<char*>(
     105           0 :         text + (localized_endptr - localized_cstr - size_diff));
     106             :     }
     107             :   }
     108             : 
     109           0 :   return result;
     110             : }
     111             : 
     112             : }  // namespace io
     113             : }  // namespace protobuf
     114             : }  // namespace google

Generated by: LCOV version 1.10