forked from zeux/meshoptimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.cpp
74 lines (60 loc) · 1.19 KB
/
json.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// This file is part of gltfpack; see gltfpack.h for version/license details
#include "gltfpack.h"
#include <stdio.h>
void comma(std::string& s)
{
char ch = s.empty() ? 0 : s[s.size() - 1];
if (ch != 0 && ch != '[' && ch != '{')
s += ",";
}
void append(std::string& s, size_t v)
{
char buf[32];
snprintf(buf, sizeof(buf), "%zu", v);
s += buf;
}
void append(std::string& s, float v)
{
char buf[64];
snprintf(buf, sizeof(buf), "%.9g", v);
s += buf;
}
void append(std::string& s, const char* v)
{
s += v;
}
void append(std::string& s, const std::string& v)
{
s += v;
}
void appendJson(std::string& s, const char* data)
{
enum State
{
None,
Escape,
Quoted
} state = None;
for (const char* it = data; *it; ++it)
{
char ch = *it;
// whitespace outside of quoted strings can be ignored
if (state != None || !isspace(ch))
s += ch;
// the finite automata tracks whether we're inside a quoted string
switch (state)
{
case None:
state = (ch == '"') ? Quoted : None;
break;
case Quoted:
state = (ch == '"') ? None : (ch == '\\') ? Escape : Quoted;
break;
case Escape:
state = Quoted;
break;
default:
assert(!"Unexpected parsing state");
}
}
}