summaryrefslogtreecommitdiff
path: root/lib/jsonParse.impl.cpp
blob: 3ed8e62be497e3c0f7cc494452d7f705c0093420 (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
#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, 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);
	}
}