Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 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 | |
| 6 | #ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 7 | #define LIB_JSONCPP_JSON_TOOL_H_INCLUDED |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 8 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 9 | #if !defined(JSON_IS_AMALGAMATION) |
| 10 | #include <json/config.h> |
| 11 | #endif |
| 12 | |
| 13 | // Also support old flag NO_LOCALE_SUPPORT |
| 14 | #ifdef NO_LOCALE_SUPPORT |
| 15 | #define JSONCPP_NO_LOCALE_SUPPORT |
| 16 | #endif |
| 17 | |
| 18 | #ifndef JSONCPP_NO_LOCALE_SUPPORT |
| 19 | #include <clocale> |
| 20 | #endif |
| 21 | |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 22 | /* This header provides common string manipulation support, such as UTF-8, |
| 23 | * portable conversion from/to string... |
| 24 | * |
| 25 | * It is an internal header that must not be exposed. |
| 26 | */ |
| 27 | |
| 28 | namespace Json { |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 29 | static inline char getDecimalPoint() { |
| 30 | #ifdef JSONCPP_NO_LOCALE_SUPPORT |
| 31 | return '\0'; |
| 32 | #else |
| 33 | struct lconv* lc = localeconv(); |
| 34 | return lc ? *(lc->decimal_point) : '\0'; |
| 35 | #endif |
| 36 | } |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 37 | |
| 38 | /// Converts a unicode code-point to UTF-8. |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 39 | static inline String codePointToUTF8(unsigned int cp) { |
| 40 | String result; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 41 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 42 | // based on description from http://en.wikipedia.org/wiki/UTF-8 |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 43 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 44 | if (cp <= 0x7f) { |
| 45 | result.resize(1); |
| 46 | result[0] = static_cast<char>(cp); |
| 47 | } else if (cp <= 0x7FF) { |
| 48 | result.resize(2); |
| 49 | result[1] = static_cast<char>(0x80 | (0x3f & cp)); |
| 50 | result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6))); |
| 51 | } else if (cp <= 0xFFFF) { |
| 52 | result.resize(3); |
| 53 | result[2] = static_cast<char>(0x80 | (0x3f & cp)); |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 54 | result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6))); |
| 55 | result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12))); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 56 | } else if (cp <= 0x10FFFF) { |
| 57 | result.resize(4); |
| 58 | result[3] = static_cast<char>(0x80 | (0x3f & cp)); |
| 59 | result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6))); |
| 60 | result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12))); |
| 61 | result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18))); |
| 62 | } |
| 63 | |
| 64 | return result; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 67 | enum { |
| 68 | /// Constant that specify the size of the buffer that must be passed to |
| 69 | /// uintToString. |
| 70 | uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1 |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | // Defines a char buffer for use with uintToString(). |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 74 | using UIntToStringBuffer = char[uintToStringBufferSize]; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 75 | |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 76 | /** Converts an unsigned integer to string. |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 77 | * @param value Unsigned integer to convert to string |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 78 | * @param current Input/Output string buffer. |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 79 | * Must have at least uintToStringBufferSize chars free. |
| 80 | */ |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 81 | static inline void uintToString(LargestUInt value, char*& current) { |
| 82 | *--current = 0; |
| 83 | do { |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 84 | *--current = static_cast<char>(value % 10U + static_cast<unsigned>('0')); |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 85 | value /= 10; |
| 86 | } while (value != 0); |
| 87 | } |
| 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 | */ |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 94 | template <typename Iter> Iter fixNumericLocale(Iter begin, Iter end) { |
| 95 | for (; begin != end; ++begin) { |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 96 | if (*begin == ',') { |
| 97 | *begin = '.'; |
| 98 | } |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 99 | } |
| 100 | return begin; |
| 101 | } |
| 102 | |
| 103 | template <typename Iter> void fixNumericLocaleInput(Iter begin, Iter end) { |
| 104 | char decimalPoint = getDecimalPoint(); |
| 105 | if (decimalPoint == '\0' || decimalPoint == '.') { |
| 106 | return; |
| 107 | } |
| 108 | for (; begin != end; ++begin) { |
| 109 | if (*begin == '.') { |
| 110 | *begin = decimalPoint; |
| 111 | } |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 112 | } |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Return iterator that would be the new end of the range [begin,end), if we |
| 117 | * were to delete zeros in the end of string, but not the last zero before '.'. |
| 118 | */ |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 119 | template <typename Iter> |
| 120 | Iter fixZerosInTheEnd(Iter begin, Iter end, unsigned int precision) { |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 121 | for (; begin != end; --end) { |
| 122 | if (*(end - 1) != '0') { |
| 123 | return end; |
| 124 | } |
| 125 | // Don't delete the last zero before the decimal point. |
Elliott Hughes | 1601ea0 | 2021-12-07 09:43:38 -0800 | [diff] [blame] | 126 | if (begin != (end - 1) && begin != (end - 2) && *(end - 2) == '.') { |
| 127 | if (precision) { |
| 128 | return end; |
| 129 | } |
| 130 | return end - 2; |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | return end; |
| 134 | } |
| 135 | |
| 136 | } // namespace Json |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 137 | |
| 138 | #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED |