Debian Codegraph @

core/string_utils.cpp

raw
1 /*
2 Copyright 2015 Google Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of 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,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #include <iomanip>
18
19 #include "static_error.h"
20 #include "string_utils.h"
21
22 namespace jsonnet::internal {
23
24 UString jsonnet_string_unparse(const UString &str, bool single)
25 {
26 UStringStream ss;
27 ss << (single ? U'\'' : U'\"');
28 ss << jsonnet_string_escape(str, single);
29 ss << (single ? U'\'' : U'\"');
30 return ss.str();
31 }
32
33 UString jsonnet_string_escape(const UString &str, bool single)
34 {
35 UStringStream ss;
36 for (std::size_t i = 0; i < str.length(); ++i) {
37 char32_t c = str[i];
38 switch (c) {
39 case U'\"': ss << (single ? U"\"" : U"\\\""); break;
40 case U'\'': ss << (single ? U"\\\'" : U"\'"); break;
41 case U'\\': ss << U"\\\\"; break;
42 case U'\b': ss << U"\\b"; break;
43 case U'\f': ss << U"\\f"; break;
44 case U'\n': ss << U"\\n"; break;
45 case U'\r': ss << U"\\r"; break;
46 case U'\t': ss << U"\\t"; break;
47 case U'\0': ss << U"\\u0000"; break;
48 default: {
49 if (c < 0x20 || (c >= 0x7f && c <= 0x9f)) {
50 // Unprintable, use \u
51 std::stringstream ss8;
52 ss8 << "\\u" << std::hex << std::setfill('0') << std::setw(4)
53 << (unsigned long)(c);
54 ss << decode_utf8(ss8.str());
55 } else {
56 // Printable, write verbatim
57 ss << c;
58 }
59 }
60 }
61 }
62 return ss.str();
63 }
64
65 unsigned long jsonnet_string_parse_unicode(const LocationRange &loc, const char32_t *c)
66 {
67 unsigned long codepoint = 0;
68 // Expect 4 hex digits.
69 for (unsigned i = 0; i < 4; ++i) {
70 auto x = (unsigned char)(c[i]);
71 unsigned digit;
72 if (x == '\0') {
73 auto msg = "Truncated unicode escape sequence in string literal.";
74 throw StaticError(loc, msg);
75 } else if (x >= '0' && x <= '9') {
76 digit = x - '0';
77 } else if (x >= 'a' && x <= 'f') {
78 digit = x - 'a' + 10;
79 } else if (x >= 'A' && x <= 'F') {
80 digit = x - 'A' + 10;
81 } else {
82 std::stringstream ss;
83 ss << "Malformed unicode escape character, "
84 << "should be hex: '" << x << "'";
85 throw StaticError(loc, ss.str());
86 }
87 codepoint *= 16;
88 codepoint += digit;
89 }
90 return codepoint;
91 }
92
93 bool is_bmp_codepoint(const unsigned long codepoint)
94 {
95 return codepoint < 0xd800 || (codepoint >= 0xe000 && codepoint < 0x10000);
96 }
97
98 char32_t decode_utf16_surrogates(const LocationRange &loc, const unsigned long high, const unsigned long low)
99 {
100 if (high >= 0xd800 && high < 0xdc00 && low >= 0xdc00 && low < 0xe000) {
101 return 0x10000 + ((high & 0x03ff) << 10) + (low & 0x03ff);
102 } else {
103 std::stringstream ss;
104 ss << "Invalid UTF-16 bytes";
105 throw StaticError(loc, ss.str());
106 }
107 }
108
109 UString jsonnet_string_unescape(const LocationRange &loc, const UString &s)
110 {
111 UString r;
112 const char32_t *s_ptr = s.c_str();
113 for (const char32_t *c = s_ptr; *c != U'\0'; ++c) {
114 switch (*c) {
115 case '\\':
116 switch (*(++c)) {
117 case '"':
118 case '\'': r += *c; break;
119
120 case '\\': r += *c; break;
121
122 case '/': r += *c; break;
123
124 case 'b': r += '\b'; break;
125
126 case 'f': r += '\f'; break;
127
128 case 'n': r += '\n'; break;
129
130 case 'r': r += '\r'; break;
131
132 case 't': r += '\t'; break;
133
134 case 'u': {
135 ++c; // Consume the 'u'.
136 unsigned long codepoint = jsonnet_string_parse_unicode(loc, c);
137
138 // Leave us on the last char, ready for the ++c at
139 // the outer for loop.
140 c += 3;
141 if (!is_bmp_codepoint(codepoint)) {
142 if (*(++c) != '\\') {
143 std::stringstream ss;
144 ss << "Invalid non-BMP Unicode escape in string literal";
145 throw StaticError(loc, ss.str());
146 }
147 if (*(++c) != 'u') {
148 std::stringstream ss;
149 ss << "Invalid non-BMP Unicode escape in string literal";
150 throw StaticError(loc, ss.str());
151 }
152 ++c;
153 unsigned long codepoint2 = jsonnet_string_parse_unicode(loc, c);
154 c += 3;
155 codepoint = decode_utf16_surrogates(loc, codepoint, codepoint2);
156 }
157 r += codepoint;
158 } break;
159
160 case '\0': {
161 auto msg = "Truncated escape sequence in string literal.";
162 throw StaticError(loc, msg);
163 }
164
165 default: {
166 std::stringstream ss;
167 std::string utf8;
168 encode_utf8(*c, utf8);
169 ss << "Unknown escape sequence in string literal: '" << utf8 << "'";
170 throw StaticError(loc, ss.str());
171 }
172 }
173 break;
174
175 default:
176 // Just a regular letter.
177 r += *c;
178 }
179 }
180 return r;
181 }
182
183 } // namespace jsonnet::internal