File: json.cpp

package info (click to toggle)
meshoptimizer 0.15%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,148 kB
  • sloc: cpp: 11,845; ansic: 6,343; javascript: 305; makefile: 109; python: 24
file content (74 lines) | stat: -rw-r--r-- 1,213 bytes parent folder | download
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];
	sprintf(buf, "%zu", v);
	s += buf;
}

void append(std::string& s, float v)
{
	char buf[512];
	sprintf(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* begin, const char* end)
{
	enum State
	{
		None,
		Escape,
		Quoted
	} state = None;

	for (const char* it = begin; it != end; ++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");
		}
	}
}