forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.h
More file actions
61 lines (55 loc) · 1.58 KB
/
JSON.h
File metadata and controls
61 lines (55 loc) · 1.58 KB
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#ifndef HERMES_SUPPORT_JSON_H
#define HERMES_SUPPORT_JSON_H
namespace hermes {
/// Quotes a string given by \p view and puts the quoted version into \p output.
/// \p view should be utf16-encoded, and \p output will be as well.
/// \post output is a container that has a sequential list of utf16 characters
/// that can be embedded into a JSON string.
template <typename Output, typename StringView>
void quoteStringForJSON(Output &output, StringView view) {
// Quote.1.
output.push_back(u'"');
// Quote.2.
for (char16_t ch : view) {
#define ESCAPE(ch, replace) \
case ch: \
output.push_back(u'\\'); \
output.push_back(replace); \
break
switch (ch) {
// Quote.2.a.
ESCAPE(u'\\', u'\\');
ESCAPE(u'"', u'"');
// Quote.2.b.
ESCAPE(u'\b', u'b');
ESCAPE(u'\f', u'f');
ESCAPE(u'\n', u'n');
ESCAPE(u'\r', u'r');
ESCAPE(u'\t', u't');
default:
if (ch < u' ') {
// Quote.2.c.
output.append({u'\\', u'u', u'0', u'0'});
output.push_back(u'0' + (ch / 16));
if (ch % 16 < 10) {
output.push_back(u'0' + (ch % 16));
} else {
output.push_back(u'a' + (ch % 16 - 10));
}
} else {
// Quote.2.d.
output.push_back(ch);
}
}
}
// Quote.3.
output.push_back(u'"');
}
} // namespace hermes
#endif