|
| 1 | +#ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED |
| 2 | +# define LIB_JSONCPP_JSON_TOOL_H_INCLUDED |
| 3 | + |
| 4 | +/* This header provides common string manipulation support, such as UTF-8, |
| 5 | + * portable conversion from/to string... |
| 6 | + * |
| 7 | + * It is an internal header that must not be exposed. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Json { |
| 11 | + |
| 12 | +/// Converts a unicode code-point to UTF-8. |
| 13 | +static inline std::string |
| 14 | +codePointToUTF8(unsigned int cp) |
| 15 | +{ |
| 16 | + std::string result; |
| 17 | + |
| 18 | + // based on description from http://en.wikipedia.org/wiki/UTF-8 |
| 19 | + |
| 20 | + if (cp <= 0x7f) |
| 21 | + { |
| 22 | + result.resize(1); |
| 23 | + result[0] = static_cast<char>(cp); |
| 24 | + } |
| 25 | + else if (cp <= 0x7FF) |
| 26 | + { |
| 27 | + result.resize(2); |
| 28 | + result[1] = static_cast<char>(0x80 | (0x3f & cp)); |
| 29 | + result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6))); |
| 30 | + } |
| 31 | + else if (cp <= 0xFFFF) |
| 32 | + { |
| 33 | + result.resize(3); |
| 34 | + result[2] = static_cast<char>(0x80 | (0x3f & cp)); |
| 35 | + result[1] = 0x80 | static_cast<char>((0x3f & (cp >> 6))); |
| 36 | + result[0] = 0xE0 | static_cast<char>((0xf & (cp >> 12))); |
| 37 | + } |
| 38 | + else if (cp <= 0x10FFFF) |
| 39 | + { |
| 40 | + result.resize(4); |
| 41 | + result[3] = static_cast<char>(0x80 | (0x3f & cp)); |
| 42 | + result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6))); |
| 43 | + result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12))); |
| 44 | + result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18))); |
| 45 | + } |
| 46 | + |
| 47 | + return result; |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +/// Returns true if ch is a control character (in range [0,32[). |
| 52 | +static inline bool |
| 53 | +isControlCharacter(char ch) |
| 54 | +{ |
| 55 | + return ch > 0 && ch <= 0x1F; |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +/** Converts an unsigned integer to string. |
| 60 | + * @param value Unsigned interger to convert to string |
| 61 | + * @param current Input/Output string buffer. Must have at least 10 chars free. |
| 62 | + */ |
| 63 | +static inline void |
| 64 | +uintToString( unsigned int value, |
| 65 | + char *¤t ) |
| 66 | +{ |
| 67 | + *--current = 0; |
| 68 | + do |
| 69 | + { |
| 70 | + *--current = (value % 10) + '0'; |
| 71 | + value /= 10; |
| 72 | + } |
| 73 | + while ( value != 0 ); |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace Json { |
| 77 | + |
| 78 | +#endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED |
0 commit comments