ckdl/src/str.c

filter: Statementstmt 50.2% 210 / 418 Branchbran 38.9% 120 / 308 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 45.4% 330 / 726 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Text/KDL/XS.pm ↑ Summary ckdl/src/emitter.c »
1 #include "str.h"
2 #include "compat.h"
3 #include "grammar.h"
4 #include "kdl/common.h"
5 #include "utf8.h"
6  
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10  
11 // No buffering in Debug mode to find more bugs
12 #ifdef KDL_DEBUG
13 # define MIN_BUFFER_SIZE 1
14 # define BUFFER_SIZE_INCREMENT 1
15 #else
16 # define BUFFER_SIZE_INCREMENT 1024
17 #endif
18  
19 KDL_EXPORT extern inline kdl_str kdl_borrow_str(kdl_owned_string const* str);
20  
21 0 kdl_str kdl_str_from_cstr(char const* s) { return (kdl_str){s, strlen(s)}; }
22  
23 15236 kdl_owned_string kdl_clone_str(kdl_str const* s)
24 {
25 kdl_owned_string result;
26 15236 result.data = malloc(s->len + 1);
27 15236 ▶ if (result.data != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (result.data != NULL) { 15236 0 15236
28 15236 memcpy(result.data, s->data, s->len);
29 15236 result.data[s->len] = '\0';
30 15236 result.len = s->len;
31 } else {
32 0 result.len = 0;
33 }
34 15236 return result;
35 }
36  
37 19712 void kdl_free_string(kdl_owned_string* s)
38 {
39 19712 ▶ if (s->data != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->data != NULL) { 15841 3871 19712
40 15841 free(s->data);
41 15841 s->data = NULL;
42 }
43 19712 s->len = 0;
44 19712 }
45  
46 828 _kdl_write_buffer _kdl_new_write_buffer(size_t initial_size)
47 {
48 828 return (_kdl_write_buffer){.buf = malloc(initial_size), .buf_len = initial_size, .str_len = 0};
49 }
50  
51 186 void _kdl_free_write_buffer(_kdl_write_buffer* buf)
52 {
53 186 free(buf->buf);
54 186 buf->buf = NULL;
55 186 buf->buf_len = 0;
56 186 buf->str_len = 0;
57 186 }
58  
59 22774 bool _kdl_buf_push_chars(_kdl_write_buffer* buf, char const* s, size_t count)
60 {
61 22774 ▶ if (buf->buf_len - buf->str_len < count) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf->buf_len - buf->str_len < count) { 226 22548 22774
62 226 size_t increment = BUFFER_SIZE_INCREMENT >= count ? BUFFER_SIZE_INCREMENT : count;
63 226 buf->buf_len += increment;
64 226 buf->buf = reallocf(buf->buf, buf->buf_len);
65 226 ▶ if (buf->buf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf->buf == NULL) { 0 226 226
66 0 return false;
67 }
68 }
69 22774 memcpy(buf->buf + buf->str_len, s, count);
70 22774 buf->str_len += count;
71 22774 return true;
72 }
73  
74 4 bool _kdl_buf_push_char(_kdl_write_buffer* buf, char c) { return _kdl_buf_push_chars(buf, &c, 1); }
75  
76 1019 bool _kdl_buf_push_codepoint(_kdl_write_buffer* buf, uint32_t c)
77 {
78 // 4 is the maximum length of a character in UTF-8
79 1019 ▶ if (buf->buf_len - buf->str_len < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf->buf_len - buf->str_len < 4) { 326 693 1019
80 326 size_t increment = BUFFER_SIZE_INCREMENT >= 4 ? BUFFER_SIZE_INCREMENT : 4;
81 326 buf->buf_len += increment;
82 326 buf->buf = reallocf(buf->buf, buf->buf_len);
83 326 ▶ if (buf->buf == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf->buf == NULL) { 0 326 326
84 0 return false;
85 }
86 }
87 1019 size_t pushed = _kdl_push_codepoint(c, buf->buf + buf->str_len);
88 1019 ▶ if (pushed == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pushed == 0) { 0 1019 1019
89 0 return false;
90 } else {
91 1019 buf->str_len += pushed;
92 1019 return true;
93 }
94 }
95  
96 642 kdl_owned_string _kdl_buf_to_string(_kdl_write_buffer* buf)
97 {
98 642 kdl_owned_string s = {reallocf(buf->buf, buf->str_len + 1), buf->str_len};
99 642 ▶ if (s.data == NULL) s.len = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s.data == NULL) s.len = 0; 0 642 642
100 642 buf->buf = NULL;
101 642 buf->buf_len = 0;
102 642 buf->str_len = 0;
103 642 s.data[s.len] = '\0';
104 642 return s;
105 }
106  
107 0 kdl_owned_string kdl_escape(kdl_str const* s, kdl_escape_mode mode) { return kdl_escape_v1(s, mode); }
108  
109 0 kdl_owned_string kdl_unescape(kdl_str const* s) { return kdl_unescape_v1(s); }
110  
111 237 kdl_owned_string kdl_escape_v(kdl_version version, kdl_str const* s, kdl_escape_mode mode)
112 {
113 237 switch (version) {
114 79 case KDL_VERSION_1:
115 79 return kdl_escape_v1(s, mode);
116 158 case KDL_VERSION_2:
117 158 return kdl_escape_v2(s, mode);
118 0 default:
119 0 return (kdl_owned_string){NULL, 0};
120 }
121 }
122  
123 0 kdl_owned_string kdl_unescape_v(kdl_version version, kdl_str const* s)
124 {
125 0 switch (version) {
126 0 case KDL_VERSION_1:
127 0 return kdl_unescape_v1(s);
128 0 case KDL_VERSION_2:
129 0 return kdl_unescape_v2_single_line(s);
130 0 default:
131 0 return (kdl_owned_string){NULL, 0};
132 }
133 }
134  
135 0 kdl_owned_string kdl_unescape_multi_line(kdl_version version, kdl_str const* s)
136 {
137 0 switch (version) {
138 0 case KDL_VERSION_1:
139 0 return kdl_unescape_v1(s);
140 0 case KDL_VERSION_2:
141 0 return kdl_unescape_v2_multi_line(s);
142 0 default:
143 0 return (kdl_owned_string){NULL, 0};
144 }
145 }
146  
147 79 kdl_owned_string kdl_escape_v1(kdl_str const* s, kdl_escape_mode mode)
148 {
149 kdl_owned_string result;
150 79 kdl_str unescaped = *s;
151  
152 79 size_t orig_len = unescaped.len;
153 79 _kdl_write_buffer buf = _kdl_new_write_buffer(2 * orig_len);
154 79 ▶ if (buf.buf == NULL) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (buf.buf == NULL) goto esc_error; 79 0 79
155  
156 uint32_t c;
157  
158 207 while (true) {
159 286 char const* orig_char = unescaped.data;
160 286 switch (_kdl_pop_codepoint(&unescaped, &c)) {
161 79 case KDL_UTF8_EOF:
162 79 goto esc_eof;
163 207 case KDL_UTF8_OK:
164 207 break;
165 0 default: // error
166 0 goto esc_error;
167 }
168  
169 207 ▶ if (c == 0x0A && (mode & KDL_ESCAPE_NEWLINE)) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == 0x0A && (mode & KDL_ESCAPE_NEWLINE)) { 1 206 207
if (c == 0x0A && (mode & KDL_ESCAPE_NEWLINE)) { 0 1 1
170 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\n", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\n", 2)) goto esc_error; 0 0 0
171 207 ▶ } else if (c == 0x0D && (mode & KDL_ESCAPE_NEWLINE)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == 0x0D && (mode & KDL_ESCAPE_NEWLINE)) { 0 207 207
} else if (c == 0x0D && (mode & KDL_ESCAPE_NEWLINE)) { 0 0 0
172 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\r", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\r", 2)) goto esc_error; 0 0 0
173 207 ▶ } else if (c == 0x09 && (mode & KDL_ESCAPE_TAB)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == 0x09 && (mode & KDL_ESCAPE_TAB)) { 0 207 207
} else if (c == 0x09 && (mode & KDL_ESCAPE_TAB)) { 0 0 0
174 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\t", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\t", 2)) goto esc_error; 0 0 0
175 207 ▶ } else if (c == 0x5C) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == 0x5C) { 0 207 207
176 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\\\", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\\\", 2)) goto esc_error; 0 0 0
177 207 ▶ } else if (c == 0x22) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == 0x22) { 0 207 207
178 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\\"", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\\"", 2)) goto esc_error; 0 0 0
179 207 ▶ } else if (c == 0x08 && (mode & KDL_ESCAPE_CONTROL)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == 0x08 && (mode & KDL_ESCAPE_CONTROL)) { 0 207 207
} else if (c == 0x08 && (mode & KDL_ESCAPE_CONTROL)) { 0 0 0
180 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\b", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\b", 2)) goto esc_error; 0 0 0
181 207 ▶ } else if (c == 0x0C && (mode & KDL_ESCAPE_NEWLINE)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == 0x0C && (mode & KDL_ESCAPE_NEWLINE)) { 0 207 207
} else if (c == 0x0C && (mode & KDL_ESCAPE_NEWLINE)) { 0 0 0
182 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\f", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\f", 2)) goto esc_error; 0 0 0
183 207 ▶ } else if (((mode & KDL_ESCAPE_CONTROL)
Branch: 1 branch
BranchTrueFalseTotal
} else if (((mode & KDL_ESCAPE_CONTROL) 204 3 207
184 204 ▶ && ((c < 0x20 && c != 0x0A && c != 0x0D && c != 0x09 && c != 0x0C)
Branch: 5 branches
BranchTrueFalseTotal
&& ((c < 0x20 && c != 0x0A && c != 0x0D && c != 0x09 && c != 0x0C) 0 204 204
&& ((c < 0x20 && c != 0x0A && c != 0x0D && c != 0x09 && c != 0x0C) 0 0 0
&& ((c < 0x20 && c != 0x0A && c != 0x0D && c != 0x09 && c != 0x0C) 0 0 0
&& ((c < 0x20 && c != 0x0A && c != 0x0D && c != 0x09 && c != 0x0C) 0 0 0
&& ((c < 0x20 && c != 0x0A && c != 0x0D && c != 0x09 && c != 0x0C) 0 0 0
185 204 ▶ || c == 0x7F /* DEL */))
Branch: 1 branch
BranchTrueFalseTotal
|| c == 0x7F /* DEL */)) 204 0 204
186 207 ▶ || ((mode & KDL_ESCAPE_NEWLINE) && (c == 0x85 || c == 0x2028 || c == 0x2029))
Branch: 4 branches
BranchTrueFalseTotal
|| ((mode & KDL_ESCAPE_NEWLINE) && (c == 0x85 || c == 0x2028 || c == 0x2029)) 204 3 207
|| ((mode & KDL_ESCAPE_NEWLINE) && (c == 0x85 || c == 0x2028 || c == 0x2029)) 204 0 204
|| ((mode & KDL_ESCAPE_NEWLINE) && (c == 0x85 || c == 0x2028 || c == 0x2029)) 204 0 204
|| ((mode & KDL_ESCAPE_NEWLINE) && (c == 0x85 || c == 0x2028 || c == 0x2029)) 204 0 204
187 207 ▶ || ((mode & KDL_ESCAPE_ASCII_MODE) == KDL_ESCAPE_ASCII_MODE && c >= 0x7f)) {
Branch: 2 branches
BranchTrueFalseTotal
|| ((mode & KDL_ESCAPE_ASCII_MODE) == KDL_ESCAPE_ASCII_MODE && c >= 0x7f)) { 0 207 207
|| ((mode & KDL_ESCAPE_ASCII_MODE) == KDL_ESCAPE_ASCII_MODE && c >= 0x7f)) { 0 0 0
188 // \u escape
189 char u_esc_buf[11];
190 0 int count = snprintf(u_esc_buf, 11, "\\u{%x}", (unsigned int)c);
191 0 ▶ if (count < 0 || !_kdl_buf_push_chars(&buf, u_esc_buf, count)) {
Branch: 2 branches
BranchTrueFalseTotal
if (count < 0 || !_kdl_buf_push_chars(&buf, u_esc_buf, count)) { 0 0 0
if (count < 0 || !_kdl_buf_push_chars(&buf, u_esc_buf, count)) { 0 0 0
192 0 goto esc_error;
193 }
194 207 ▶ } else if (c > 0x10ffff) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c > 0x10ffff) { 0 207 207
195 // Not a valid character
196 0 goto esc_error;
197 } else {
198 // keep the rest
199 207 _kdl_buf_push_chars(&buf, orig_char, unescaped.data - orig_char);
200 }
201 }
202 79 esc_eof:
203 79 return _kdl_buf_to_string(&buf);
204  
205 0 esc_error:
206 0 _kdl_free_write_buffer(&buf);
207 0 result = (kdl_owned_string){NULL, 0};
208 0 return result;
209 }
210  
211 77 kdl_owned_string kdl_unescape_v1(kdl_str const* s)
212 {
213 kdl_owned_string result;
214 77 kdl_str escaped = *s;
215 uint32_t c;
216  
217 77 size_t orig_len = escaped.len;
218 77 _kdl_write_buffer buf = _kdl_new_write_buffer(2 * orig_len);
219 77 ▶ if (buf.buf == NULL) goto unesc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (buf.buf == NULL) goto unesc_error; 0 77 77
220  
221 77 char const* p = s->data;
222 77 char const* end = p + s->len;
223  
224 154 ▶ while (p != end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p != end) { 77 77 154
225 77 ▶ if (*p == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\\') { 2 75 77
226 // deal with the escape
227 2 ▶ if (++p == end) goto unesc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (++p == end) goto unesc_error; 0 2 2
228 2 switch (*p) {
229 0 case 'n':
230 0 _kdl_buf_push_char(&buf, '\n');
231 0 ++p;
232 0 break;
233 0 case 'r':
234 0 _kdl_buf_push_char(&buf, '\r');
235 0 ++p;
236 0 break;
237 0 case 't':
238 0 _kdl_buf_push_char(&buf, '\t');
239 0 ++p;
240 0 break;
241 0 case '\\':
242 0 _kdl_buf_push_char(&buf, '\\');
243 0 ++p;
244 0 break;
245 0 case '/':
246 0 _kdl_buf_push_char(&buf, '/');
247 0 ++p;
248 0 break;
249 0 case '"':
250 0 _kdl_buf_push_char(&buf, '\"');
251 0 ++p;
252 0 break;
253 0 case 'b':
254 0 _kdl_buf_push_char(&buf, '\b');
255 0 ++p;
256 0 break;
257 0 case 'f':
258 0 _kdl_buf_push_char(&buf, '\f');
259 0 ++p;
260 0 break;
261 2 case 'u':
262 // u should be followed by {
263 2 ▶ if (++p == end || *(p++) != '{') goto unesc_error;
Branch: 2 branches
BranchTrueFalseTotal
if (++p == end || *(p++) != '{') goto unesc_error; 2 0 2
if (++p == end || *(p++) != '{') goto unesc_error; 0 2 2
264 // parse hex
265 2 c = 0;
266 8 for (;; ++p) {
267 10 ▶ if (p == end) goto unesc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (p == end) goto unesc_error; 0 10 10
268 10 ▶ else if (*p == '}') break;
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '}') break; 2 8 10
269 8 ▶ else if (*p >= '0' && *p <= '9') c = (c << 4) + (*p - '0');
Branch: 2 branches
BranchTrueFalseTotal
else if (*p >= '0' && *p <= '9') c = (c << 4) + (*p - '0'); 8 0 8
else if (*p >= '0' && *p <= '9') c = (c << 4) + (*p - '0'); 6 2 8
270 2 ▶ else if (*p >= 'a' && *p <= 'f') c = (c << 4) + (*p - 'a' + 0xa);
Branch: 2 branches
BranchTrueFalseTotal
else if (*p >= 'a' && *p <= 'f') c = (c << 4) + (*p - 'a' + 0xa); 0 2 2
else if (*p >= 'a' && *p <= 'f') c = (c << 4) + (*p - 'a' + 0xa); 0 0 0
271 2 ▶ else if (*p >= 'A' && *p <= 'F') c = (c << 4) + (*p - 'A' + 0xa);
Branch: 2 branches
BranchTrueFalseTotal
else if (*p >= 'A' && *p <= 'F') c = (c << 4) + (*p - 'A' + 0xa); 2 0 2
else if (*p >= 'A' && *p <= 'F') c = (c << 4) + (*p - 'A' + 0xa); 2 0 2
272 0 else goto unesc_error;
273 }
274 2 ▶ if (!_kdl_buf_push_codepoint(&buf, c)) goto unesc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_codepoint(&buf, c)) goto unesc_error; 0 2 2
275 2 ++p;
276 2 break;
277 0 default:
278 // stray backslashes are not allowed
279 0 goto unesc_error;
280 }
281 }
282  
283 77 char const* start = p;
284 340 ▶ while (p != end && *p != '\\') ++p;
Branch: 2 branches
BranchTrueFalseTotal
while (p != end && *p != '\\') ++p; 263 77 340
while (p != end && *p != '\\') ++p; 263 0 263
285 // copy everything until the backslash
286 77 ▶ if (!_kdl_buf_push_chars(&buf, start, (p - start))) goto unesc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, start, (p - start))) goto unesc_error; 0 77 77
287 }
288  
289 77 return _kdl_buf_to_string(&buf);
290  
291 0 unesc_error:
292 0 _kdl_free_write_buffer(&buf);
293 0 result = (kdl_owned_string){NULL, 0};
294 0 return result;
295 }
296  
297 158 kdl_owned_string kdl_escape_v2(kdl_str const* s, kdl_escape_mode mode)
298 {
299 kdl_owned_string result;
300 158 kdl_str unescaped = *s;
301  
302 158 size_t orig_len = unescaped.len;
303 158 _kdl_write_buffer buf = _kdl_new_write_buffer(2 * orig_len);
304 158 ▶ if (buf.buf == NULL) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (buf.buf == NULL) goto esc_error; 158 0 158
305  
306 uint32_t c;
307  
308 448 while (true) {
309 606 char const* orig_char = unescaped.data;
310 606 switch (_kdl_pop_codepoint(&unescaped, &c)) {
311 158 case KDL_UTF8_EOF:
312 158 goto esc_eof;
313 448 case KDL_UTF8_OK:
314 448 break;
315 0 default: // error
316 0 goto esc_error;
317 }
318  
319 448 ▶ if (c > 0x10ffff) {
Branch: 1 branch
BranchTrueFalseTotal
if (c > 0x10ffff) { 0 448 448
320 // Not a valid character
321 0 goto esc_error;
322 448 ▶ } else if (c == 0x0A && (mode & KDL_ESCAPE_NEWLINE)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == 0x0A && (mode & KDL_ESCAPE_NEWLINE)) { 1 447 448
} else if (c == 0x0A && (mode & KDL_ESCAPE_NEWLINE)) { 1 0 1
323 1 ▶ if (!_kdl_buf_push_chars(&buf, "\\n", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\n", 2)) goto esc_error; 0 1 1
324 447 ▶ } else if (c == 0x0D && (mode & KDL_ESCAPE_NEWLINE)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == 0x0D && (mode & KDL_ESCAPE_NEWLINE)) { 0 447 447
} else if (c == 0x0D && (mode & KDL_ESCAPE_NEWLINE)) { 0 0 0
325 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\r", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\r", 2)) goto esc_error; 0 0 0
326 447 ▶ } else if (c == 0x09 && (mode & KDL_ESCAPE_TAB)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == 0x09 && (mode & KDL_ESCAPE_TAB)) { 0 447 447
} else if (c == 0x09 && (mode & KDL_ESCAPE_TAB)) { 0 0 0
327 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\t", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\t", 2)) goto esc_error; 0 0 0
328 447 ▶ } else if (c == 0x5C) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == 0x5C) { 0 447 447
329 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\\\", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\\\", 2)) goto esc_error; 0 0 0
330 447 ▶ } else if (c == 0x22) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == 0x22) { 0 447 447
331 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\\"", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\\"", 2)) goto esc_error; 0 0 0
332 447 ▶ } else if (c == 0x08 && (mode & KDL_ESCAPE_CONTROL)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == 0x08 && (mode & KDL_ESCAPE_CONTROL)) { 0 447 447
} else if (c == 0x08 && (mode & KDL_ESCAPE_CONTROL)) { 0 0 0
333 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\b", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\b", 2)) goto esc_error; 0 0 0
334 447 ▶ } else if (c == 0x0C && (mode & KDL_ESCAPE_NEWLINE)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c == 0x0C && (mode & KDL_ESCAPE_NEWLINE)) { 0 447 447
} else if (c == 0x0C && (mode & KDL_ESCAPE_NEWLINE)) { 0 0 0
335 0 ▶ if (!_kdl_buf_push_chars(&buf, "\\f", 2)) goto esc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_buf_push_chars(&buf, "\\f", 2)) goto esc_error; 0 0 0
336 447 ▶ } else if (_kdl_is_illegal_char(KDL_CHARACTER_SET_V2, c)
Branch: 1 branch
BranchTrueFalseTotal
} else if (_kdl_is_illegal_char(KDL_CHARACTER_SET_V2, c) 447 0 447
337 447 ▶ || ((mode & KDL_ESCAPE_CONTROL) && c == 0x0B /* vertical tab */)
Branch: 2 branches
BranchTrueFalseTotal
|| ((mode & KDL_ESCAPE_CONTROL) && c == 0x0B /* vertical tab */) 445 2 447
|| ((mode & KDL_ESCAPE_CONTROL) && c == 0x0B /* vertical tab */) 445 0 445
338 447 ▶ || ((mode & KDL_ESCAPE_NEWLINE) && (c == 0x85 || c == 0x2028 || c == 0x2029))
Branch: 4 branches
BranchTrueFalseTotal
|| ((mode & KDL_ESCAPE_NEWLINE) && (c == 0x85 || c == 0x2028 || c == 0x2029)) 447 0 447
|| ((mode & KDL_ESCAPE_NEWLINE) && (c == 0x85 || c == 0x2028 || c == 0x2029)) 447 0 447
|| ((mode & KDL_ESCAPE_NEWLINE) && (c == 0x85 || c == 0x2028 || c == 0x2029)) 447 0 447
|| ((mode & KDL_ESCAPE_NEWLINE) && (c == 0x85 || c == 0x2028 || c == 0x2029)) 447 0 447
339 448 ▶ || ((mode & KDL_ESCAPE_ASCII_MODE) == KDL_ESCAPE_ASCII_MODE && c >= 0x7f)) {
Branch: 2 branches
BranchTrueFalseTotal
|| ((mode & KDL_ESCAPE_ASCII_MODE) == KDL_ESCAPE_ASCII_MODE && c >= 0x7f)) { 4 443 447
|| ((mode & KDL_ESCAPE_ASCII_MODE) == KDL_ESCAPE_ASCII_MODE && c >= 0x7f)) { 1 3 4
340 // \u escape
341 char u_esc_buf[11];
342 1 int count = snprintf(u_esc_buf, 11, "\\u{%x}", (unsigned int)c);
343 1 ▶ if (count < 0 || !_kdl_buf_push_chars(&buf, u_esc_buf, count)) {
Branch: 2 branches
BranchTrueFalseTotal
if (count < 0 || !_kdl_buf_push_chars(&buf, u_esc_buf, count)) { 1 0 1
if (count < 0 || !_kdl_buf_push_chars(&buf, u_esc_buf, count)) { 0 1 1
344 0 goto esc_error;
345 }
346 } else {
347 // keep the rest
348 446 _kdl_buf_push_chars(&buf, orig_char, unescaped.data - orig_char);
349 }
350 }
351 158 esc_eof:
352 158 return _kdl_buf_to_string(&buf);
353  
354 0 esc_error:
355 0 _kdl_free_write_buffer(&buf);
356 0 result = (kdl_owned_string){NULL, 0};
357 0 return result;
358 }
359  
360 165 kdl_owned_string kdl_unescape_v2_single_line(kdl_str const* s)
361 {
362 kdl_owned_string result;
363 165 kdl_owned_string no_ws_escapes = _kdl_remove_escaped_whitespace(s);
364 165 kdl_str escaped = kdl_borrow_str(&no_ws_escapes);
365  
366 165 ▶ if (_kdl_str_contains_newline(&escaped)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_str_contains_newline(&escaped)) { 0 165 165
367 0 result = (kdl_owned_string){NULL, 0};
368 } else {
369 165 result = _kdl_resolve_escapes_v2(&escaped);
370 }
371  
372 165 kdl_free_string(&no_ws_escapes);
373 165 return result;
374 }
375  
376 0 kdl_owned_string kdl_unescape_v2_multi_line(kdl_str const* s)
377 {
378 kdl_owned_string result;
379 0 kdl_owned_string no_ws_escapes = _kdl_remove_escaped_whitespace(s);
380 0 kdl_str pre_dedent = kdl_borrow_str(&no_ws_escapes);
381 0 kdl_owned_string dedented = _kdl_dedent_multiline_string(&pre_dedent);
382 0 kdl_str escaped = kdl_borrow_str(&dedented);
383  
384 0 kdl_free_string(&no_ws_escapes);
385  
386 0 ▶ if (dedented.data == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (dedented.data == NULL) { 0 0 0
387 // dedenting error
388 0 result = (kdl_owned_string){NULL, 0};
389 } else {
390 0 result = _kdl_resolve_escapes_v2(&escaped);
391 }
392  
393 0 kdl_free_string(&dedented);
394  
395 0 return result;
396 }
397  
398 0 kdl_owned_string _kdl_dedent_multiline_string(kdl_str const* s)
399 {
400 kdl_owned_string result;
401  
402 uint32_t c;
403 0 kdl_str orig = *s;
404 kdl_utf8_status status;
405  
406 0 char* buf_dedented = NULL;
407  
408 // Normalize newlines first
409 0 _kdl_write_buffer buf_norm_lf = _kdl_new_write_buffer(s->len);
410  
411 0 ▶ while ((status = _kdl_pop_codepoint(&orig, &c)) == KDL_UTF8_OK) {
Branch: 1 branch
BranchTrueFalseTotal
while ((status = _kdl_pop_codepoint(&orig, &c)) == KDL_UTF8_OK) { 0 0 0
412 0 ▶ if (_kdl_is_newline(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_is_newline(c)) { 0 0 0
413 // Normalize CRLF
414 0 ▶ if (c == '\r' && orig.len >= 1 && orig.data[0] == '\n') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '\r' && orig.len >= 1 && orig.data[0] == '\n') { 0 0 0
if (c == '\r' && orig.len >= 1 && orig.data[0] == '\n') { 0 0 0
if (c == '\r' && orig.len >= 1 && orig.data[0] == '\n') { 0 0 0
415 0 ++orig.data;
416 0 --orig.len;
417 }
418 // every newline becomes a line feed
419 0 _kdl_buf_push_char(&buf_norm_lf, '\n');
420 } else {
421 0 _kdl_buf_push_codepoint(&buf_norm_lf, c);
422 }
423 }
424  
425 0 ▶ if (status != KDL_UTF8_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (status != KDL_UTF8_EOF) { 0 0 0
426 0 goto dedent_err;
427 }
428  
429 0 kdl_str norm_lf = (kdl_str){.data = buf_norm_lf.buf, .len = buf_norm_lf.str_len};
430  
431 // What's after the final newline?
432 0 char const* final_newline = NULL;
433 0 ▶ for (char const* p = norm_lf.data + norm_lf.len - 1; p >= norm_lf.data; --p) {
Branch: 1 branch
BranchTrueFalseTotal
for (char const* p = norm_lf.data + norm_lf.len - 1; p >= norm_lf.data; --p) { 0 0 0
434 0 ▶ if (*p == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') { 0 0 0
435 0 final_newline = p;
436 0 break;
437 }
438 }
439  
440 0 ▶ if (final_newline == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (final_newline == NULL) { 0 0 0
441 // no newlines: this is not a valid multi-line string!
442 0 goto dedent_err;
443 }
444  
445 0 kdl_str indent
446 0 = (kdl_str){.data = final_newline + 1, .len = (norm_lf.data + norm_lf.len) - (final_newline + 1)};
447  
448 // Check that the indentation is all whitespace
449 0 kdl_str indent_ = indent;
450 0 ▶ while (_kdl_pop_codepoint(&indent_, &c) == KDL_UTF8_OK) {
Branch: 1 branch
BranchTrueFalseTotal
while (_kdl_pop_codepoint(&indent_, &c) == KDL_UTF8_OK) { 0 0 0
451 0 ▶ if (!_kdl_is_whitespace(KDL_CHARACTER_SET_V2, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_is_whitespace(KDL_CHARACTER_SET_V2, c)) { 0 0 0
452 0 goto dedent_err;
453 }
454 }
455  
456 // The first character of the string MUST be a newline
457 0 ▶ if (norm_lf.data[0] != '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (norm_lf.data[0] != '\n') { 0 0 0
458 0 goto dedent_err;
459 }
460  
461 // Remove the whitespace from the beginning of all lines
462 0 buf_dedented = malloc(norm_lf.len);
463 0 char* out = buf_dedented;
464 0 char const* in = norm_lf.data + 1; // skip initial LF
465 0 char const* end = norm_lf.data + norm_lf.len;
466 // copy the rest of the string
467 0 ▶ while (in < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (in < end) { 0 0 0
468 // find the next newline
469 0 char const* eol = in;
470 0 ▶ while (eol < end && *eol != '\n') ++eol;
Branch: 2 branches
BranchTrueFalseTotal
while (eol < end && *eol != '\n') ++eol; 0 0 0
while (eol < end && *eol != '\n') ++eol; 0 0 0
471 0 ▶ if (eol == end) break;
Branch: 1 branch
BranchTrueFalseTotal
if (eol == end) break; 0 0 0
472  
473 // check if the line is all whitespace
474 0 kdl_str line = (kdl_str){.data = in, .len = (eol - in)};
475 uint32_t c;
476 0 bool is_ws = true;
477 0 ▶ while (_kdl_pop_codepoint(&line, &c) == KDL_UTF8_OK) {
Branch: 1 branch
BranchTrueFalseTotal
while (_kdl_pop_codepoint(&line, &c) == KDL_UTF8_OK) { 0 0 0
478 0 ▶ if (!_kdl_is_whitespace(KDL_CHARACTER_SET_V2, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!_kdl_is_whitespace(KDL_CHARACTER_SET_V2, c)) { 0 0 0
479 0 is_ws = false;
480 0 break;
481 }
482 }
483  
484 0 ▶ if (is_ws) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ws) { 0 0 0
485 // push a single newline
486 0 *(out++) = '\n';
487 } else {
488 // check if the line starts with the indent
489 0 size_t line_len = eol - in;
490 0 ▶ if (line_len >= indent.len && memcmp(in, indent.data, indent.len) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (line_len >= indent.len && memcmp(in, indent.data, indent.len) == 0) { 0 0 0
if (line_len >= indent.len && memcmp(in, indent.data, indent.len) == 0) { 0 0 0
491 // copy the line after the indent to the output (incl newline)
492 0 size_t n = line_len - indent.len + 1;
493 0 memcpy(out, in + indent.len, n);
494 0 out += n;
495 } else {
496 0 goto dedent_err;
497 }
498 }
499  
500 0 in = eol + 1;
501 }
502  
503 0 size_t len = out - buf_dedented;
504 // Strip the final line feed
505 0 ▶ if (len > 0 && buf_dedented[len - 1] == '\n') --len;
Branch: 2 branches
BranchTrueFalseTotal
if (len > 0 && buf_dedented[len - 1] == '\n') --len; 0 0 0
if (len > 0 && buf_dedented[len - 1] == '\n') --len; 0 0 0
506 0 buf_dedented = realloc(buf_dedented, len + 1);
507 0 buf_dedented[len] = '\0';
508 0 return (kdl_owned_string){.data = buf_dedented, .len = len};
509  
510 0 dedent_err:
511 0 _kdl_free_write_buffer(&buf_norm_lf);
512 0 ▶ if (buf_dedented != NULL) free(buf_dedented);
Branch: 1 branch
BranchTrueFalseTotal
if (buf_dedented != NULL) free(buf_dedented); 0 0 0
513 0 result = (kdl_owned_string){NULL, 0};
514 0 return result;
515 }
516  
517 165 kdl_owned_string _kdl_remove_escaped_whitespace(kdl_str const* s)
518 {
519 kdl_owned_string result;
520 165 kdl_str escaped = *s;
521  
522 165 size_t orig_len = escaped.len;
523 165 _kdl_write_buffer buf = _kdl_new_write_buffer(orig_len);
524 165 ▶ if (buf.buf == NULL) goto unesc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (buf.buf == NULL) goto unesc_error; 0 165 165
525 165 ▶ if (escaped.data == NULL) goto unesc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (escaped.data == NULL) goto unesc_error; 0 165 165
526  
527 165 uint32_t c = 0;
528 kdl_utf8_status status;
529  
530 681 ▶ while ((status = _kdl_pop_codepoint(&escaped, &c)) == KDL_UTF8_OK) {
Branch: 1 branch
BranchTrueFalseTotal
while ((status = _kdl_pop_codepoint(&escaped, &c)) == KDL_UTF8_OK) { 516 165 681
531 516 ▶ if (_kdl_is_illegal_char(KDL_CHARACTER_SET_V2, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_is_illegal_char(KDL_CHARACTER_SET_V2, c)) { 0 516 516
532 0 goto unesc_error;
533 516 ▶ } else if (c == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '\\') { 3 513 516
534 3 kdl_str tail = escaped; // make a copy - we will advance too far
535 3 bool removed_whitespace = false;
536  
537 3 while ((status = _kdl_pop_codepoint(&tail, &c)) == KDL_UTF8_OK
538 3 ▶ && (_kdl_is_whitespace(KDL_CHARACTER_SET_V2, c) || _kdl_is_newline(c))) {
Branch: 3 branches
BranchTrueFalseTotal
&& (_kdl_is_whitespace(KDL_CHARACTER_SET_V2, c) || _kdl_is_newline(c))) { 3 0 3
&& (_kdl_is_whitespace(KDL_CHARACTER_SET_V2, c) || _kdl_is_newline(c))) { 0 3 3
&& (_kdl_is_whitespace(KDL_CHARACTER_SET_V2, c) || _kdl_is_newline(c))) { 0 3 3
539 // skip this char
540 0 escaped = tail;
541 0 removed_whitespace = true;
542 }
543  
544 3 ▶ switch (status) { // why did the loop end?
Branch: 1 branch
BranchTrueFalseTotal
switch (status) { // why did the loop end? 3 0 3
545 3 case KDL_UTF8_OK:
546 case KDL_UTF8_EOF:
547 3 break;
548 0 default:
549 0 goto unesc_error;
550 }
551  
552 3 ▶ if (!removed_whitespace) {
Branch: 1 branch
BranchTrueFalseTotal
if (!removed_whitespace) { 3 0 3
553 // no whitespace -> keep backslash for kdl_unescape_v2()
554 3 _kdl_buf_push_char(&buf, '\\');
555 // definitely keep the next char as well
556 3 ▶ if ((status = _kdl_pop_codepoint(&escaped, &c)) == KDL_UTF8_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ((status = _kdl_pop_codepoint(&escaped, &c)) == KDL_UTF8_OK) { 3 0 3
557 3 _kdl_buf_push_codepoint(&buf, c);
558 }
559 }
560 } else {
561 // Nothing special, copy the character
562 513 _kdl_buf_push_codepoint(&buf, c);
563 }
564 }
565  
566 165 ▶ if (status == KDL_UTF8_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == KDL_UTF8_EOF) { 165 0 165
567 // ok
568 165 result = _kdl_buf_to_string(&buf);
569 165 return result;
570 } else {
571 0 goto unesc_error;
572 }
573  
574 0 unesc_error:
575 0 _kdl_free_write_buffer(&buf);
576 0 result = (kdl_owned_string){NULL, 0};
577 0 return result;
578 }
579  
580 165 kdl_owned_string _kdl_resolve_escapes_v2(kdl_str const* s)
581 {
582 kdl_owned_string result;
583 165 kdl_str escaped = *s;
584  
585 165 size_t orig_len = escaped.len;
586 165 _kdl_write_buffer buf = _kdl_new_write_buffer(orig_len);
587 165 ▶ if (buf.buf == NULL) goto unesc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (buf.buf == NULL) goto unesc_error; 0 165 165
588  
589 165 uint32_t c = 0;
590 kdl_utf8_status status;
591  
592 667 ▶ while ((status = _kdl_pop_codepoint(&escaped, &c)) == KDL_UTF8_OK) {
Branch: 1 branch
BranchTrueFalseTotal
while ((status = _kdl_pop_codepoint(&escaped, &c)) == KDL_UTF8_OK) { 504 163 667
593 504 ▶ if (_kdl_is_illegal_char(KDL_CHARACTER_SET_V2, c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_is_illegal_char(KDL_CHARACTER_SET_V2, c)) { 0 504 504
594 0 goto unesc_error;
595 504 ▶ } else if (c == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '\\') { 3 501 504
596 3 ▶ if (_kdl_pop_codepoint(&escaped, &c) != KDL_UTF8_OK) goto unesc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_pop_codepoint(&escaped, &c) != KDL_UTF8_OK) goto unesc_error; 0 3 3
597  
598 3 switch (c) {
599 1 case 'n':
600 1 _kdl_buf_push_char(&buf, '\n');
601 1 break;
602 0 case 'r':
603 0 _kdl_buf_push_char(&buf, '\r');
604 0 break;
605 0 case 't':
606 0 _kdl_buf_push_char(&buf, '\t');
607 0 break;
608 0 case 's':
609 0 _kdl_buf_push_char(&buf, ' ');
610 0 break;
611 0 case '\\':
612 0 _kdl_buf_push_char(&buf, '\\');
613 0 break;
614 0 case '"':
615 0 _kdl_buf_push_char(&buf, '\"');
616 0 break;
617 0 case 'b':
618 0 _kdl_buf_push_char(&buf, '\b');
619 0 break;
620 0 case 'f':
621 0 _kdl_buf_push_char(&buf, '\f');
622 0 break;
623 2 case 'u': {
624 // u should be followed by {
625 2 ▶ if (_kdl_pop_codepoint(&escaped, &c) != KDL_UTF8_OK || c != '{') goto unesc_error;
Branch: 2 branches
BranchTrueFalseTotal
if (_kdl_pop_codepoint(&escaped, &c) != KDL_UTF8_OK || c != '{') goto unesc_error; 2 0 2
if (_kdl_pop_codepoint(&escaped, &c) != KDL_UTF8_OK || c != '{') goto unesc_error; 0 2 2
626  
627 2 uint32_t r = 0;
628 10 ▶ while ((status = _kdl_pop_codepoint(&escaped, &c)) == KDL_UTF8_OK) {
Branch: 1 branch
BranchTrueFalseTotal
while ((status = _kdl_pop_codepoint(&escaped, &c)) == KDL_UTF8_OK) { 10 0 10
629 // parse hex
630 10 ▶ if (c == '}') break;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '}') break; 2 8 10
631 8 ▶ else if (c >= '0' && c <= '9') r = (r << 4) + (c - '0');
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= '0' && c <= '9') r = (r << 4) + (c - '0'); 8 0 8
else if (c >= '0' && c <= '9') r = (r << 4) + (c - '0'); 6 2 8
632 2 ▶ else if (c >= 'a' && c <= 'f') r = (r << 4) + (c - 'a' + 0xa);
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'a' && c <= 'f') r = (r << 4) + (c - 'a' + 0xa); 0 2 2
else if (c >= 'a' && c <= 'f') r = (r << 4) + (c - 'a' + 0xa); 0 0 0
633 2 ▶ else if (c >= 'A' && c <= 'F') r = (r << 4) + (c - 'A' + 0xa);
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'A' && c <= 'F') r = (r << 4) + (c - 'A' + 0xa); 2 0 2
else if (c >= 'A' && c <= 'F') r = (r << 4) + (c - 'A' + 0xa); 2 0 2
634 0 else goto unesc_error;
635 }
636 2 ▶ if (status != KDL_UTF8_OK) goto unesc_error;
Branch: 1 branch
BranchTrueFalseTotal
if (status != KDL_UTF8_OK) goto unesc_error; 0 2 2
637 2 ▶ if ((0xD800 <= r && r <= 0xDFFF) // only Unicode Scalar values are allowed in strings
Branch: 2 branches
BranchTrueFalseTotal
if ((0xD800 <= r && r <= 0xDFFF) // only Unicode Scalar values are allowed in strings 2 0 2
if ((0xD800 <= r && r <= 0xDFFF) // only Unicode Scalar values are allowed in strings 0 2 2
638 0 ▶ || !_kdl_buf_push_codepoint(&buf, r))
Branch: 1 branch
BranchTrueFalseTotal
|| !_kdl_buf_push_codepoint(&buf, r)) 0 0 0
639 2 goto unesc_error;
640 0 break;
641 }
642 0 default:
643 // invalid escape
644 0 goto unesc_error;
645 }
646 } else {
647 // Nothing special, copy the character
648 501 _kdl_buf_push_codepoint(&buf, c);
649 }
650 }
651  
652 163 ▶ if (status == KDL_UTF8_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == KDL_UTF8_EOF) { 163 0 163
653 // ok
654 163 result = _kdl_buf_to_string(&buf);
655 163 return result;
656 } else {
657 0 goto unesc_error;
658 }
659  
660 2 unesc_error:
661 2 _kdl_free_write_buffer(&buf);
662 2 result = (kdl_owned_string){NULL, 0};
663 2 return result;
664 }
665  
666 165 bool _kdl_str_contains_newline(kdl_str const* s)
667 {
668 165 kdl_str string = *s;
669 kdl_utf8_status status;
670 uint32_t c;
671  
672 684 ▶ while ((status = _kdl_pop_codepoint(&string, &c)) == KDL_UTF8_OK) {
Branch: 1 branch
BranchTrueFalseTotal
while ((status = _kdl_pop_codepoint(&string, &c)) == KDL_UTF8_OK) { 519 165 684
673 519 ▶ if (_kdl_is_newline(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_kdl_is_newline(c)) { 0 519 519
674 0 return true;
675 }
676 }
677 165 return false;
678 }
« blib/lib/Text/KDL/XS.pm ↑ Summary ckdl/src/emitter.c »