诸暨麻将添加redis
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

144 wiersze
6.0 KiB

  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 <limits>
  34. #include <string>
  35. #include <google/protobuf/stubs/logging.h>
  36. #include <google/protobuf/stubs/common.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace io {
  40. // ----------------------------------------------------------------------
  41. // NoLocaleStrtod()
  42. // This code will make you cry.
  43. // ----------------------------------------------------------------------
  44. namespace {
  45. // This approximately 0x1.ffffffp127, but we don't use 0x1.ffffffp127 because
  46. // it won't compile in MSVC.
  47. const double MAX_FLOAT_AS_DOUBLE_ROUNDED = 3.4028235677973366e+38;
  48. // Returns a string identical to *input except that the character pointed to
  49. // by radix_pos (which should be '.') is replaced with the locale-specific
  50. // radix character.
  51. std::string LocalizeRadix(const char* input, const char* radix_pos) {
  52. // Determine the locale-specific radix character by calling sprintf() to
  53. // print the number 1.5, then stripping off the digits. As far as I can
  54. // tell, this is the only portable, thread-safe way to get the C library
  55. // to divuldge the locale's radix character. No, localeconv() is NOT
  56. // thread-safe.
  57. char temp[16];
  58. int size = sprintf(temp, "%.1f", 1.5);
  59. GOOGLE_CHECK_EQ(temp[0], '1');
  60. GOOGLE_CHECK_EQ(temp[size - 1], '5');
  61. GOOGLE_CHECK_LE(size, 6);
  62. // Now replace the '.' in the input with it.
  63. std::string result;
  64. result.reserve(strlen(input) + size - 3);
  65. result.append(input, radix_pos);
  66. result.append(temp + 1, size - 2);
  67. result.append(radix_pos + 1);
  68. return result;
  69. }
  70. } // namespace
  71. double NoLocaleStrtod(const char* text, char** original_endptr) {
  72. // We cannot simply set the locale to "C" temporarily with setlocale()
  73. // as this is not thread-safe. Instead, we try to parse in the current
  74. // locale first. If parsing stops at a '.' character, then this is a
  75. // pretty good hint that we're actually in some other locale in which
  76. // '.' is not the radix character.
  77. char* temp_endptr;
  78. double result = strtod(text, &temp_endptr);
  79. if (original_endptr != NULL) *original_endptr = temp_endptr;
  80. if (*temp_endptr != '.') return result;
  81. // Parsing halted on a '.'. Perhaps we're in a different locale? Let's
  82. // try to replace the '.' with a locale-specific radix character and
  83. // try again.
  84. std::string localized = LocalizeRadix(text, temp_endptr);
  85. const char* localized_cstr = localized.c_str();
  86. char* localized_endptr;
  87. result = strtod(localized_cstr, &localized_endptr);
  88. if ((localized_endptr - localized_cstr) > (temp_endptr - text)) {
  89. // This attempt got further, so replacing the decimal must have helped.
  90. // Update original_endptr to point at the right location.
  91. if (original_endptr != NULL) {
  92. // size_diff is non-zero if the localized radix has multiple bytes.
  93. int size_diff = localized.size() - strlen(text);
  94. // const_cast is necessary to match the strtod() interface.
  95. *original_endptr = const_cast<char*>(
  96. text + (localized_endptr - localized_cstr - size_diff));
  97. }
  98. }
  99. return result;
  100. }
  101. float SafeDoubleToFloat(double value) {
  102. // static_cast<float> on a number larger than float can result in illegal
  103. // instruction error, so we need to manually convert it to infinity or max.
  104. if (value > std::numeric_limits<float>::max()) {
  105. // Max float value is about 3.4028234664E38 when represented as a double.
  106. // However, when printing float as text, it will be rounded as
  107. // 3.4028235e+38. If we parse the value of 3.4028235e+38 from text and
  108. // compare it to 3.4028234664E38, we may think that it is larger, but
  109. // actually, any number between these two numbers could only be represented
  110. // as the same max float number in float, so we should treat them the same
  111. // as max float.
  112. if (value <= MAX_FLOAT_AS_DOUBLE_ROUNDED) {
  113. return std::numeric_limits<float>::max();
  114. }
  115. return std::numeric_limits<float>::infinity();
  116. } else if (value < -std::numeric_limits<float>::max()) {
  117. if (value >= -MAX_FLOAT_AS_DOUBLE_ROUNDED) {
  118. return -std::numeric_limits<float>::max();
  119. }
  120. return -std::numeric_limits<float>::infinity();
  121. } else {
  122. return static_cast<float>(value);
  123. }
  124. }
  125. } // namespace io
  126. } // namespace protobuf
  127. } // namespace google