/src/spotify-json/src/codec/string.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2015-2019 Spotify AB |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 | | * use this file except in compliance with the License. You may obtain a copy of |
6 | | * the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
12 | | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 | | * License for the specific language governing permissions and limitations under |
14 | | * the License. |
15 | | */ |
16 | | |
17 | | #include <spotify/json/codec/string.hpp> |
18 | | |
19 | | #include <spotify/json/decode_exception.hpp> |
20 | | #include <spotify/json/detail/decode_helpers.hpp> |
21 | | #include <spotify/json/detail/escape.hpp> |
22 | | #include <spotify/json/detail/macros.hpp> |
23 | | #include <spotify/json/detail/skip_chars.hpp> |
24 | | |
25 | | namespace spotify { |
26 | | namespace json { |
27 | | namespace codec { |
28 | | namespace { |
29 | | |
30 | 25.2k | bool is_high_surrogate(unsigned p) { |
31 | 25.2k | return (p & 0xFC00) == 0xD800; |
32 | 25.2k | } |
33 | | |
34 | 3.29k | bool is_low_surrogate(unsigned p) { |
35 | 3.29k | return (p & 0xFC00) == 0xDC00; |
36 | 3.29k | } |
37 | | |
38 | 1.11k | uint32_t codepoint_from_surrogate_pair(uint32_t high, uint32_t low) { |
39 | 1.11k | return (((high & 0x03FF) << 10) | (low & 0x03FF)) + 0x10000; |
40 | 1.11k | } |
41 | | |
42 | 114k | uint8_t decode_hex_nibble(decode_context &context, const char c) { |
43 | 114k | if (c >= '0' && c <= '9') { return c - '0'; } |
44 | 67.0k | if (c >= 'a' && c <= 'f') { return c - 'a' + 0xA; } |
45 | 51.8k | if (c >= 'A' && c <= 'F') { return c - 'A' + 0xA; } |
46 | 123 | detail::fail(context, "\\u must be followed by 4 hex digits"); |
47 | 51.8k | } |
48 | | |
49 | 28.7k | unsigned decode_hex_number(decode_context &context) { |
50 | 28.7k | detail::require_bytes<4>(context, "\\u must be followed by 4 hex digits"); |
51 | 28.7k | const auto a = decode_hex_nibble(context, *(context.position++)); |
52 | 28.7k | const auto b = decode_hex_nibble(context, *(context.position++)); |
53 | 28.7k | const auto c = decode_hex_nibble(context, *(context.position++)); |
54 | 28.7k | const auto d = decode_hex_nibble(context, *(context.position++)); |
55 | 28.7k | return unsigned((a << 12) | (b << 8) | (c << 4) | d); |
56 | 28.7k | } |
57 | | |
58 | 1.11k | void encode_utf8_4(std::string &out, uint32_t p) { |
59 | 1.11k | const char c0 = 0xF0 | ((p >> 18) & 0x07); |
60 | 1.11k | const char c1 = 0x80 | ((p >> 12) & 0x3F); |
61 | 1.11k | const char c2 = 0x80 | ((p >> 6) & 0x3F); |
62 | 1.11k | const char c3 = 0x80 | ((p >> 0) & 0x3F); |
63 | 1.11k | const char cc[] = { c0, c1, c2, c3 }; |
64 | 1.11k | out.append(&cc[0], 4); |
65 | 1.11k | } |
66 | | |
67 | 15.2k | void encode_utf8_3(std::string &out, unsigned p) { |
68 | 15.2k | const char c0 = 0xE0 | ((p >> 12) & 0x0F); |
69 | 15.2k | const char c1 = 0x80 | ((p >> 6) & 0x3F); |
70 | 15.2k | const char c2 = 0x80 | ((p >> 0) & 0x3F); |
71 | 15.2k | const char cc[] = { c0, c1, c2 }; |
72 | 15.2k | out.append(&cc[0], 3); |
73 | 15.2k | } |
74 | | |
75 | 7.56k | void encode_utf8_2(std::string &out, unsigned p) { |
76 | 7.56k | const char c0 = 0xC0 | ((p >> 6) & 0x1F); |
77 | 7.56k | const char c1 = 0x80 | ((p >> 0) & 0x3F); |
78 | 7.56k | const char cc[] = { c0, c1 }; |
79 | 7.56k | out.append(&cc[0], 2); |
80 | 7.56k | } |
81 | | |
82 | 1.30k | void encode_utf8_1(std::string &out, unsigned p) { |
83 | 1.30k | const char c0 = (p & 0x7F); |
84 | 1.30k | out.push_back(c0); |
85 | 1.30k | } |
86 | | |
87 | 24.1k | void encode_utf8(std::string &out, unsigned p) { |
88 | 24.1k | if (json_likely(p <= 0x7F)) { |
89 | 1.30k | encode_utf8_1(out, p); |
90 | 22.8k | } else if (json_likely(p <= 0x07FF)) { |
91 | 7.56k | encode_utf8_2(out, p); |
92 | 15.2k | } else { |
93 | 15.2k | encode_utf8_3(out, p); |
94 | 15.2k | } |
95 | 24.1k | } |
96 | | |
97 | 25.2k | bool handle_surrogate_pair(decode_context &context, std::string &out, unsigned p) { |
98 | 25.2k | if (json_unlikely(is_high_surrogate(p))) { |
99 | | // Parse low surrogate |
100 | 7.09k | if (detail::peek_2(context, '\\', 'u')) { |
101 | 3.30k | detail::skip_unchecked_n(context, 2); |
102 | 3.30k | const auto n = decode_hex_number(context); |
103 | 3.30k | if (json_likely(is_low_surrogate(n))) { |
104 | | // Any Unicode codepoint encoded by a surrogate pair is 4 bytes in UTF-8 |
105 | 1.11k | encode_utf8_4(out, codepoint_from_surrogate_pair(p, n)); |
106 | 1.11k | return true; |
107 | 2.19k | } else { |
108 | | // Rewind context to before the escape sequence |
109 | 2.19k | context.position -= 6; |
110 | 2.19k | } |
111 | 3.30k | } |
112 | 7.09k | } |
113 | | |
114 | 24.1k | return false; |
115 | 25.2k | } |
116 | | |
117 | 25.3k | void decode_unicode_escape(decode_context &context, std::string &out) { |
118 | 25.3k | const auto p = decode_hex_number(context); |
119 | 25.3k | if (json_likely(!handle_surrogate_pair(context, out, p))) { |
120 | 24.1k | encode_utf8(out, p); |
121 | 24.1k | } |
122 | 25.3k | } |
123 | | |
124 | 78.0k | void decode_escape(decode_context &context, std::string &out) { |
125 | 78.0k | const auto escape_character = detail::next(context, "Unterminated string"); |
126 | 78.0k | switch (escape_character) { |
127 | 15.3k | case '"': out.push_back('"'); break; |
128 | 1.75k | case '/': out.push_back('/'); break; |
129 | 12.2k | case 'b': out.push_back('\b'); break; |
130 | 1.50k | case 'f': out.push_back('\f'); break; |
131 | 1.55k | case 'n': out.push_back('\n'); break; |
132 | 2.43k | case 'r': out.push_back('\r'); break; |
133 | 2.15k | case 't': out.push_back('\t'); break; |
134 | 15.5k | case '\\': out.push_back('\\'); break; |
135 | 25.3k | case 'u': decode_unicode_escape(context, out); break; |
136 | 42 | default: detail::fail(context, "Invalid escape character", -1); |
137 | 78.0k | } |
138 | 78.0k | } |
139 | | |
140 | 14.7k | string_t::object_type decode_escaped_string(decode_context &context, const char *begin) { |
141 | 14.7k | std::string unescaped(begin, context.position - 1); |
142 | 14.7k | decode_escape(context, unescaped); |
143 | | |
144 | 77.9k | while (json_likely(context.remaining())) { |
145 | 77.5k | const auto begin_simple = context.position; |
146 | 77.5k | detail::skip_any_simple_characters(context); |
147 | 77.5k | unescaped.append(begin_simple, context.position); |
148 | | |
149 | 77.5k | switch (detail::next(context, "Unterminated string")) { |
150 | 14.0k | case '"': return unescaped; |
151 | 63.2k | case '\\': decode_escape(context, unescaped); break; |
152 | 0 | default: json_unreachable(); |
153 | 77.5k | } |
154 | 77.5k | } |
155 | | |
156 | 400 | detail::fail(context, "Unterminated string"); |
157 | 14.7k | } |
158 | | |
159 | 24.5k | string_t::object_type decode_string(decode_context &context) { |
160 | 24.5k | const auto begin_simple = context.position; |
161 | 24.5k | detail::skip_any_simple_characters(context); |
162 | | |
163 | 24.5k | switch (detail::next(context, "Unterminated string")) { |
164 | 9.65k | case '"': return std::string(begin_simple, context.position - 1); |
165 | 14.7k | case '\\': return decode_escaped_string(context, begin_simple); |
166 | 0 | default: json_unreachable(); |
167 | 24.5k | } |
168 | 24.5k | } |
169 | | |
170 | | } // namespace |
171 | | |
172 | 24.5k | string_t::object_type string_t::decode(decode_context &context) const { |
173 | 24.5k | detail::skip_1(context, '"'); |
174 | 24.5k | return decode_string(context); |
175 | 24.5k | } |
176 | | |
177 | 2.33k | void string_t::encode(encode_context &context, const object_type value) const { |
178 | 2.33k | context.append('"'); |
179 | | |
180 | | // Write the strings in 1024 byte chunks, so that we do not have to reserve a |
181 | | // potentially very large buffer for the escaped string. It is possible that |
182 | | // the chunking will happen in the middle of a UTF-8 multi-byte character, but |
183 | | // that is ok since write_escaped will not escape characters with the high bit |
184 | | // set, so the combined escaped string contains the correct UTF-8 characters |
185 | | // in the end. |
186 | 2.33k | auto chunk_begin = value.data(); |
187 | 2.33k | const auto string_end = chunk_begin + value.size(); |
188 | | |
189 | 4.66k | while (chunk_begin != string_end) { |
190 | 2.33k | const auto chunk_end = std::min(chunk_begin + 1024, string_end); |
191 | 2.33k | detail::write_escaped(context, chunk_begin, chunk_end); |
192 | 2.33k | chunk_begin = chunk_end; |
193 | 2.33k | } |
194 | | |
195 | 2.33k | context.append('"'); |
196 | 2.33k | } |
197 | | |
198 | | } // namespace codec |
199 | | } // namespace json |
200 | | } // namespace spotify |