Skip to content

Commit d38ba2a

Browse files
committed
- extracted some utility functions out-of reader and parser.
1 parent 130730f commit d38ba2a

3 files changed

Lines changed: 81 additions & 53 deletions

File tree

src/lib_json/json_reader.cpp

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <json/reader.h>
22
#include <json/value.h>
3+
#include "json_tool.h"
34
#include <utility>
45
#include <cstdio>
56
#include <cassert>
@@ -66,42 +67,6 @@ containsNewLine( Reader::Location begin,
6667
return false;
6768
}
6869

69-
static std::string codePointToUTF8(unsigned int cp)
70-
{
71-
std::string result;
72-
73-
// based on description from http://en.wikipedia.org/wiki/UTF-8
74-
75-
if (cp <= 0x7f)
76-
{
77-
result.resize(1);
78-
result[0] = static_cast<char>(cp);
79-
}
80-
else if (cp <= 0x7FF)
81-
{
82-
result.resize(2);
83-
result[1] = static_cast<char>(0x80 | (0x3f & cp));
84-
result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
85-
}
86-
else if (cp <= 0xFFFF)
87-
{
88-
result.resize(3);
89-
result[2] = static_cast<char>(0x80 | (0x3f & cp));
90-
result[1] = 0x80 | static_cast<char>((0x3f & (cp >> 6)));
91-
result[0] = 0xE0 | static_cast<char>((0xf & (cp >> 12)));
92-
}
93-
else if (cp <= 0x10FFFF)
94-
{
95-
result.resize(4);
96-
result[3] = static_cast<char>(0x80 | (0x3f & cp));
97-
result[2] = static_cast<char>(0x80 | (0x3f & (cp >> 6)));
98-
result[1] = static_cast<char>(0x80 | (0x3f & (cp >> 12)));
99-
result[0] = static_cast<char>(0xF0 | (0x7 & (cp >> 18)));
100-
}
101-
102-
return result;
103-
}
104-
10570

10671
// Class Reader
10772
// //////////////////////////////////////////////////////////////////

src/lib_json/json_tool.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 *&current )
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

src/lib_json/json_writer.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <json/writer.h>
2+
#include "json_tool.h"
23
#include <utility>
34
#include <assert.h>
45
#include <stdio.h>
@@ -13,11 +14,6 @@
1314

1415
namespace Json {
1516

16-
static bool isControlCharacter(char ch)
17-
{
18-
return ch > 0 && ch <= 0x1F;
19-
}
20-
2117
static bool containsControlCharacter( const char* str )
2218
{
2319
while ( *str )
@@ -27,17 +23,6 @@ static bool containsControlCharacter( const char* str )
2723
}
2824
return false;
2925
}
30-
static void uintToString( unsigned int value,
31-
char *&current )
32-
{
33-
*--current = 0;
34-
do
35-
{
36-
*--current = (value % 10) + '0';
37-
value /= 10;
38-
}
39-
while ( value != 0 );
40-
}
4126

4227
std::string valueToString( Int value )
4328
{
@@ -116,7 +101,7 @@ std::string valueToQuotedString( const char *value )
116101
// We have to walk value and escape any special characters.
117102
// Appending to std::string is not efficient, but this should be rare.
118103
// (Note: forward slashes are *not* rare, but I am not escaping them.)
119-
unsigned maxsize = strlen(value)*2 + 3; // allescaped+quotes+NULL
104+
std::string::size_type maxsize = strlen(value)*2 + 3; // allescaped+quotes+NULL
120105
std::string result;
121106
result.reserve(maxsize); // to avoid lots of mallocs
122107
result += "\"";

0 commit comments

Comments
 (0)