Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur |
| 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 | |
| 9 | /* This header provides common string manipulation support, such as UTF-8, |
| 10 | * portable conversion from/to string... |
| 11 | * |
| 12 | * It is an internal header that must not be exposed. |
| 13 | */ |
| 14 | |
| 15 | namespace Json { |
| 16 | |
| 17 | /// Converts a unicode code-point to UTF-8. |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame^] | 18 | static inline std::string codePointToUTF8(unsigned int cp) { |
| 19 | std::string result; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 20 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame^] | 21 | // based on description from http://en.wikipedia.org/wiki/UTF-8 |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 22 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame^] | 23 | if (cp <= 0x7f) { |
| 24 | result.resize(1); |
| 25 | result[0] = static_cast<char>(cp); |
| 26 | } else if (cp <= 0x7FF) { |
| 27 | result.resize(2); |
| 28 | result[1] = static_cast<char>(0x80 | (0x3f & cp)); |
| 29 | result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6))); |
| 30 | } else if (cp <= 0xFFFF) { |
| 31 | result.resize(3); |
| 32 | result[2] = static_cast<char>(0x80 | (0x3f & cp)); |
| 33 | result[1] = 0x80 | static_cast<char>((0x3f & (cp >> 6))); |
| 34 | result[0] = 0xE0 | static_cast<char>((0xf & (cp >> 12))); |
| 35 | } else if (cp <= 0x10FFFF) { |
| 36 | result.resize(4); |
| 37 | result[3] = static_cast<char>(0x80 | (0x3f & cp)); |
| 38 | result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6))); |
| 39 | result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12))); |
| 40 | result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18))); |
| 41 | } |
| 42 | |
| 43 | return result; |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 46 | /// Returns true if ch is a control character (in range [0,32[). |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame^] | 47 | static inline bool isControlCharacter(char ch) { return ch > 0 && ch <= 0x1F; } |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 48 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame^] | 49 | enum { |
| 50 | /// Constant that specify the size of the buffer that must be passed to |
| 51 | /// uintToString. |
| 52 | uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1 |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | // Defines a char buffer for use with uintToString(). |
| 56 | typedef char UIntToStringBuffer[uintToStringBufferSize]; |
| 57 | |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 58 | /** Converts an unsigned integer to string. |
| 59 | * @param value Unsigned interger to convert to string |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame^] | 60 | * @param current Input/Output string buffer. |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 61 | * Must have at least uintToStringBufferSize chars free. |
| 62 | */ |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame^] | 63 | static inline void uintToString(LargestUInt value, char*& current) { |
| 64 | *--current = 0; |
| 65 | do { |
| 66 | *--current = char(value % 10) + '0'; |
| 67 | value /= 10; |
| 68 | } while (value != 0); |
| 69 | } |
| 70 | |
| 71 | /** Change ',' to '.' everywhere in buffer. |
| 72 | * |
| 73 | * We had a sophisticated way, but it did not work in WinCE. |
| 74 | * @see https://github.com/open-source-parsers/jsoncpp/pull/9 |
| 75 | */ |
| 76 | static inline void fixNumericLocale(char* begin, char* end) { |
| 77 | while (begin < end) { |
| 78 | if (*begin == ',') { |
| 79 | *begin = '.'; |
| 80 | } |
| 81 | ++begin; |
| 82 | } |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace Json { |
| 86 | |
| 87 | #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED |