]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
c59f5181 | 3 | ** Copyright (C) 1994-2003 University of Virginia, |
616915dd | 4 | ** Massachusetts Institute of Technology |
5 | ** | |
6 | ** This program is free software; you can redistribute it and/or modify it | |
7 | ** under the terms of the GNU General Public License as published by the | |
8 | ** Free Software Foundation; either version 2 of the License, or (at your | |
9 | ** option) any later version. | |
10 | ** | |
11 | ** This program is distributed in the hope that it will be useful, but | |
12 | ** WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | ** General Public License for more details. | |
15 | ** | |
16 | ** The GNU General Public License is available from http://www.gnu.org/ or | |
17 | ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
18 | ** MA 02111-1307, USA. | |
19 | ** | |
155af98d | 20 | ** For information on splint: info@splint.org |
21 | ** To report a bug: splint-bug@splint.org | |
11db3170 | 22 | ** For more information: http://www.splint.org |
616915dd | 23 | */ |
24 | /* | |
25 | ** cstring.c | |
26 | */ | |
27 | ||
28 | /* | |
29 | * Herbert 06/12/2000 | |
30 | * - use drive spec specials with OS2 like with WIN32 | |
31 | * - cstring_replaceAll () needed in cpplib.c | |
32 | */ | |
33 | ||
1b8ae690 | 34 | # include "splintMacros.nf" |
616915dd | 35 | # include "basic.h" |
36 | # include "osd.h" | |
37 | # include "portab.h" | |
38 | ||
2934b455 | 39 | /*@only@*/ /*@notnull@*/ |
616915dd | 40 | cstring cstring_newEmpty (void) |
41 | { | |
42 | return (cstring_create (0)); | |
43 | } | |
44 | ||
45 | char cstring_firstChar (cstring s) | |
46 | { | |
47 | llassert (cstring_isDefined (s)); | |
48 | llassert (cstring_length (s) > 0); | |
49 | ||
50 | return (s[0]); | |
51 | } | |
52 | ||
abd7f895 | 53 | char cstring_getChar (cstring s, size_t n) |
616915dd | 54 | { |
abd7f895 | 55 | size_t length = cstring_length (s); |
616915dd | 56 | |
57 | llassert (cstring_isDefined (s)); | |
58 | llassert (n >= 1 && n <= length); | |
59 | ||
60 | return (s[n - 1]); | |
61 | } | |
62 | ||
abd7f895 | 63 | cstring cstring_suffix (cstring s, size_t n) |
616915dd | 64 | { |
65 | llassert (cstring_isDefined (s)); | |
66 | llassert (n <= cstring_length (s)); | |
67 | ||
68 | return (s + n); | |
69 | } | |
70 | ||
abd7f895 | 71 | cstring cstring_prefix (cstring s, size_t n) |
72 | /*@requires maxRead(s) >= n /\ maxSet(s) >= n @*/ | |
73 | /*@ensures maxRead(result) == n /\ maxSet(result) == n @*/ | |
616915dd | 74 | { |
75 | cstring t; | |
76 | char c; | |
77 | llassert (cstring_isDefined (s)); | |
78 | llassert (n <= cstring_length (s)); | |
79 | ||
80 | c = *(s + n); | |
81 | /*@-mods@*/ /* The modifications cancel out. */ | |
82 | *(s + n) = '\0'; | |
83 | t = cstring_copy (s); | |
84 | *(s + n) = c; | |
85 | /*@=mods@*/ | |
86 | ||
87 | return t; | |
88 | } | |
89 | ||
90 | /* effects If s = [0-9]*, returns s as an int. | |
91 | ** else returns -1. | |
92 | */ | |
93 | ||
94 | int cstring_toPosInt (cstring s) | |
95 | { | |
96 | int val = 0; | |
97 | ||
98 | cstring_chars (s, c) | |
99 | { | |
100 | if (isdigit ((unsigned char) c)) | |
101 | { | |
102 | val = (val * 10) + (int)(c - '0'); | |
103 | } | |
104 | else | |
105 | { | |
106 | return -1; | |
107 | } | |
108 | } end_cstring_chars ; | |
109 | ||
110 | return val; | |
111 | } | |
112 | ||
68de3f33 | 113 | cstring cstring_afterChar (cstring s, char c) |
114 | { | |
115 | llassert (cstring_isDefined (s)); | |
116 | return strchr (s, c); | |
117 | } | |
118 | ||
616915dd | 119 | cstring cstring_beforeChar (cstring s, char c) |
120 | { | |
121 | if (cstring_isDefined (s)) | |
122 | { | |
123 | char *cp = strchr (s, c); | |
124 | ||
125 | if (cp != NULL) | |
126 | { | |
127 | cstring ret; | |
128 | ||
129 | /*@-mods@*/ | |
130 | *cp = '\0'; | |
131 | ret = cstring_copy (s); | |
132 | *cp = c; | |
133 | /*@=mods@*/ /* modification is undone */ | |
134 | ||
135 | return ret; | |
136 | } | |
137 | } | |
138 | ||
139 | return cstring_undefined; | |
140 | } | |
141 | ||
abd7f895 | 142 | void cstring_setChar (cstring s, size_t n, char c) /*@requires maxRead(s) >= (n - 1) /\ maxSet(s) >= (n - 1) @*/ |
616915dd | 143 | { |
144 | llassert (cstring_isDefined (s)); | |
145 | llassert (n > 0 && n <= cstring_length (s)); | |
146 | ||
147 | s[n - 1] = c; | |
148 | } | |
149 | ||
150 | char cstring_lastChar (cstring s) | |
151 | { | |
abd7f895 | 152 | size_t length; |
616915dd | 153 | |
154 | llassert (cstring_isDefined (s)); | |
155 | ||
156 | length = cstring_length (s); | |
157 | llassert (length > 0); | |
158 | ||
159 | return (s[length - 1]); | |
160 | } | |
161 | ||
86d93ed3 | 162 | /*@only@*/ cstring cstring_copy (cstring s) /*@ensures maxSet(result) == maxRead(s) /\ maxRead(result) == maxRead(s) @*/ |
616915dd | 163 | { |
164 | if (cstring_isDefined (s)) | |
165 | { | |
166 | return (mstring_copy (s)); | |
167 | } | |
168 | else | |
169 | { | |
170 | return cstring_undefined; | |
171 | } | |
172 | } | |
173 | ||
abd7f895 | 174 | /*@only@*/ cstring cstring_copyLength (char *s, size_t len) /*@requires maxSet(s) >= (len - 1) @*/ |
616915dd | 175 | { |
176 | char *res = mstring_create (len + 1); | |
177 | ||
abd7f895 | 178 | strncpy (res, s, len); |
616915dd | 179 | res[len] = '\0'; |
180 | return res; | |
181 | } | |
182 | ||
183 | bool cstring_containsChar (cstring c, char ch) | |
184 | { | |
185 | if (cstring_isDefined (c)) | |
186 | { | |
187 | return (strchr (c, ch) != NULL); | |
188 | } | |
189 | else | |
190 | { | |
191 | return FALSE; | |
192 | } | |
193 | } | |
194 | ||
195 | /* | |
196 | ** Replaces all occurances of old in s with new. | |
197 | */ | |
198 | ||
616915dd | 199 | void cstring_replaceAll (cstring s, char old, char snew) |
200 | { | |
201 | ||
202 | llassert (old != snew); | |
203 | ||
204 | if (cstring_isDefined (s)) | |
205 | { | |
206 | char *sp = strchr (s, old); | |
207 | ||
208 | while (sp != NULL) | |
209 | { | |
210 | *sp = snew; | |
211 | sp = strchr (sp, old); | |
212 | } | |
213 | ||
214 | } | |
215 | } | |
616915dd | 216 | |
92b2ba2c | 217 | void cstring_replaceLit (/*@unique@*/ cstring s, char *old, char *snew) |
218 | /*@requires maxRead(snew) >= 0 /\ maxRead(old) >= 0 /\ maxRead(old) >= maxRead(snew) @*/ | |
616915dd | 219 | { |
616915dd | 220 | llassert (strlen (old) >= strlen (snew)); |
92b2ba2c | 221 | |
616915dd | 222 | if (cstring_isDefined (s)) |
223 | { | |
224 | char *sp = strstr (s, old); | |
225 | ||
226 | while (sp != NULL) | |
227 | { | |
228 | int lendiff = size_toInt (strlen (old) - strlen (snew)); | |
229 | char *tsnew = snew; | |
92b2ba2c | 230 | |
231 | llassert (lendiff >= 0); | |
232 | ||
616915dd | 233 | while (*tsnew != '\0') |
234 | { | |
92b2ba2c | 235 | llassert (*sp != '\0'); |
616915dd | 236 | *sp++ = *tsnew++; |
237 | } | |
92b2ba2c | 238 | |
616915dd | 239 | if (lendiff > 0) |
240 | { | |
241 | while (*(sp + lendiff) != '\0') | |
242 | { | |
243 | *sp = *(sp + lendiff); | |
244 | sp++; | |
245 | } | |
dcdc46c8 | 246 | |
616915dd | 247 | *sp = '\0'; |
248 | } | |
249 | ||
250 | sp = strstr (s, old); | |
251 | } | |
dcdc46c8 | 252 | } |
616915dd | 253 | } |
254 | ||
255 | /* | |
256 | ** removes all chars in clist from s | |
257 | */ | |
258 | ||
259 | void cstring_stripChars (cstring s, const char *clist) | |
260 | { | |
261 | if (cstring_isDefined (s)) | |
262 | { | |
263 | int i; | |
abd7f895 | 264 | size_t size = cstring_length (s); |
616915dd | 265 | |
abd7f895 | 266 | for (i = 0; i < size_toInt (size); i++) |
616915dd | 267 | { |
8fd556fb | 268 | |
abd7f895 | 269 | char c = s[i]; |
616915dd | 270 | |
271 | if (strchr (clist, c) != NULL) | |
272 | { | |
273 | /* strip this char */ | |
274 | int j; | |
275 | ||
276 | size--; | |
abd7f895 | 277 | |
278 | for (j = i; j < size_toInt (size); j++) | |
616915dd | 279 | { |
8fd556fb | 280 | s[j] = s[j+1]; |
616915dd | 281 | } |
abd7f895 | 282 | |
8fd556fb | 283 | s[size] = '\0'; |
616915dd | 284 | i--; |
285 | } | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
290 | bool cstring_contains (/*@unique@*/ cstring c, cstring sub) | |
291 | { | |
292 | if (cstring_isDefined (c)) | |
293 | { | |
294 | llassert (cstring_isDefined (sub)); | |
295 | ||
296 | return (strstr (c, sub) != NULL); | |
297 | } | |
298 | else | |
299 | { | |
300 | return FALSE; | |
301 | } | |
302 | } | |
303 | ||
304 | static char lookLike (char c) /*@*/ | |
305 | { | |
306 | if (c == 'I' || c == 'l') | |
307 | { | |
308 | return '1'; | |
309 | } | |
310 | else if (c == 'O' || c == 'o') | |
311 | { | |
312 | return '0'; | |
313 | } | |
314 | else if (c == 'Z') | |
315 | { | |
316 | return '2'; | |
317 | } | |
318 | else if (c == 'S') | |
319 | { | |
320 | return '5'; | |
321 | } | |
322 | else | |
323 | { | |
324 | return c; | |
325 | } | |
326 | } | |
327 | ||
328 | cmpcode cstring_genericEqual (cstring s, cstring t, | |
abd7f895 | 329 | size_t nchars, |
616915dd | 330 | bool caseinsensitive, |
abd7f895 | 331 | bool lookalike) |
332 | /*@requires maxRead(s) >= nchars /\ maxRead(t) >= nchars @*/ | |
616915dd | 333 | { |
334 | if (s == t) return CGE_SAME; | |
335 | else if (cstring_isUndefined (s)) | |
336 | { | |
337 | return cstring_isEmpty (t) ? CGE_SAME : CGE_DISTINCT; | |
338 | } | |
339 | else if (cstring_isUndefined (t)) | |
340 | { | |
341 | return cstring_isEmpty (s) ? CGE_SAME : CGE_DISTINCT; | |
342 | } | |
343 | else | |
344 | { | |
345 | int i = 0; | |
346 | bool diffcase = FALSE; | |
347 | bool difflookalike = FALSE; | |
348 | ||
349 | while (*s != '\0') | |
350 | { | |
abd7f895 | 351 | if (nchars > 0 && i >= size_toInt (nchars)) |
616915dd | 352 | { |
353 | break; | |
354 | } | |
355 | ||
356 | if (*t == *s) | |
357 | { | |
358 | ; /* no difference */ | |
359 | } | |
360 | else if (caseinsensitive | |
361 | && (toupper ((int) *t) == toupper ((int) *s))) | |
362 | { | |
363 | diffcase = TRUE; | |
364 | } | |
365 | else if (lookalike && (lookLike (*t) == lookLike (*s))) | |
366 | { | |
367 | difflookalike = TRUE; | |
368 | } | |
369 | else | |
370 | { | |
371 | return CGE_DISTINCT; | |
372 | } | |
373 | i++; | |
374 | s++; | |
375 | t++; | |
376 | } | |
377 | ||
8fd556fb | 378 | |
616915dd | 379 | if (*s == '\0' && *t != '\0') |
380 | { | |
381 | return CGE_DISTINCT; | |
382 | } | |
383 | ||
384 | if (diffcase) | |
385 | { | |
386 | return CGE_CASE; | |
387 | } | |
388 | else if (difflookalike) | |
389 | { | |
390 | return CGE_LOOKALIKE; | |
391 | } | |
392 | else | |
393 | { | |
394 | return CGE_SAME; | |
395 | } | |
396 | } | |
397 | } | |
398 | ||
399 | ||
400 | ||
401 | bool cstring_equalFree (/*@only@*/ cstring c1, /*@only@*/ cstring c2) | |
402 | { | |
403 | bool res = cstring_equal (c1, c2); | |
404 | cstring_free (c1); | |
405 | cstring_free (c2); | |
406 | return res; | |
407 | } | |
408 | ||
409 | bool cstring_equal (cstring c1, cstring c2) | |
410 | { | |
411 | if (c1 == c2) return TRUE; | |
412 | else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2); | |
413 | else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1); | |
414 | else return (strcmp (c1, c2) == 0); | |
415 | } | |
416 | ||
abd7f895 | 417 | bool cstring_equalLen (cstring c1, cstring c2, size_t len) |
616915dd | 418 | { |
419 | if (c1 == c2) return TRUE; | |
420 | else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2); | |
421 | else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1); | |
abd7f895 | 422 | else return (strncmp (c1, c2, len) == 0); |
616915dd | 423 | } |
424 | ||
425 | bool cstring_equalCaseInsensitive (cstring c1, cstring c2) | |
426 | { | |
427 | if (c1 == c2) return TRUE; | |
428 | else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2); | |
429 | else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1); | |
430 | else return (cstring_genericEqual (c1, c2, 0, TRUE, FALSE) != CGE_DISTINCT); | |
431 | } | |
432 | ||
abd7f895 | 433 | bool cstring_equalLenCaseInsensitive (cstring c1, cstring c2, size_t len) |
616915dd | 434 | { |
616915dd | 435 | if (c1 == c2) return TRUE; |
436 | else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2); | |
437 | else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1); | |
438 | else return (cstring_genericEqual (c1, c2, len, TRUE, FALSE) != CGE_DISTINCT); | |
439 | } | |
440 | ||
68de3f33 | 441 | bool cstring_equalPrefix (cstring c1, cstring c2) |
616915dd | 442 | { |
443 | llassert (c2 != NULL); | |
444 | ||
445 | if (cstring_isUndefined (c1)) | |
446 | { | |
447 | return (strlen (c2) == 0); | |
448 | } | |
449 | ||
450 | return (strncmp (c1, c2, strlen (c2)) == 0); | |
451 | } | |
452 | ||
68de3f33 | 453 | bool cstring_equalPrefixLit (cstring c1, const char *c2) |
616915dd | 454 | { |
455 | llassert (c2 != NULL); | |
456 | ||
457 | if (cstring_isUndefined (c1)) | |
458 | { | |
459 | return (strlen (c2) == 0); | |
460 | } | |
461 | ||
616915dd | 462 | return (strncmp (c1, c2, strlen (c2)) == 0); |
616915dd | 463 | } |
464 | ||
465 | int cstring_xcompare (cstring *c1, cstring *c2) | |
466 | { | |
467 | return (cstring_compare (*c1, *c2)); | |
468 | } | |
469 | ||
470 | int cstring_compare (cstring c1, cstring c2) | |
471 | { | |
472 | int res; | |
473 | ||
474 | if (c1 == c2) | |
475 | { | |
476 | res = 0; | |
477 | } | |
478 | else if (cstring_isUndefined (c1)) | |
479 | { | |
480 | if (cstring_isEmpty (c2)) | |
481 | { | |
482 | res = 0; | |
483 | } | |
484 | else | |
485 | { | |
486 | res = 1; | |
487 | } | |
488 | } | |
489 | else if (cstring_isUndefined (c2)) | |
490 | { | |
491 | if (cstring_isEmpty (c1)) | |
492 | { | |
493 | res = 0; | |
494 | } | |
495 | else | |
496 | { | |
497 | res = -1; | |
498 | } | |
499 | } | |
500 | else | |
501 | { | |
502 | res = strcmp (c1, c2); | |
503 | } | |
504 | ||
505 | return (res); | |
506 | } | |
507 | ||
508 | void cstring_markOwned (/*@owned@*/ cstring s) | |
509 | { | |
510 | sfreeEventually (s); | |
511 | } | |
512 | ||
513 | void cstring_free (/*@only@*/ cstring s) | |
514 | { | |
515 | if (cstring_isDefined (s)) | |
516 | { | |
c21b4c87 | 517 | /*drl 2/3/2002*/ |
518 | s[0] = '\0'; | |
519 | ||
616915dd | 520 | sfree (s); |
521 | } | |
522 | } | |
523 | ||
524 | cstring cstring_fromChars (/*@exposed@*/ const char *cp) | |
525 | { | |
526 | return (cstring) cp; | |
527 | } | |
528 | ||
529 | /*@exposed@*/ char *cstring_toCharsSafe (cstring s) | |
530 | { | |
531 | static /*@only@*/ cstring emptystring = cstring_undefined; | |
532 | ||
533 | if (cstring_isDefined (s)) | |
534 | { | |
535 | return (char *) s; | |
536 | } | |
537 | else | |
538 | { | |
539 | if (cstring_isUndefined (emptystring)) | |
540 | { | |
541 | emptystring = cstring_newEmpty (); | |
542 | } | |
543 | ||
544 | return emptystring; | |
545 | } | |
546 | } | |
547 | ||
abd7f895 | 548 | size_t cstring_length (cstring s) |
616915dd | 549 | { |
550 | if (cstring_isDefined (s)) | |
551 | { | |
abd7f895 | 552 | return strlen (s); |
616915dd | 553 | } |
abd7f895 | 554 | |
616915dd | 555 | return 0; |
556 | } | |
557 | ||
558 | cstring | |
86d93ed3 | 559 | cstring_capitalize (cstring s) /*@requires maxSet(s) >= 0 @*/ |
616915dd | 560 | { |
561 | if (!cstring_isEmpty (s)) | |
562 | { | |
563 | cstring ret = cstring_copy (s); | |
564 | ||
565 | cstring_setChar (ret, 1, (char) toupper ((int) cstring_firstChar (ret))); | |
566 | return ret; | |
567 | } | |
568 | ||
569 | return cstring_undefined; | |
570 | } | |
571 | ||
572 | cstring | |
86d93ed3 | 573 | cstring_capitalizeFree (cstring s) /*@requires maxSet(s) >= 0 /\ maxRead(s) >= 0 @*/ |
616915dd | 574 | { |
575 | if (!cstring_isEmpty (s)) | |
576 | { | |
577 | cstring_setChar (s, 1, (char) toupper ((int) cstring_firstChar (s))); | |
578 | return s; | |
579 | } | |
580 | ||
581 | return s; | |
582 | } | |
583 | ||
584 | cstring | |
abd7f895 | 585 | cstring_clip (cstring s, size_t len) |
616915dd | 586 | { |
587 | if (cstring_isUndefined (s) || cstring_length (s) <= len) | |
588 | { | |
589 | ; | |
590 | } | |
591 | else | |
592 | { | |
593 | llassert (s != NULL); | |
8fd556fb | 594 | |
abd7f895 | 595 | *(s + len) = '\0'; |
616915dd | 596 | } |
abd7f895 | 597 | |
616915dd | 598 | return s; |
599 | } | |
600 | ||
601 | /*@only@*/ cstring | |
abd7f895 | 602 | cstring_elide (cstring s, size_t len) |
616915dd | 603 | { |
604 | if (cstring_isUndefined (s) || cstring_length (s) <= len) | |
605 | { | |
606 | return cstring_copy (s); | |
607 | } | |
608 | else | |
609 | { | |
610 | cstring sc = cstring_create (len); | |
abd7f895 | 611 | |
612 | strncpy (sc, s, len); | |
8fd556fb | 613 | |
abd7f895 | 614 | *(sc + len - 1) = '\0'; |
616915dd | 615 | *(sc + len - 2) = '.'; |
616 | *(sc + len - 3) = '.'; | |
617 | *(sc + len - 4) = '.'; | |
abd7f895 | 618 | |
616915dd | 619 | return sc; |
620 | } | |
621 | } | |
622 | ||
623 | /*@only@*/ cstring | |
abd7f895 | 624 | cstring_fill (cstring s, size_t n) /*@requires n >= 0 @*/ |
616915dd | 625 | { |
626 | cstring t = cstring_create (n + 1); | |
627 | cstring ot = t; | |
abd7f895 | 628 | size_t len = cstring_length (s); |
629 | size_t i; | |
616915dd | 630 | |
631 | if (len > n) | |
632 | { | |
633 | for (i = 0; i < n; i++) | |
634 | { | |
8fd556fb | 635 | |
616915dd | 636 | *t++ = *s++; |
637 | } | |
638 | *t = '\0'; | |
639 | } | |
640 | else | |
641 | { | |
642 | for (i = 0; i < len; i++) | |
643 | { | |
8fd556fb | 644 | |
616915dd | 645 | *t++ = *s++; |
646 | } | |
647 | for (i = 0; i < n - len; i++) | |
648 | { | |
8fd556fb | 649 | |
616915dd | 650 | *t++ = ' '; |
651 | } | |
652 | *t = '\0'; | |
653 | } | |
654 | ||
655 | return ot; | |
656 | } | |
657 | ||
658 | cstring | |
659 | cstring_downcase (cstring s) | |
660 | { | |
661 | if (cstring_isDefined (s)) | |
662 | { | |
663 | cstring t = cstring_create (strlen (s) + 1); | |
664 | cstring ot = t; | |
665 | char c; | |
666 | ||
8fd556fb | 667 | while ((c = *s) != '\0') |
616915dd | 668 | { |
669 | if (c >= 'A' && c <= 'Z') | |
670 | { | |
671 | c = c - 'A' + 'a'; | |
672 | } | |
673 | *t++ = c; | |
674 | s++; | |
675 | } | |
8fd556fb | 676 | *t = '\0'; |
616915dd | 677 | |
678 | return ot; | |
679 | } | |
680 | else | |
681 | { | |
682 | return cstring_undefined; | |
683 | } | |
684 | } | |
685 | ||
686 | /*@notnull@*/ cstring | |
687 | cstring_appendChar (/*@only@*/ cstring s1, char c) | |
688 | { | |
abd7f895 | 689 | size_t l = cstring_length (s1); |
616915dd | 690 | char *s; |
691 | ||
692 | s = (char *) dmalloc (sizeof (*s) * (l + 2)); | |
693 | ||
694 | if (cstring_isDefined (s1)) | |
695 | { | |
696 | strcpy (s, s1); | |
697 | *(s + l) = c; | |
8fd556fb | 698 | *(s + l + 1) = '\0'; |
616915dd | 699 | sfree (s1); |
700 | } | |
701 | else | |
702 | { | |
703 | *(s) = c; | |
8fd556fb | 704 | *(s + 1) = '\0'; |
616915dd | 705 | } |
706 | ||
707 | return s; | |
708 | } | |
709 | ||
710 | /*@only@*/ cstring | |
711 | cstring_concatFree (cstring s, cstring t) | |
712 | { | |
713 | cstring res = cstring_concat (s, t); | |
714 | cstring_free (s); | |
715 | cstring_free (t); | |
716 | return res; | |
717 | } | |
718 | ||
719 | /*@only@*/ cstring | |
720 | cstring_concatFree1 (cstring s, cstring t) | |
721 | { | |
722 | cstring res = cstring_concat (s, t); | |
723 | cstring_free (s); | |
724 | return res; | |
725 | } | |
726 | ||
616915dd | 727 | /*@only@*/ cstring |
728 | cstring_concatChars (cstring s, char *t) | |
729 | { | |
730 | cstring res = cstring_concat (s, cstring_fromChars (t)); | |
731 | cstring_free (s); | |
732 | return res; | |
733 | } | |
616915dd | 734 | |
735 | /*@only@*/ cstring | |
abd7f895 | 736 | cstring_concatLength (cstring s1, char *s2, size_t len) /*@requires maxSet(s2) >= (len - 1) @*/ |
616915dd | 737 | { |
738 | cstring tmp = cstring_copyLength (s2, len); | |
739 | cstring res = cstring_concat (s1, tmp); | |
740 | cstring_free (tmp); | |
741 | cstring_free (s1); | |
742 | ||
743 | return res; | |
744 | } | |
745 | ||
746 | /*@only@*/ cstring | |
86d93ed3 | 747 | cstring_concat (cstring s, cstring t) /*@requires maxSet(s) >= 0 @*/ |
616915dd | 748 | { |
749 | char *ret = mstring_create (cstring_length (s) + cstring_length (t)); | |
750 | ||
751 | if (cstring_isDefined (s)) | |
752 | { | |
8fd556fb | 753 | strcpy (ret, s); |
616915dd | 754 | } |
755 | if (cstring_isDefined (t)) | |
756 | { | |
757 | strcat (ret, t); | |
758 | } | |
759 | ||
760 | return ret; | |
761 | } | |
762 | ||
763 | /*@notnull@*/ /*@only@*/ cstring | |
764 | cstring_prependCharO (char c, /*@only@*/ cstring s1) | |
765 | { | |
766 | cstring res = cstring_prependChar (c, s1); | |
767 | ||
768 | cstring_free (s1); | |
769 | return (res); | |
770 | } | |
771 | ||
772 | /*@notnull@*/ /*@only@*/ cstring | |
773 | cstring_prependChar (char c, /*@temp@*/ cstring s1) | |
774 | { | |
abd7f895 | 775 | size_t l = cstring_length (s1); |
616915dd | 776 | char *s = (char *) dmalloc (sizeof (*s) * (l + 2)); |
777 | ||
8fd556fb | 778 | *(s) = c; |
616915dd | 779 | |
780 | if (cstring_isDefined (s1)) | |
781 | { | |
782 | /*@-mayaliasunique@*/ | |
783 | strcpy (s + 1, s1); | |
784 | /*@=mayaliasunique@*/ | |
785 | } | |
786 | ||
8fd556fb | 787 | *(s + l + 1) = '\0'; |
616915dd | 788 | return s; |
789 | } | |
790 | ||
616915dd | 791 | bool |
792 | cstring_hasNonAlphaNumBar (cstring s) | |
793 | { | |
794 | int c; | |
795 | ||
796 | if (cstring_isUndefined (s)) return FALSE; | |
797 | ||
8fd556fb | 798 | while ((c = (int) *s) != (int) '\0') |
616915dd | 799 | { |
800 | if ((isalnum (c) == 0) && (c != (int) '_') | |
801 | && (c != (int) '.') && (c != (int) CONNECTCHAR)) | |
802 | { | |
803 | return TRUE; | |
804 | } | |
805 | ||
806 | s++; | |
807 | } | |
808 | return FALSE; | |
809 | } | |
616915dd | 810 | |
811 | /*@only@*/ /*@notnull@*/ cstring | |
abd7f895 | 812 | cstring_create (size_t n) |
616915dd | 813 | { |
814 | char *s = dmalloc (sizeof (*s) * (n + 1)); | |
abd7f895 | 815 | |
8fd556fb | 816 | *s = '\0'; |
616915dd | 817 | return s; |
818 | } | |
819 | ||
2934b455 | 820 | /*@only@*/ /*@notnull@*/ cstring |
abd7f895 | 821 | cstring_copySegment (cstring s, size_t findex, size_t tindex) |
2934b455 | 822 | { |
823 | cstring res = cstring_create (tindex - findex + 1); | |
824 | ||
825 | llassert (cstring_isDefined (s)); | |
826 | llassert (cstring_length (s) > tindex); | |
827 | ||
e5081f8c | 828 | strncpy (res, (s + findex), size_fromInt (size_toInt (tindex - findex) + 1)); |
2934b455 | 829 | return res; |
830 | } | |
831 | ||
616915dd | 832 | lsymbol cstring_toSymbol (cstring s) |
833 | { | |
2934b455 | 834 | lsymbol res = lsymbol_fromString (s); |
616915dd | 835 | |
836 | cstring_free (s); | |
837 | return res; | |
838 | } | |
616915dd | 839 | |
840 | cstring cstring_bsearch (cstring key, char **table, int nentries) | |
841 | { | |
842 | if (cstring_isDefined (key)) | |
843 | { | |
844 | int low = 0; | |
845 | int high = nentries; | |
846 | int mid = (high + low + 1) / 2; | |
847 | int last = -1; | |
848 | cstring res = cstring_undefined; | |
849 | ||
850 | while (low <= high && mid < nentries) | |
851 | { | |
852 | int cmp; | |
853 | ||
854 | llassert (mid != last); | |
855 | llassert (mid >= 0 && mid < nentries); | |
856 | ||
857 | cmp = cstring_compare (key, table[mid]); | |
858 | ||
859 | if (cmp == 0) | |
860 | { | |
861 | res = table[mid]; | |
862 | break; | |
863 | } | |
864 | else if (cmp < 0) /* key is before table[mid] */ | |
865 | { | |
866 | high = mid - 1; | |
867 | } | |
868 | else /* key of after table[mid] */ | |
869 | { | |
870 | low = mid + 1; | |
871 | } | |
872 | ||
873 | last = mid; | |
874 | mid = (high + low + 1) / 2; | |
875 | } | |
876 | ||
877 | if (mid != 0 && mid < nentries - 1) | |
878 | { | |
879 | llassert (cstring_compare (key, table[mid - 1]) > 0); | |
8fd556fb | 880 | llassert (cstring_compare (key, table[mid + 1]) < 0); |
616915dd | 881 | } |
882 | ||
883 | return res; | |
884 | } | |
885 | ||
886 | return cstring_undefined; | |
887 | } | |
888 | ||
889 | extern /*@observer@*/ cstring cstring_advanceWhiteSpace (cstring s) | |
890 | { | |
891 | if (cstring_isDefined (s)) { | |
892 | char *t = s; | |
893 | ||
8fd556fb | 894 | while (*t != '\0' && isspace ((int) *t)) { |
616915dd | 895 | t++; |
896 | } | |
897 | ||
898 | return t; | |
899 | } | |
900 | ||
901 | return cstring_undefined; | |
902 | } | |
1810fe2a | 903 | |
46edc85c | 904 | /* changes strings like "sdf" "sdfsd" into "sdfsdfsd"*/ |
905 | /* This function understands that "sdf\" \"sdfsdf" is okay*/ | |
906 | static mstring doMergeString (cstring s) | |
907 | { | |
908 | char *ptr; | |
909 | mstring ret; | |
910 | char * retPtr; | |
911 | bool escape; | |
912 | ||
913 | llassert(cstring_isDefined (s)); | |
914 | ||
915 | ret = mstring_create (cstring_length(s) ); | |
916 | ||
917 | ptr = s; | |
918 | ||
919 | retPtr = ret; | |
920 | /* | |
921 | llassert(*ptr == '\"'); | |
922 | ||
923 | *retPtr = *ptr; | |
924 | ||
925 | retPtr++; | |
926 | ptr++; | |
927 | */ | |
928 | ||
929 | while (*ptr != '\0') | |
930 | { | |
931 | escape = FALSE; | |
932 | ||
933 | if (*ptr == '\\') | |
934 | { | |
935 | *retPtr = *ptr; | |
936 | ||
937 | if (!escape) | |
938 | escape = TRUE; | |
939 | else | |
940 | /* case of escaped \ ('\\') */ | |
941 | escape = FALSE; | |
942 | } | |
943 | else if ( (*ptr == '\"') && (!escape) ) | |
944 | { | |
945 | while ( (ptr[1] != '\"') && (ptr[1] != '\0') ) | |
946 | { | |
947 | ptr++; | |
948 | } | |
949 | if (ptr[1] == '\0') | |
950 | { | |
951 | llassert(*ptr == '\"'); | |
952 | *retPtr = '\"'; | |
953 | retPtr++; | |
954 | *retPtr = '\0'; | |
955 | BADEXIT; | |
846c7865 | 956 | |
957 | /*@notreached@*/ return ret; | |
46edc85c | 958 | } |
959 | else | |
960 | { | |
961 | ptr++; | |
962 | } | |
963 | } | |
964 | else | |
965 | { | |
966 | *retPtr = *ptr; | |
967 | } | |
968 | ||
969 | retPtr++; | |
970 | ptr++; | |
971 | ||
972 | }/* end while */ | |
846c7865 | 973 | *retPtr = '\0'; |
46edc85c | 974 | return ret; |
975 | } | |
976 | ||
37ae0b5e | 977 | static mstring doExpandEscapes (cstring s, /*@out@*/ size_t *len) |
1810fe2a | 978 | { |
979 | char *ptr; | |
980 | mstring ret; | |
981 | char * retPtr; | |
1810fe2a | 982 | |
983 | llassert(cstring_isDefined (s)); | |
984 | ||
abd7f895 | 985 | ret = mstring_create (cstring_length(s)); |
1810fe2a | 986 | |
987 | ptr = s; | |
988 | ||
989 | retPtr = ret; | |
990 | while (*ptr != '\0') | |
991 | { | |
992 | if (*ptr != '\\') | |
993 | { | |
994 | *retPtr = *ptr; | |
995 | retPtr++; | |
996 | ptr++; | |
997 | continue; | |
998 | } | |
999 | ||
1000 | if (*ptr == '\\') | |
1001 | { | |
1002 | ptr++; | |
1003 | if (*ptr == '\0') | |
1004 | { | |
1005 | /*not a legal escape sequence but try to handle it in a sesible way*/ | |
1006 | *retPtr = '\\'; | |
1007 | retPtr++; | |
1008 | } | |
1009 | ||
0e5499ac | 1010 | /* Handle Octal escapes */ |
1810fe2a | 1011 | else if (*ptr >= '0' && *ptr <= '9' ) |
1012 | { | |
1013 | int total; | |
1014 | total = (int)(*ptr - '0'); | |
1015 | ptr++; | |
1016 | /*octal can only be 3 characters long */ | |
1017 | if (*ptr != '\0' && (*ptr >= '0' && *ptr <= '9' ) ) | |
1018 | { | |
1019 | total *= 8; | |
1020 | ptr++; | |
1021 | if (*ptr != '\0' && (*ptr >= '0' && *ptr <= '9' ) ) | |
1022 | { | |
1023 | total *= 8; | |
1024 | total += (int) (*ptr - '0'); | |
1025 | ptr++; | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | *retPtr = (char) total; | |
1030 | retPtr++; | |
1031 | } | |
1032 | ||
1033 | else if (*ptr == 'x') | |
1034 | { | |
1035 | int total; | |
1036 | total = 0; | |
1037 | ptr++; | |
1038 | if (!(*ptr != '\0' && | |
1039 | ( (*ptr >= '0' && *ptr <= '9' ) || | |
1040 | (toupper(*ptr) >= (int)('A') && toupper(*ptr) <= (int)('F') ) ) | |
1041 | )) | |
1042 | { | |
1043 | total = (int)'x'; | |
1044 | } | |
1045 | else | |
1046 | { | |
1047 | while (*ptr != '\0' && | |
1048 | ( (*ptr >= '0' && *ptr <= '9' ) || | |
1049 | (toupper(*ptr) >= ((int)('A')) && toupper(*ptr) <= ((int)'F') ) ) | |
1050 | ) | |
1051 | { | |
1052 | total *= 16; | |
1053 | if (*ptr >= '0' && *ptr <= '9' ) | |
1054 | total += (int)(*ptr - '0'); | |
1055 | else | |
1056 | total += ( (toupper(*ptr) - 'A') + 10); | |
1057 | ptr++; | |
1058 | } | |
1059 | } | |
1060 | *retPtr = (char) total; | |
1061 | retPtr++; | |
1062 | } | |
1063 | else | |
1064 | { | |
1065 | switch ( *ptr ) | |
1066 | { | |
1067 | case 'a': | |
1068 | *retPtr = '\a'; | |
1069 | retPtr++; | |
1070 | /*@switchbreak@*/ break; | |
1071 | ||
1072 | case 'b': | |
1073 | *retPtr = '\b'; | |
1074 | retPtr++; | |
1075 | /*@switchbreak@*/ break; | |
1076 | ||
1077 | case 'f': | |
1078 | *retPtr = '\f'; | |
1079 | retPtr++; | |
1080 | /*@switchbreak@*/ break; | |
1081 | ||
1082 | case 'n': | |
1083 | *retPtr = '\n'; | |
1084 | retPtr++; | |
1085 | /*@switchbreak@*/ break; | |
1086 | ||
1087 | case 'r': | |
1088 | *retPtr = '\r'; | |
1089 | retPtr++; | |
1090 | /*@switchbreak@*/ break; | |
1091 | ||
1092 | case 't': | |
1093 | *retPtr = '\t'; | |
1094 | retPtr++; | |
1095 | /*@switchbreak@*/ break; | |
1096 | /* ' " ? \ */ | |
1097 | /* we assume invalid sequences are handled somewhere else | |
1098 | so we handle an invalid sequence of the form \char by replacing | |
1099 | it with char (this is what gcc does) the C standard says a diagnostic is | |
1100 | required..*/ | |
1101 | default: | |
1102 | *retPtr = *ptr; | |
1103 | retPtr++; | |
1104 | } | |
1105 | ptr++; | |
1106 | } | |
1107 | ||
1108 | }/*end outer if*/ | |
1109 | ||
1110 | }/*end while */ | |
1111 | ||
1112 | /* add the null character */ | |
1113 | *retPtr = '\0'; | |
1114 | ||
846c7865 | 1115 | llassert( (retPtr-ret) >= 0 ); |
1116 | *len = (size_t)(retPtr - ret); | |
1810fe2a | 1117 | return ret; |
1118 | } | |
1119 | ||
1120 | ||
1121 | /*this function is like sctring_expandEscapses */ | |
1122 | mstring cstring_expandEscapes (cstring s) | |
1123 | { | |
37ae0b5e | 1124 | size_t len; |
1810fe2a | 1125 | |
1126 | mstring ret; | |
1810fe2a | 1127 | ret = doExpandEscapes (s, &len); |
1128 | return ret; | |
1129 | } | |
1130 | ||
37ae0b5e | 1131 | size_t cstring_lengthExpandEscapes (cstring s) |
1810fe2a | 1132 | { |
37ae0b5e | 1133 | size_t len; |
46edc85c | 1134 | mstring tmpStr, tmpStr2; |
1135 | ||
1136 | tmpStr = doMergeString (s); | |
1137 | tmpStr2 = doExpandEscapes (tmpStr, &len); | |
46edc85c | 1138 | |
1810fe2a | 1139 | cstring_free(tmpStr); |
46edc85c | 1140 | cstring_free(tmpStr2); |
1810fe2a | 1141 | |
1142 | return len; | |
1143 | } | |
1144 | ||
9276a168 | 1145 | cstring cstring_replaceChar(/*@returned@*/ cstring c, char oldChar, char newChar) |
1146 | { | |
1147 | char *ptr; | |
1148 | llassert(oldChar != '\0'); | |
1149 | if (cstring_isUndefined(c) ) | |
1150 | { | |
1151 | llcontbug(cstring_makeLiteral("cstring_replaceChar called with undefined string")); | |
1152 | return c; | |
1153 | } | |
1154 | ||
1155 | ptr = c; | |
1156 | while (*ptr != '\0') | |
1157 | { | |
1158 | if (*ptr == oldChar) | |
1159 | *ptr = newChar; | |
1160 | ptr++; | |
1161 | } | |
1162 | ||
1163 | return c; | |
1164 | } | |
b941db6b | 1165 | |
393e573f | 1166 | |
1167 | ||
1168 | ||
1169 | ||
1170 | ||
1171 | ||
1172 |