NIM PC Cross Platform SDK
json_tool.h
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#if !defined(JSON_IS_AMALGAMATION)
10#include "nim_wrapper_util/json/config.h"
11#endif
12
13// Also support old flag NO_LOCALE_SUPPORT
14#ifdef NO_LOCALE_SUPPORT
15#define JSONCPP_NO_LOCALE_SUPPORT
16#endif
17
18#ifndef JSONCPP_NO_LOCALE_SUPPORT
19#include <clocale>
20#endif
21
22/* This header provides common string manipulation support, such as UTF-8,
23 * portable conversion from/to string...
24 *
25 * It is an internal header that must not be exposed.
26 */
27
28namespace nim_cpp_wrapper_util {
29namespace Json {
30static inline char getDecimalPoint() {
31#ifdef JSONCPP_NO_LOCALE_SUPPORT
32 return '\0';
33#else
34 struct lconv* lc = localeconv();
35 return lc ? *(lc->decimal_point) : '\0';
36#endif
37}
38
40static inline String codePointToUTF8(unsigned int cp) {
41 String result;
42
43 // based on description from http://en.wikipedia.org/wiki/UTF-8
44
45 if (cp <= 0x7f) {
46 result.resize(1);
47 result[0] = static_cast<char>(cp);
48 } else if (cp <= 0x7FF) {
49 result.resize(2);
50 result[1] = static_cast<char>(0x80 | (0x3f & cp));
51 result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
52 } else if (cp <= 0xFFFF) {
53 result.resize(3);
54 result[2] = static_cast<char>(0x80 | (0x3f & cp));
55 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
56 result[0] = static_cast<char>(0xE0 | (0xf & (cp >> 12)));
57 } else if (cp <= 0x10FFFF) {
58 result.resize(4);
59 result[3] = static_cast<char>(0x80 | (0x3f & cp));
60 result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
61 result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
62 result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
63 }
64
65 return result;
66}
67
68enum {
71 uintToStringBufferSize = 3 * sizeof(LargestUInt) + 1
72};
73
74// Defines a char buffer for use with uintToString().
75using UIntToStringBuffer = char[uintToStringBufferSize];
76
82static inline void uintToString(LargestUInt value, char*& current) {
83 *--current = 0;
84 do {
85 *--current = static_cast<char>(value % 10U + static_cast<unsigned>('0'));
86 value /= 10;
87 } while (value != 0);
88}
89
95template <typename Iter>
96Iter fixNumericLocale(Iter begin, Iter end) {
97 for (; begin != end; ++begin) {
98 if (*begin == ',') {
99 *begin = '.';
100 }
101 }
102 return begin;
103}
104
105template <typename Iter>
106void fixNumericLocaleInput(Iter begin, Iter end) {
107 char decimalPoint = getDecimalPoint();
108 if (decimalPoint == '\0' || decimalPoint == '.') {
109 return;
110 }
111 for (; begin != end; ++begin) {
112 if (*begin == '.') {
113 *begin = decimalPoint;
114 }
115 }
116}
117
122template <typename Iter>
123Iter fixZerosInTheEnd(Iter begin, Iter end) {
124 for (; begin != end; --end) {
125 if (*(end - 1) != '0') {
126 return end;
127 }
128 // Don't delete the last zero before the decimal point.
129 if (begin != (end - 1) && *(end - 2) == '.') {
130 return end;
131 }
132 }
133 return end;
134}
135
136} // namespace Json
137} // namespace nim_cpp_wrapper_util
138
139#endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED
JSON (JavaScript Object Notation).
Definition: allocator.h:14