strtod.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <google/protobuf/io/strtod.h>
  31. #include <cstdio>
  32. #include <cstring>
  33. #include <string>
  34. #include <google/protobuf/stubs/logging.h>
  35. #include <google/protobuf/stubs/common.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace io {
  39. // ----------------------------------------------------------------------
  40. // NoLocaleStrtod()
  41. // This code will make you cry.
  42. // ----------------------------------------------------------------------
  43. namespace {
  44. // Returns a string identical to *input except that the character pointed to
  45. // by radix_pos (which should be '.') is replaced with the locale-specific
  46. // radix character.
  47. string LocalizeRadix(const char* input, const char* radix_pos) {
  48. // Determine the locale-specific radix character by calling sprintf() to
  49. // print the number 1.5, then stripping off the digits. As far as I can
  50. // tell, this is the only portable, thread-safe way to get the C library
  51. // to divuldge the locale's radix character. No, localeconv() is NOT
  52. // thread-safe.
  53. char temp[16];
  54. int size = sprintf(temp, "%.1f", 1.5);
  55. GOOGLE_CHECK_EQ(temp[0], '1');
  56. GOOGLE_CHECK_EQ(temp[size-1], '5');
  57. GOOGLE_CHECK_LE(size, 6);
  58. // Now replace the '.' in the input with it.
  59. string result;
  60. result.reserve(strlen(input) + size - 3);
  61. result.append(input, radix_pos);
  62. result.append(temp + 1, size - 2);
  63. result.append(radix_pos + 1);
  64. return result;
  65. }
  66. } // namespace
  67. double NoLocaleStrtod(const char* text, char** original_endptr) {
  68. // We cannot simply set the locale to "C" temporarily with setlocale()
  69. // as this is not thread-safe. Instead, we try to parse in the current
  70. // locale first. If parsing stops at a '.' character, then this is a
  71. // pretty good hint that we're actually in some other locale in which
  72. // '.' is not the radix character.
  73. char* temp_endptr;
  74. double result = strtod(text, &temp_endptr);
  75. if (original_endptr != NULL) *original_endptr = temp_endptr;
  76. if (*temp_endptr != '.') return result;
  77. // Parsing halted on a '.'. Perhaps we're in a different locale? Let's
  78. // try to replace the '.' with a locale-specific radix character and
  79. // try again.
  80. string localized = LocalizeRadix(text, temp_endptr);
  81. const char* localized_cstr = localized.c_str();
  82. char* localized_endptr;
  83. result = strtod(localized_cstr, &localized_endptr);
  84. if ((localized_endptr - localized_cstr) >
  85. (temp_endptr - text)) {
  86. // This attempt got further, so replacing the decimal must have helped.
  87. // Update original_endptr to point at the right location.
  88. if (original_endptr != NULL) {
  89. // size_diff is non-zero if the localized radix has multiple bytes.
  90. int size_diff = localized.size() - strlen(text);
  91. // const_cast is necessary to match the strtod() interface.
  92. *original_endptr = const_cast<char*>(
  93. text + (localized_endptr - localized_cstr - size_diff));
  94. }
  95. }
  96. return result;
  97. }
  98. } // namespace io
  99. } // namespace protobuf
  100. } // namespace google