ian@0
|
1 // Copyright 2007-2010 Baptiste Lepilleur
|
ian@0
|
2 // Distributed under MIT license, or public domain if desired and
|
ian@0
|
3 // recognized in your jurisdiction.
|
ian@0
|
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
ian@0
|
5
|
ian@0
|
6 #ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED
|
ian@0
|
7 # define LIB_JSONCPP_JSON_TOOL_H_INCLUDED
|
ian@0
|
8
|
ian@0
|
9 /* This header provides common string manipulation support, such as UTF-8,
|
ian@0
|
10 * portable conversion from/to string...
|
ian@0
|
11 *
|
ian@0
|
12 * It is an internal header that must not be exposed.
|
ian@0
|
13 */
|
ian@0
|
14
|
ian@0
|
15 namespace Json {
|
ian@0
|
16
|
ian@0
|
17 /// Converts a unicode code-point to UTF-8.
|
ian@0
|
18 static inline std::string
|
ian@0
|
19 codePointToUTF8(unsigned int cp)
|
ian@0
|
20 {
|
ian@0
|
21 std::string result;
|
ian@0
|
22
|
ian@0
|
23 // based on description from http://en.wikipedia.org/wiki/UTF-8
|
ian@0
|
24
|
ian@0
|
25 if (cp <= 0x7f)
|
ian@0
|
26 {
|
ian@0
|
27 result.resize(1);
|
ian@0
|
28 result[0] = static_cast<char>(cp);
|
ian@0
|
29 }
|
ian@0
|
30 else if (cp <= 0x7FF)
|
ian@0
|
31 {
|
ian@0
|
32 result.resize(2);
|
ian@0
|
33 result[1] = static_cast<char>(0x80 | (0x3f & cp));
|
ian@0
|
34 result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
|
ian@0
|
35 }
|
ian@0
|
36 else if (cp <= 0xFFFF)
|
ian@0
|
37 {
|
ian@0
|
38 result.resize(3);
|
ian@0
|
39 result[2] = static_cast<char>(0x80 | (0x3f & cp));
|
ian@0
|
40 result[1] = 0x80 | static_cast<char>((0x3f & (cp >> 6)));
|
ian@0
|
41 result[0] = 0xE0 | static_cast<char>((0xf & (cp >> 12)));
|
ian@0
|
42 }
|
ian@0
|
43 else if (cp <= 0x10FFFF)
|
ian@0
|
44 {
|
ian@0
|
45 result.resize(4);
|
ian@0
|
46 result[3] = static_cast<char>(0x80 | (0x3f & cp));
|
ian@0
|
47 result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
|
ian@0
|
48 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
|
ian@0
|
49 result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
|
ian@0
|
50 }
|
ian@0
|
51
|
ian@0
|
52 return result;
|
ian@0
|
53 }
|
ian@0
|
54
|
ian@0
|
55
|
ian@0
|
56 /// Returns true if ch is a control character (in range [0,32[).
|
ian@0
|
57 static inline bool
|
ian@0
|
58 isControlCharacter(char ch)
|
ian@0
|
59 {
|
ian@0
|
60 return ch > 0 && ch <= 0x1F;
|
ian@0
|
61 }
|
ian@0
|
62
|
ian@0
|
63
|
ian@0
|
64 enum {
|
ian@0
|
65 /// Constant that specify the size of the buffer that must be passed to uintToString.
|
ian@0
|
66 uintToStringBufferSize = 3*sizeof(LargestUInt)+1
|
ian@0
|
67 };
|
ian@0
|
68
|
ian@0
|
69 // Defines a char buffer for use with uintToString().
|
ian@0
|
70 typedef char UIntToStringBuffer[uintToStringBufferSize];
|
ian@0
|
71
|
ian@0
|
72
|
ian@0
|
73 /** Converts an unsigned integer to string.
|
ian@0
|
74 * @param value Unsigned interger to convert to string
|
ian@0
|
75 * @param current Input/Output string buffer.
|
ian@0
|
76 * Must have at least uintToStringBufferSize chars free.
|
ian@0
|
77 */
|
ian@0
|
78 static inline void
|
ian@0
|
79 uintToString( LargestUInt value,
|
ian@0
|
80 char *¤t )
|
ian@0
|
81 {
|
ian@0
|
82 *--current = 0;
|
ian@0
|
83 do
|
ian@0
|
84 {
|
ian@0
|
85 *--current = char(value % 10) + '0';
|
ian@0
|
86 value /= 10;
|
ian@0
|
87 }
|
ian@0
|
88 while ( value != 0 );
|
ian@0
|
89 }
|
ian@0
|
90
|
ian@0
|
91 } // namespace Json {
|
ian@0
|
92
|
ian@0
|
93 #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED
|