blob: 4d13eae64562302fe5f87ab22ca045c3a5366b8e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include "jsonParse.h"
#include <cstdlib>
#include <stdexcept>
#include <string>
void
json::jsonParser::LexerError(const char * msg)
{
throw std::runtime_error(msg);
}
void
json::jsonParser::appendEscape(const char * cphs, std::string & str)
{
appendEscape(std::strtoul(cphs + 1, nullptr, 16), str);
}
void
json::jsonParser::appendEscape(unsigned long cp, std::string & str)
{
if (cp <= 0x7F) {
str += (char)cp;
}
else if (cp <= 0x7FF) {
str += char((cp >> 6) + 192);
str += char((cp & 63) + 128);
}
else if (0xd800 <= cp && cp <= 0xdfff) {
throw std::range_error("Invalid UTF-8 sequence");
}
else if (cp <= 0xFFFF) {
str += char((cp >> 12) + 224);
str += char(((cp >> 6) & 63) + 128);
str += char((cp & 63) + 128);
}
else if (cp <= 0x10FFFF) {
str += char((cp >> 18) + 240);
str += char(((cp >> 12) & 63) + 128);
str += char(((cp >> 6) & 63) + 128);
str += char((cp & 63) + 128);
}
else {
throw std::range_error("Invalid UTF-8 sequence");
}
}
|