You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.8 KiB

7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
  1. // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
  2. // Distributed under MIT license, or public domain if desired and
  3. // recognized in your jurisdiction.
  4. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
  5. #ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED
  6. #define LIB_JSONCPP_JSON_TOOL_H_INCLUDED
  7. #if ! defined(JSON_IS_AMALGAMATION)
  8. #include <json/config.h>
  9. #endif
  10. // Also support old flag NO_LOCALE_SUPPORT
  11. #ifdef NO_LOCALE_SUPPORT
  12. #define JSONCPP_NO_LOCALE_SUPPORT
  13. #endif
  14. #ifndef JSONCPP_NO_LOCALE_SUPPORT
  15. #include <clocale>
  16. #endif
  17. /* This header provides common string manipulation support, such as UTF-8,
  18. * portable conversion from/to string...
  19. *
  20. * It is an internal header that must not be exposed.
  21. */
  22. namespace Json
  23. {
  24. static inline char getDecimalPoint()
  25. {
  26. #ifdef JSONCPP_NO_LOCALE_SUPPORT
  27. return '\0';
  28. #else
  29. struct lconv * lc = localeconv();
  30. return lc ? *(lc->decimal_point) : '\0';
  31. #endif
  32. }
  33. /// Converts a unicode code-point to UTF-8.
  34. static inline String codePointToUTF8(unsigned int cp)
  35. {
  36. String result;
  37. // based on description from http://en.wikipedia.org/wiki/UTF-8
  38. if (cp <= 0x7f)
  39. {
  40. result.resize(1);
  41. result[0] = static_cast<char>(cp);
  42. }
  43. else if (cp <= 0x7FF)
  44. {
  45. result.resize(2);
  46. result[1] = static_cast<char>(0x80 | (0x3f & cp));
  47. result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
  48. }
  49. else if (cp <= 0xFFFF)
  50. {
  51. result.resize(3);
  52. result[2] = static_cast<char>(0x80 | (0x3f & cp));
  53. result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
  54. result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));
  55. }
  56. else if (cp <= 0x10FFFF)
  57. {
  58. result.resize(4);
  59. result[3] = static_cast<char>(0x80 | (0x3f & cp));
  60. result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
  61. result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
  62. result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
  63. }
  64. return result;
  65. }
  66. enum
  67. {
  68. /// Constant that specify the size of the buffer that must be passed to
  69. /// uintToString.
  70. uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1
  71. };
  72. // Defines a char buffer for use with uintToString().
  73. typedef char UIntToStringBuffer[uintToStringBufferSize];
  74. /** Converts an unsigned integer to string.
  75. * @param value Unsigned integer to convert to string
  76. * @param current Input/Output string buffer.
  77. * Must have at least uintToStringBufferSize chars free.
  78. */
  79. static inline void uintToString(LargestUInt value, char *& current)
  80. {
  81. *--current = 0;
  82. do
  83. {
  84. *--current = static_cast<char>(value % 10U + static_cast<unsigned>('0'));
  85. value /= 10;
  86. }
  87. while (value != 0);
  88. }
  89. /** Change ',' to '.' everywhere in buffer.
  90. *
  91. * We had a sophisticated way, but it did not work in WinCE.
  92. * @see https://github.com/open-source-parsers/jsoncpp/pull/9
  93. */
  94. template <typename Iter> Iter fixNumericLocale(Iter begin, Iter end)
  95. {
  96. for (; begin != end; ++begin)
  97. {
  98. if (*begin == ',')
  99. {
  100. *begin = '.';
  101. }
  102. }
  103. return begin;
  104. }
  105. template <typename Iter> void fixNumericLocaleInput(Iter begin, Iter end)
  106. {
  107. char decimalPoint = getDecimalPoint();
  108. if (decimalPoint == '\0' || decimalPoint == '.')
  109. {
  110. return;
  111. }
  112. for (; begin != end; ++begin)
  113. {
  114. if (*begin == '.')
  115. {
  116. *begin = decimalPoint;
  117. }
  118. }
  119. }
  120. /**
  121. * Return iterator that would be the new end of the range [begin,end), if we
  122. * were to delete zeros in the end of string, but not the last zero before '.'.
  123. */
  124. template <typename Iter> Iter fixZerosInTheEnd(Iter begin, Iter end)
  125. {
  126. for (; begin != end; --end)
  127. {
  128. if (*(end - 1) != '0')
  129. {
  130. return end;
  131. }
  132. // Don't delete the last zero before the decimal point.
  133. if (begin != (end - 1) && *(end - 2) == '.')
  134. {
  135. return end;
  136. }
  137. }
  138. return end;
  139. }
  140. } // namespace Json
  141. #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED