JsonCpp project page | JsonCpp home page

json_tool.h
Go to the documentation of this file.
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
6 #ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED
7 #define LIB_JSONCPP_JSON_TOOL_H_INCLUDED
8
9
10 // Also support old flag NO_LOCALE_SUPPORT
11 #ifdef NO_LOCALE_SUPPORT
12 #define JSONCPP_NO_LOCALE_SUPPORT
13 #endif
14
15 #ifndef JSONCPP_NO_LOCALE_SUPPORT
16 #include <clocale>
17 #endif
18
19 /* This header provides common string manipulation support, such as UTF-8,
20 * portable conversion from/to string...
21 *
22 * It is an internal header that must not be exposed.
23 */
24
25 namespace Json {
26 static char getDecimalPoint() {
27 #ifdef JSONCPP_NO_LOCALE_SUPPORT
28 return '\0';
29 #else
30 struct lconv* lc = localeconv();
31 return lc ? *(lc->decimal_point) : '\0';
32 #endif
33 }
34
36 static inline JSONCPP_STRING codePointToUTF8(unsigned int cp) {
37 JSONCPP_STRING result;
38
39 // based on description from http://en.wikipedia.org/wiki/UTF-8
40
41 if (cp <= 0x7f) {
42 result.resize(1);
43 result[0] = static_cast<char>(cp);
44 } else if (cp <= 0x7FF) {
45 result.resize(2);
46 result[1] = static_cast<char>(0x80 | (0x3f & cp));
47 result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
48 } else if (cp <= 0xFFFF) {
49 result.resize(3);
50 result[2] = static_cast<char>(0x80 | (0x3f & cp));
51 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
52 result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));
53 } else if (cp <= 0x10FFFF) {
54 result.resize(4);
55 result[3] = static_cast<char>(0x80 | (0x3f & cp));
56 result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
57 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
58 result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
59 }
60
61 return result;
62 }
63
65 static inline bool isControlCharacter(char ch) { return ch > 0 && ch <= 0x1F; }
66
67 enum {
70 uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1
71 };
72
73 // Defines a char buffer for use with uintToString().
74 typedef char UIntToStringBuffer[uintToStringBufferSize];
75
81 static inline void uintToString(LargestUInt value, char*& current) {
82 *--current = 0;
83 do {
84 *--current = static_cast<char>(value % 10U + static_cast<unsigned>('0'));
85 value /= 10;
86 } while (value != 0);
87 }
88
94 static inline void fixNumericLocale(char* begin, char* end) {
95 while (begin < end) {
96 if (*begin == ',') {
97 *begin = '.';
98 }
99 ++begin;
100 }
101 }
102
103 static inline void fixNumericLocaleInput(char* begin, char* end) {
104 char decimalPoint = getDecimalPoint();
105 if (decimalPoint != '\0' && decimalPoint != '.') {
106 while (begin < end) {
107 if (*begin == '.') {
108 *begin = decimalPoint;
109 }
110 ++begin;
111 }
112 }
113 }
114
115 } // namespace Json {
116
117 #endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED
Json::uintToString
static void uintToString(LargestUInt value, char *&current)
Converts an unsigned integer to string.
Definition: json_tool.h:81
JSONCPP_STRING
#define JSONCPP_STRING
Definition: config.h:179
Json::getDecimalPoint
static char getDecimalPoint()
Definition: json_tool.h:26
Json::codePointToUTF8
static JSONCPP_STRING codePointToUTF8(unsigned int cp)
Converts a unicode code-point to UTF-8.
Definition: json_tool.h:36
Json::UIntToStringBuffer
char UIntToStringBuffer[uintToStringBufferSize]
Definition: json_tool.h:74
Json::isControlCharacter
static bool isControlCharacter(char ch)
Returns true if ch is a control character (in range [1,31]).
Definition: json_tool.h:65
Json::fixNumericLocale
static void fixNumericLocale(char *begin, char *end)
Change &#39;,&#39; to &#39;.
Definition: json_tool.h:94
Json::fixNumericLocaleInput
static void fixNumericLocaleInput(char *begin, char *end)
Definition: json_tool.h:103
Json::LargestUInt
UInt64 LargestUInt
Definition: config.h:169
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::uintToStringBufferSize
Constant that specify the size of the buffer that must be passed to uintToString. ...
Definition: json_tool.h:70