asar coverage - build #292


src/asar/
File: src/asar/libstr.cpp
Date: 2025-03-10 15:35:02
Lines:
222/272
81.6%
Functions:
16/18
88.9%
Branches:
254/393
64.6%

Line Branch Exec Source
1 #include "asar.h"
2 #include "virtualfile.h"
3 #include "unicode.h"
4 #include "libmisc.h"
5
6 #include "platform/file-helpers.h"
7
8 #define typed_malloc(type, count) (type*)malloc(sizeof(type)*(count))
9 #define typed_realloc(type, ptr, count) (type*)realloc(ptr, sizeof(type)*(count))
10
11
12 // This function is intentionally left out of the header file so that it is not visible to the
13 // compiler and less likely to be inlined into resize(). This in turn reduces the size of resize()
14 // so that it is then inlined in more places.
15 3303 void string::reallocate_capacity(unsigned int new_length)
16 {
17 // Allocate 1 extra byte for NUL terminator
18 3303 int new_alloc_capacity = bitround(new_length + 1);
19
20
3/3
✓ Branch 0 taken 1431 times.
✓ Branch 1 taken 1762 times.
✓ Branch 2 taken 110 times.
3303 if (is_inlined()) {
21 3119 data_ptr = copy(data_ptr, min(len, new_length), (char*)malloc(new_alloc_capacity));
22 }
23 else {
24 184 data_ptr = (char*)realloc(data_ptr, new_alloc_capacity);
25 }
26 3303 allocated.capacity = new_alloc_capacity - 1; // capacity field doesn't count NUL terminator
27 3303 }
28
29
30 // Detects if str starts with a UTF-8 byte order mark.
31 // If so, throws a warning, then returns the number of bytes we should skip ahead in the string.
32 329 static size_t check_bom(const char* str)
33 {
34 // RPG Hacker: We could also check for BoMs of incompatible encodings here (like UTF-16)
35 // and throw errors, but not sure if that's worth adding. Asar never supported any wide
36 // encodings to begin with, so it's unreasonable to assume that any UTF-16 patches currently
37 // exist for it. As for future patches, those should be caught by the "must be UTF-8" checks
38 // I have already implemented further below.
39 // I think UTF-8 + BoM is the only case that could lead to confusion if we didn't handle it,
40 // so that's why I have added this.
41
6/9
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
329 if (str[0u] == '\xEF' && str[1u] == '\xBB' && str[2u] == '\xBF')
42 {
43 2 throw_warning(0, warn_byte_order_mark_utf8);
44 2 return 3u;
45 }
46
47 327 return 0u;
48 }
49
50
51 335 char * readfile(const char * fname, const char * basepath)
52 {
53 335 virtual_file_handle myfile = filesystem->open_file(fname, basepath);
54
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 329 times.
335 if (myfile == INVALID_VIRTUAL_FILE_HANDLE) return nullptr;
55 329 size_t datalen = filesystem->get_file_size(myfile);
56 329 char * data= typed_malloc(char, datalen+1);
57 329 data[filesystem->read_file(myfile, data, 0u, datalen)] = 0;
58 329 filesystem->close_file(myfile);
59
60
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 327 times.
329 if (!is_valid_utf8(data, datalen))
61 {
62 2 free(data);
63 2 throw_err_block(0, err_invalid_utf8);
64 }
65
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 325 times.
327 if(check_bom(data)){
66 2 data[0] = ' ';
67 2 data[1] = ' ';
68 2 data[2] = ' ';
69 }
70 327 return data;
71 }
72
73 // RPG Hacker: like readfile(), but doesn't use virtual file system
74 // and instead read our file directly.
75 8 char * readfilenative(const char * fname)
76 {
77 8 FileHandleType myfile = open_file(fname, FileOpenMode_Read);
78
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 if (myfile == InvalidFileHandle) return nullptr;
79 2 size_t datalen = (size_t)get_file_size(myfile);
80 2 char * data = typed_malloc(char, datalen + 1);
81 2 data[read_file(myfile, data, datalen)] = 0;
82 2 close_file(myfile);
83
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!is_valid_utf8(data, datalen)) throw_err_block(0, err_invalid_utf8);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if(check_bom(data)){
86 data[0] = ' ';
87 data[1] = ' ';
88 data[2] = ' ';
89 }
90 2 return data;
91 }
92
93 266 bool readfile(const char * fname, const char * basepath, char ** data, int * len)
94 {
95 266 virtual_file_handle myfile = filesystem->open_file(fname, basepath);
96
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 254 times.
266 if (!myfile) return false;
97 254 size_t datalen = filesystem->get_file_size(myfile);
98 254 *data= typed_malloc(char, datalen);
99 254 *len = (int)filesystem->read_file(myfile, *data, 0, datalen);
100 254 filesystem->close_file(myfile);
101 254 return true;
102 }
103
104 #define isq(n) (((0x2227 ^ (0x0101 * (n))) - 0x0101UL) & ~(0x2227 ^ (0x0101 * (n))) & 0x8080UL)
105 #define isqp(n) (((0x22272829 ^ (0x01010101 * (n))) - 0x01010101UL) & ~(0x22272829 ^ (0x01010101 * (n))) & 0x80808080UL)
106
107 // RPG Hacker: Only index this with ASCII characters.
108 // Anything else doesn't make sense, anyways.
109 const bool qparlut[128] = {
110 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0,
113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118 };
119
120 //this will leave the last char found as the one pointed at
121 22208 inline bool skip_quote(char *&str)
122 {
123
124
5/6
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 10206 times.
✓ Branch 2 taken 357 times.
✓ Branch 3 taken 11301 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 357 times.
22208 if(*str == '"') str = strchr(str + 1, '"');
125
4/4
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 10152 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 11247 times.
21507 else if(*str == '\'')
126 {
127 54 int codepoint;
128
2/4
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
108 str += utf8_val(&codepoint, str + 1) + 1;
129
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
108 if(*str != '\'') return false;
130 }
131 22208 return str;
132 }
133
134 //eat 1 char or quote/par
135 13420 inline bool skip_par(char *&str)
136 {
137 13420 int par = 0;
138
11/12
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6673 times.
✓ Branch 2 taken 95 times.
✓ Branch 3 taken 13319 times.
✓ Branch 4 taken 1068 times.
✓ Branch 5 taken 5516 times.
✓ Branch 6 taken 91 times.
✓ Branch 7 taken 12160 times.
✓ Branch 8 taken 1077 times.
✓ Branch 9 taken 5567 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 5567 times.
13420 if(*str != '\'' && *str != '"' && *str != '(' && *str != ')')
139 {
140 11083 str++;
141 11083 return true;
142 }
143 while(true)
144 {
145 13655 char *t = str;
146
5/6
✓ Branch 0 taken 191 times.
✓ Branch 1 taken 6617 times.
✓ Branch 2 taken 199 times.
✓ Branch 3 taken 6648 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 199 times.
13655 if(*str == '"') t = strchr(t + 1, '"');
147
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6611 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6642 times.
13265 else if(*str == '\'')
148 {
149 6 int codepoint;
150
2/3
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
12 t += utf8_val(&codepoint, t + 1) + 1;
151
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
12 if(*t != '\'') return false;
152 }
153
3/3
✓ Branch 0 taken 1076 times.
✓ Branch 1 taken 6620 times.
✓ Branch 2 taken 5557 times.
13253 else if(*t == '(')
154 {
155 2161 par++;
156 }
157
3/3
✓ Branch 0 taken 1076 times.
✓ Branch 1 taken 5544 times.
✓ Branch 2 taken 4472 times.
11092 else if(*t == ')')
158 {
159 2161 par--;
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2161 times.
2161 if(par < 0) return false;
161 }
162
163 13655 str = t + 1;
164
6/6
✓ Branch 0 taken 5986 times.
✓ Branch 1 taken 822 times.
✓ Branch 2 taken 6358 times.
✓ Branch 3 taken 6475 times.
✓ Branch 4 taken 344 times.
✓ Branch 5 taken 5673 times.
13655 if(!*str || !par) return par == 0 ? true : false;
165 11318 }
166 }
167
168 //instr should not be duplicate chars. Instr should also not be 1 char
169 string& string::qreplace(const char * instr, const char * outstr)
170 {
171 string& thisstring =*this;
172 if (!strstr(thisstring, instr)) return thisstring;
173 int inlen = strlen(instr);
174 string out;
175 for (int i=0;thisstring[i];)
176 {
177 if (!strncmp((const char*)thisstring +i, instr, inlen))
178 {
179 out+=outstr;
180 i+=inlen;
181 }
182 // randomdude999: prevent appending the null terminator to the output
183 else if(!isq(thisstring[i])) out+= thisstring[i++];
184 else
185 {
186 char *start = raw() + i;
187 char *end = start;
188 if(!skip_quote(end)) return thisstring;
189 out.append(raw(), i, end - start + i + 1);
190 i += end - start + 1;
191
192 }
193 }
194 thisstring =out;
195 return thisstring;
196 }
197
198 // * convert tabs to spaces
199 // * collapse multiple consecutive spaces into one
200 // * delete spaces after comma
201 // in total, this means that after qnormalize, splitting by spaces will split the input into "words".
202 56437 string& string::qnormalize()
203 {
204 56437 string& thisstring =*this;
205 56437 string out;
206 56437 const char *startstr = thisstring.data();
207 56437 const char *str = startstr;
208
4/4
✓ Branch 0 taken 35352 times.
✓ Branch 1 taken 92263 times.
✓ Branch 2 taken 35856 times.
✓ Branch 3 taken 28368 times.
127615 while((str = strpbrk(str, "'\" \t,\r")))
209 {
210
3/3
✓ Branch 0 taken 25522 times.
✓ Branch 1 taken 35769 times.
✓ Branch 2 taken 9917 times.
71208 if(is_space(*str))
211 {
212
10/10
✓ Branch 0 taken 25516 times.
✓ Branch 1 taken 25939 times.
✓ Branch 2 taken 25498 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 25492 times.
✓ Branch 5 taken 30 times.
✓ Branch 6 taken 25909 times.
✓ Branch 7 taken 24 times.
✓ Branch 8 taken 25909 times.
✓ Branch 9 taken 30 times.
51461 if(str[0] == ' ' && !is_space(str[1]))
213 {
214 51401 str++;
215 51401 continue;
216 }
217
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 out.append(startstr, 0, str - startstr);
218
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 out += ' ';
219
4/4
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 96 times.
✓ Branch 3 taken 30 times.
252 while(is_space(*str)) str++;
220 60 startstr = str;
221
3/3
✓ Branch 0 taken 3110 times.
✓ Branch 1 taken 9884 times.
✓ Branch 2 taken 6753 times.
19747 }else if(*str == ',')
222 {
223 6274 str++;
224
3/3
✓ Branch 0 taken 914 times.
✓ Branch 1 taken 3116 times.
✓ Branch 2 taken 2244 times.
6274 if(is_space(*str))
225 {
226
1/2
✓ Branch 0 taken 1834 times.
✗ Branch 1 not taken.
1834 out.append(startstr, 0, str - startstr);
227
4/4
✓ Branch 0 taken 935 times.
✓ Branch 1 taken 914 times.
✓ Branch 2 taken 941 times.
✓ Branch 3 taken 920 times.
3710 while(is_space(*str)) str++;
228 1834 startstr = str;
229 }
230 }
231 else
232 {
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753 times.
13473 str = strchr(str + 1, *str); //confirm quotes has already been run, so this should be okay
234
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 13443 times.
13473 if(!str) return thisstring;
235 13443 str++;
236 }
237 }
238
3/3
✓ Branch 0 taken 748 times.
✓ Branch 1 taken 28045 times.
✓ Branch 2 taken 27614 times.
56407 if(startstr != thisstring.data())
239 {
240
2/4
✓ Branch 0 taken 748 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 754 times.
✗ Branch 3 not taken.
1502 out.append(startstr, 0, (thisstring.data() + thisstring.length()) - startstr); //the remaining
241
242 1502 thisstring = std::move(out);
243 }
244 56407 return thisstring;
245 56437 }
246
247 77970 bool confirmquotes(const char * str)
248 {
249
3/3
✓ Branch 0 taken 27946 times.
✓ Branch 1 taken 46091 times.
✓ Branch 2 taken 17887 times.
91924 while(*str)
250 {
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28338 times.
56284 char *dquote = strchr((char *)str, '"');
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28338 times.
56284 char *squote = strchr((char *)str, '\'');
253
4/4
✓ Branch 0 taken 43248 times.
✓ Branch 1 taken 13036 times.
✓ Branch 2 taken 928 times.
✓ Branch 3 taken 42320 times.
56284 if(dquote || squote)
254 {
255
6/6
✓ Branch 0 taken 13036 times.
✓ Branch 1 taken 928 times.
✓ Branch 2 taken 12934 times.
✓ Branch 3 taken 102 times.
✓ Branch 4 taken 12918 times.
✓ Branch 5 taken 16 times.
13964 if(dquote && (dquote < squote || !squote))
256 {
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6540 times.
13020 dquote = strchr(dquote+1, '"');
258
2/2
✓ Branch 0 taken 13012 times.
✓ Branch 1 taken 8 times.
13020 if(dquote) str = dquote+1;
259 8 else return false;
260 }
261 else
262 {
263 472 int codepoint;
264
2/3
✓ Branch 0 taken 472 times.
✓ Branch 1 taken 472 times.
✗ Branch 2 not taken.
944 squote += utf8_val(&codepoint, squote + 1) + 1;
265
3/3
✓ Branch 0 taken 471 times.
✓ Branch 1 taken 472 times.
✓ Branch 2 taken 1 times.
944 if(*squote == '\'') str = squote+1;
266 2 else return false;
267 }
268 13954 }
269 else
270 {
271 42320 return true;
272 }
273 }
274 35640 return true;
275 }
276
277 724 bool confirmqpar(const char * str)
278 {
279 //todo fully optimize
280 724 int par = 0;
281
6/8
✗ Branch 0 not taken.
✓ Branch 1 taken 4810 times.
✓ Branch 2 taken 4455 times.
✓ Branch 3 taken 5193 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4838 times.
✓ Branch 6 taken 4469 times.
✓ Branch 7 taken 369 times.
9648 while((unsigned char)*str >= 128 || !qparlut[(unsigned char)*str]) str++;
282
3/3
✓ Branch 0 taken 905 times.
✓ Branch 1 taken 1288 times.
✓ Branch 2 taken 369 times.
2562 while(*str)
283 {
284
3/3
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 905 times.
✓ Branch 2 taken 750 times.
1838 if(*str == '"')
285 {
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
366 str = strchr(str + 1, '"');
287
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
✓ Branch 2 taken 183 times.
366 if(!str++) return false;
288 }
289
3/3
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 722 times.
✓ Branch 2 taken 738 times.
1472 else if(*str == '\'')
290 {
291 12 int codepoint;
292
2/3
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
24 str += utf8_val(&codepoint, str + 1) + 1;
293
2/3
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
24 if(*str == '\'') str++;
294 else return false;
295 }
296 else
297 {
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 738 times.
1448 par += 1 - ((*str++ - '(') << 1);
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1448 times.
1448 if(par < 0) return false;
300 }
301
6/8
✗ Branch 0 not taken.
✓ Branch 1 taken 2265 times.
✓ Branch 2 taken 1360 times.
✓ Branch 3 taken 3212 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2307 times.
✓ Branch 6 taken 1374 times.
✓ Branch 7 taken 933 times.
4572 while((unsigned char)*str >= 128 || !qparlut[(unsigned char)*str]) str++;
302 }
303 724 return !par;
304 }
305
306 12023 char ** split(char * str, char key, int * len)
307 {
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6080 times.
12023 char *thisentry=strchr(str, key);
309
2/2
✓ Branch 0 taken 9081 times.
✓ Branch 1 taken 2942 times.
12023 if (!thisentry)
310 {
311 9081 char ** out= typed_malloc(char*, 2);
312 9081 out[0]=str;
313 9081 out[1]=nullptr;
314
2/2
✓ Branch 0 taken 4493 times.
✓ Branch 1 taken 4588 times.
9081 if (len) *len=1;
315 9081 return out;
316 }
317 2942 int count=15; //makes the default alloc 8 elements, sounds fair.
318 2942 char ** outdata= typed_malloc(char*, (size_t)count+1);
319
320 2942 int newcount=0;
321 2942 outdata[newcount++]=str;
322 do{
323 14690 *thisentry = 0;
324 14690 thisentry++;
325 14690 outdata[newcount++]=thisentry;
326
2/2
✓ Branch 0 taken 307 times.
✓ Branch 1 taken 14383 times.
14690 if(newcount >= count)
327 {
328 307 count *= 2;
329 307 outdata = typed_realloc(char *, outdata, count);
330 }
331
4/4
✓ Branch 0 taken 5840 times.
✓ Branch 1 taken 8850 times.
✓ Branch 2 taken 5908 times.
✓ Branch 3 taken 1489 times.
14690 }while((thisentry = strchr(thisentry, key)));
332
333 2942 outdata[newcount]= nullptr;
334
2/2
✓ Branch 0 taken 2174 times.
✓ Branch 1 taken 768 times.
2942 if (len) *len=newcount;
335 2942 return outdata;
336 }
337
338 12021 char ** qsplit(char * str, char key, int * len)
339 {
340
8/10
✓ Branch 0 taken 5737 times.
✓ Branch 1 taken 6284 times.
✓ Branch 2 taken 11543 times.
✓ Branch 3 taken 256 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5833 times.
✓ Branch 6 taken 5806 times.
✓ Branch 7 taken 27 times.
✓ Branch 8 taken 5797 times.
✗ Branch 9 not taken.
12021 if (!strchr(str, '"') && !strchr(str, '\'')) return split(str, key, len);
341
342 505 int count=15;
343 505 char ** outdata= typed_malloc(char*, (size_t)count+1);
344 505 int newcount=0;
345 505 char * thisentry=str;
346 505 outdata[newcount++]=thisentry;
347
3/3
✓ Branch 0 taken 739 times.
✓ Branch 1 taken 995 times.
✓ Branch 2 taken 256 times.
1990 while (*thisentry) /*todo fix*/
348 {
349
3/3
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 739 times.
✓ Branch 2 taken 615 times.
1485 if (*thisentry == key)
350 {
351 262 *thisentry=0;
352 262 thisentry++;
353 262 outdata[newcount++]=thisentry;
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262 times.
262 if(newcount >= count)
355 {
356 count *= 2;
357 outdata = typed_realloc(char *, outdata, count);
358 }
359 }
360
2/4
✓ Branch 0 taken 1223 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1223 times.
✗ Branch 3 not taken.
1223 else if(skip_quote(thisentry)) thisentry++;
361 else return nullptr;
362 }
363 505 outdata[newcount]= nullptr;
364
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 355 times.
505 if (len) *len=newcount;
365 505 return outdata;
366 }
367
368 55471 char ** qsplitstr(char * str, const char * key, int * len)
369 {
370 //check if the str is found first
371
5/6
✓ Branch 0 taken 27368 times.
✓ Branch 1 taken 28103 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27884 times.
✓ Branch 4 taken 27605 times.
✓ Branch 5 taken 279 times.
55471 if (!strstr(str, key))
372 {
373 54973 char ** out= typed_malloc(char*, 2);
374 54973 out[0]=str;
375 54973 out[1]=nullptr;
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54973 times.
54973 if (len) *len=1;
377 54973 return out;
378 }
379
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
498 int keylen=(int)strlen(key);
381 498 int count=15;
382 498 char ** outdata= typed_malloc(char*, (size_t)count+1);
383 498 int newcount=0;
384 498 char * thisentry=str;
385 498 outdata[newcount++]=thisentry;
386
3/3
✓ Branch 0 taken 10842 times.
✓ Branch 1 taken 12231 times.
✓ Branch 2 taken 279 times.
23352 while (*thisentry) /*todo fix*/
387 {
388
5/6
✓ Branch 0 taken 900 times.
✓ Branch 1 taken 21954 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12012 times.
✓ Branch 4 taken 969 times.
✓ Branch 5 taken 11043 times.
22854 if (!strncmp(thisentry, key, (size_t)keylen))
389 {
390 1869 *thisentry=0;
391 1869 thisentry+=keylen;
392 1869 outdata[newcount++]=thisentry;
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1869 times.
1869 if(newcount >= count)
394 {
395 count *= 2;
396 outdata = typed_realloc(char *, outdata, count);
397 }
398 }
399
2/4
✓ Branch 0 taken 20985 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20985 times.
✗ Branch 3 not taken.
20985 else if(skip_quote(thisentry)) thisentry++;
400 else return nullptr;
401 }
402 498 outdata[newcount]= nullptr;
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 498 times.
498 if (len) *len=newcount;
404 498 return outdata;
405 }
406
407 //this function is most commonly called in cases where additional chars are very likely
408 12484 char ** qpsplit(char * str, char key, int * len)
409 {
410
8/10
✓ Branch 0 taken 5255 times.
✓ Branch 1 taken 7229 times.
✓ Branch 2 taken 10610 times.
✓ Branch 3 taken 938 times.
✓ Branch 4 taken 5255 times.
✓ Branch 5 taken 5355 times.
✓ Branch 6 taken 5355 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5355 times.
✗ Branch 9 not taken.
12484 if (!strchr(str, '(') && !strchr(str, ')')) return qsplit(str, key, len);
411 1874 int count=7;
412 1874 char ** outdata= typed_malloc(char*, (size_t)count+1);
413
414 1874 int newcount=0;
415 1874 char * thisentry=str;
416 1874 outdata[newcount++]=thisentry;
417
3/3
✓ Branch 0 taken 5417 times.
✓ Branch 1 taken 6370 times.
✓ Branch 2 taken 938 times.
12725 while (*thisentry)
418 {
419 //skippar(*thisentry, thisentry++, return nullptr;)
420
3/3
✓ Branch 0 taken 358 times.
✓ Branch 1 taken 5420 times.
✓ Branch 2 taken 5073 times.
10851 if (*thisentry == key)
421 {
422 719 *thisentry=0;
423 719 thisentry++;
424 719 outdata[newcount++]=thisentry;
425
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 717 times.
719 if(newcount >= count)
426 {
427 2 count *= 2;
428 2 outdata = typed_realloc(char *, outdata, count);
429 }
430 }
431
2/4
✓ Branch 0 taken 10132 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10132 times.
10132 else if(!skip_par(thisentry)) return nullptr;
432 }
433 1874 outdata[newcount]= nullptr;
434
2/2
✓ Branch 0 taken 906 times.
✓ Branch 1 taken 968 times.
1874 if (len) *len=newcount;
435 1874 return outdata;
436 }
437
438 string &itrim(string &input, const char * left, const char * right)
439 {
440 bool nukeright=true;
441 int totallen=input.length();
442 int rightlen=(int)strlen(right);
443 if (rightlen && rightlen<=totallen)
444 {
445 const char * rightend=right+rightlen;
446 const char * strend=input.data()+totallen;
447 while (right!=rightend)
448 {
449 rightend--;
450 strend--;
451 if (to_lower(*strend)!=to_lower(*rightend)) nukeright=false;
452 }
453 if (nukeright)
454 {
455 totallen-=rightlen;
456 input.truncate(totallen);
457 }
458 }
459 bool nukeleft=true;
460 int leftlen = strlen(left);
461 if(leftlen == 1 && input.data()[0] == left[0])
462 {
463 return input = string(input.data()+1, (input.length()-1));
464 }
465 else
466 {
467 for (int i = 0; i < leftlen; i++)
468 {
469 if (to_lower(input.data()[i])!=to_lower(left[i])) nukeleft=false;
470 }
471 if (nukeleft) input = string(input.data()+leftlen, (input.length()-leftlen));
472 }
473 return input;
474 }
475
476 336 char* strqpchr(char* str, char key)
477 {
478
3/3
✓ Branch 0 taken 1455 times.
✓ Branch 1 taken 1653 times.
✓ Branch 2 taken 156 times.
3264 while (*str)
479 {
480
3/3
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1455 times.
✓ Branch 2 taken 1488 times.
2958 if (*str == key) return str;
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2928 times.
2928 else if(!skip_par(str)) return nullptr;
482 }
483 306 return nullptr;
484 }
485
486 114 char* strqpstr(char* str, const char* key)
487 {
488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
114 size_t keylen = strlen(key);
489
3/3
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 237 times.
✓ Branch 2 taken 3 times.
474 while (*str)
490 {
491
5/6
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 414 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 234 times.
✓ Branch 4 taken 54 times.
✓ Branch 5 taken 180 times.
468 if (!strncmp(str, key, keylen)) return str;
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
360 else if(!skip_par(str)) return nullptr;
493 }
494 6 return nullptr;
495 }
496
497 extern const uint8_t char_props[256] = {
498 //x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF
499 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x80,0x00,0x00, // 0x
500 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 1x
501 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 2x !"#$%&'()*+,-./
502 0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x00,0x00,0x00,0x00,0x00,0x00, // 3x 0123456789:;<=>?
503 0x00,0x23,0x23,0x23,0x23,0x23,0x23,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22, // 4x @ABCDEFGHIJKLMNO
504 0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x00,0x00,0x00,0x00,0x08, // 5x PQRSTUVWXYZ[\]^_
505 0x00,0x25,0x25,0x25,0x25,0x25,0x25,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24, // 6x `abcdefghijklmno
506 0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x00,0x00,0x00,0x00,0x00, // 7x pqrstuvwxyz{|}~
507 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 8x
508 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 9x
509 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Ax
510 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Bx
511 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Cx
512 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Dx
513 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Ex
514 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // Fx
515 };
516