summaryrefslogtreecommitdiff
path: root/lib/jsonParse.impl.cpp
blob: ce1020fd59a9dd7d02d78b57a10004763130ce53 (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 + 1, nullptr, 16), str);
}

void
json::jsonParser::appendEscape(unsigned long cp, std::string & str)
{
	if (cp <= 0x7F) {
		str += static_cast<char>(cp);
	}
	else if (cp <= 0x7FF) {
		str += static_cast<char>((cp >> 6) + 192);
		str += static_cast<char>((cp & 63) + 128);
	}
	else if ((0xd800 <= cp && cp <= 0xdfff) || cp > 0x10FFFF) {
		throw std::range_error("Invalid UTF-8 sequence");
	}
	else if (cp <= 0xFFFF) {
		str += static_cast<char>((cp >> 12) + 224);
		str += static_cast<char>(((cp >> 6) & 63) + 128);
		str += static_cast<char>((cp & 63) + 128);
	}
	else {
		str += static_cast<char>((cp >> 18) + 240);
		str += static_cast<char>(((cp >> 12) & 63) + 128);
		str += static_cast<char>(((cp >> 6) & 63) + 128);
		str += static_cast<char>((cp & 63) + 128);
	}
}