JsonCpp project page JsonCpp home page

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