]> andersk Git - splint.git/blame - src/cstring.c
Fixed problem with print format for +showalluses.
[splint.git] / src / cstring.c
CommitLineData
616915dd 1/*
11db3170 2** Splint - annotation-assisted static program checker
77d37419 3** Copyright (C) 1994-2002 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 40cstring cstring_newEmpty (void)
41{
42 return (cstring_create (0));
43}
44
45char cstring_firstChar (cstring s)
46{
47 llassert (cstring_isDefined (s));
48 llassert (cstring_length (s) > 0);
49
50 return (s[0]);
51}
52
abd7f895 53char 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 63cstring 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 71cstring 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
94int 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 113cstring cstring_afterChar (cstring s, char c)
114{
115 llassert (cstring_isDefined (s));
116 return strchr (s, c);
117}
118
616915dd 119cstring 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 142void 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
150char 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
183bool 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 199void 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 217void 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
259void 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 {
abd7f895 268 /*drl bee: is*/
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 {
abd7f895 280 /*drl bee: is*/ /*drl bee: is*/ s[j] = s[j+1];
616915dd 281 }
abd7f895 282
283 /*drl bee: is*/ s[size] = '\0';
616915dd 284 i--;
285 }
286 }
287 }
288}
289
290bool 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
304static 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
328cmpcode 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
86d93ed3 378 /*drl bee: ib*/
379 /*drl bee: ib*/
616915dd 380 if (*s == '\0' && *t != '\0')
381 {
382 return CGE_DISTINCT;
383 }
384
385 if (diffcase)
386 {
387 return CGE_CASE;
388 }
389 else if (difflookalike)
390 {
391 return CGE_LOOKALIKE;
392 }
393 else
394 {
395 return CGE_SAME;
396 }
397 }
398}
399
400
401
402bool cstring_equalFree (/*@only@*/ cstring c1, /*@only@*/ cstring c2)
403{
404 bool res = cstring_equal (c1, c2);
405 cstring_free (c1);
406 cstring_free (c2);
407 return res;
408}
409
410bool cstring_equal (cstring c1, cstring c2)
411{
412 if (c1 == c2) return TRUE;
413 else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2);
414 else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1);
415 else return (strcmp (c1, c2) == 0);
416}
417
abd7f895 418bool cstring_equalLen (cstring c1, cstring c2, size_t len)
616915dd 419{
420 if (c1 == c2) return TRUE;
421 else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2);
422 else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1);
abd7f895 423 else return (strncmp (c1, c2, len) == 0);
616915dd 424}
425
426bool cstring_equalCaseInsensitive (cstring c1, cstring c2)
427{
428 if (c1 == c2) return TRUE;
429 else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2);
430 else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1);
431 else return (cstring_genericEqual (c1, c2, 0, TRUE, FALSE) != CGE_DISTINCT);
432}
433
abd7f895 434bool cstring_equalLenCaseInsensitive (cstring c1, cstring c2, size_t len)
616915dd 435{
616915dd 436 if (c1 == c2) return TRUE;
437 else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2);
438 else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1);
439 else return (cstring_genericEqual (c1, c2, len, TRUE, FALSE) != CGE_DISTINCT);
440}
441
68de3f33 442bool cstring_equalPrefix (cstring c1, cstring c2)
616915dd 443{
444 llassert (c2 != NULL);
445
446 if (cstring_isUndefined (c1))
447 {
448 return (strlen (c2) == 0);
449 }
450
451 return (strncmp (c1, c2, strlen (c2)) == 0);
452}
453
68de3f33 454bool cstring_equalPrefixLit (cstring c1, const char *c2)
616915dd 455{
456 llassert (c2 != NULL);
457
458 if (cstring_isUndefined (c1))
459 {
460 return (strlen (c2) == 0);
461 }
462
616915dd 463 return (strncmp (c1, c2, strlen (c2)) == 0);
616915dd 464}
465
466int cstring_xcompare (cstring *c1, cstring *c2)
467{
468 return (cstring_compare (*c1, *c2));
469}
470
471int cstring_compare (cstring c1, cstring c2)
472{
473 int res;
474
475 if (c1 == c2)
476 {
477 res = 0;
478 }
479 else if (cstring_isUndefined (c1))
480 {
481 if (cstring_isEmpty (c2))
482 {
483 res = 0;
484 }
485 else
486 {
487 res = 1;
488 }
489 }
490 else if (cstring_isUndefined (c2))
491 {
492 if (cstring_isEmpty (c1))
493 {
494 res = 0;
495 }
496 else
497 {
498 res = -1;
499 }
500 }
501 else
502 {
503 res = strcmp (c1, c2);
504 }
505
506 return (res);
507}
508
509void cstring_markOwned (/*@owned@*/ cstring s)
510{
511 sfreeEventually (s);
512}
513
514void cstring_free (/*@only@*/ cstring s)
515{
516 if (cstring_isDefined (s))
517 {
c21b4c87 518 /*drl 2/3/2002*/
519 s[0] = '\0';
520
616915dd 521 sfree (s);
522 }
523}
524
525cstring cstring_fromChars (/*@exposed@*/ const char *cp)
526{
527 return (cstring) cp;
528}
529
530/*@exposed@*/ char *cstring_toCharsSafe (cstring s)
531{
532 static /*@only@*/ cstring emptystring = cstring_undefined;
533
534 if (cstring_isDefined (s))
535 {
536 return (char *) s;
537 }
538 else
539 {
540 if (cstring_isUndefined (emptystring))
541 {
542 emptystring = cstring_newEmpty ();
543 }
544
545 return emptystring;
546 }
547}
548
abd7f895 549size_t cstring_length (cstring s)
616915dd 550{
551 if (cstring_isDefined (s))
552 {
abd7f895 553 return strlen (s);
616915dd 554 }
abd7f895 555
616915dd 556 return 0;
557}
558
559cstring
86d93ed3 560cstring_capitalize (cstring s) /*@requires maxSet(s) >= 0 @*/
616915dd 561{
562 if (!cstring_isEmpty (s))
563 {
564 cstring ret = cstring_copy (s);
565
566 cstring_setChar (ret, 1, (char) toupper ((int) cstring_firstChar (ret)));
567 return ret;
568 }
569
570 return cstring_undefined;
571}
572
573cstring
86d93ed3 574cstring_capitalizeFree (cstring s) /*@requires maxSet(s) >= 0 /\ maxRead(s) >= 0 @*/
616915dd 575{
576 if (!cstring_isEmpty (s))
577 {
578 cstring_setChar (s, 1, (char) toupper ((int) cstring_firstChar (s)));
579 return s;
580 }
581
582 return s;
583}
584
585cstring
abd7f895 586cstring_clip (cstring s, size_t len)
616915dd 587{
588 if (cstring_isUndefined (s) || cstring_length (s) <= len)
589 {
590 ;
591 }
592 else
593 {
594 llassert (s != NULL);
abd7f895 595 /*drl bee: mrms*/
596 *(s + len) = '\0';
616915dd 597 }
abd7f895 598
616915dd 599 return s;
600}
601
602/*@only@*/ cstring
abd7f895 603cstring_elide (cstring s, size_t len)
616915dd 604{
605 if (cstring_isUndefined (s) || cstring_length (s) <= len)
606 {
607 return cstring_copy (s);
608 }
609 else
610 {
611 cstring sc = cstring_create (len);
abd7f895 612
613 strncpy (sc, s, len);
614 /*drl bee: mrms*/
615 *(sc + len - 1) = '\0';
616915dd 616 *(sc + len - 2) = '.';
617 *(sc + len - 3) = '.';
618 *(sc + len - 4) = '.';
abd7f895 619
616915dd 620 return sc;
621 }
622}
623
624/*@only@*/ cstring
abd7f895 625cstring_fill (cstring s, size_t n) /*@requires n >= 0 @*/
616915dd 626{
627 cstring t = cstring_create (n + 1);
628 cstring ot = t;
abd7f895 629 size_t len = cstring_length (s);
630 size_t i;
616915dd 631
632 if (len > n)
633 {
634 for (i = 0; i < n; i++)
635 {
86d93ed3 636 /*drl bee: is*/
637 /*drl bee: is*/
616915dd 638 *t++ = *s++;
639 }
640 *t = '\0';
641 }
642 else
643 {
644 for (i = 0; i < len; i++)
645 {
86d93ed3 646 /*drl bee: is*/
647/*drl bee: is*/
616915dd 648 *t++ = *s++;
649 }
650 for (i = 0; i < n - len; i++)
651 {
86d93ed3 652/*drl bee: is*/
616915dd 653 *t++ = ' ';
654 }
655 *t = '\0';
656 }
657
658 return ot;
659}
660
661cstring
662cstring_downcase (cstring s)
663{
664 if (cstring_isDefined (s))
665 {
666 cstring t = cstring_create (strlen (s) + 1);
667 cstring ot = t;
668 char c;
669
86d93ed3 670 /*drl bee: lhnt*/ while ((c = *s) != '\0')
616915dd 671 {
672 if (c >= 'A' && c <= 'Z')
673 {
674 c = c - 'A' + 'a';
675 }
676 *t++ = c;
677 s++;
678 }
86d93ed3 679 /*drl bee: is*/ *t = '\0';
616915dd 680
681 return ot;
682 }
683 else
684 {
685 return cstring_undefined;
686 }
687}
688
689/*@notnull@*/ cstring
690cstring_appendChar (/*@only@*/ cstring s1, char c)
691{
abd7f895 692 size_t l = cstring_length (s1);
616915dd 693 char *s;
694
695 s = (char *) dmalloc (sizeof (*s) * (l + 2));
696
697 if (cstring_isDefined (s1))
698 {
699 strcpy (s, s1);
700 *(s + l) = c;
86d93ed3 701 /*drl bee: dm*/ *(s + l + 1) = '\0';
616915dd 702 sfree (s1);
703 }
704 else
705 {
706 *(s) = c;
4dd72714 707 /*drl bee: dm*/ *(s + 1) = '\0';
616915dd 708 }
709
710 return s;
711}
712
713/*@only@*/ cstring
714cstring_concatFree (cstring s, cstring t)
715{
716 cstring res = cstring_concat (s, t);
717 cstring_free (s);
718 cstring_free (t);
719 return res;
720}
721
722/*@only@*/ cstring
723cstring_concatFree1 (cstring s, cstring t)
724{
725 cstring res = cstring_concat (s, t);
726 cstring_free (s);
727 return res;
728}
729
730# ifndef NOLCL
731/*@only@*/ cstring
732cstring_concatChars (cstring s, char *t)
733{
734 cstring res = cstring_concat (s, cstring_fromChars (t));
735 cstring_free (s);
736 return res;
737}
738# endif
739
740/*@only@*/ cstring
abd7f895 741cstring_concatLength (cstring s1, char *s2, size_t len) /*@requires maxSet(s2) >= (len - 1) @*/
616915dd 742{
743 cstring tmp = cstring_copyLength (s2, len);
744 cstring res = cstring_concat (s1, tmp);
745 cstring_free (tmp);
746 cstring_free (s1);
747
748 return res;
749}
750
751/*@only@*/ cstring
86d93ed3 752cstring_concat (cstring s, cstring t) /*@requires maxSet(s) >= 0 @*/
616915dd 753{
754 char *ret = mstring_create (cstring_length (s) + cstring_length (t));
755
756 if (cstring_isDefined (s))
757 {
86d93ed3 758 /*drl bee: sl*/ strcpy (ret, s);
616915dd 759 }
760 if (cstring_isDefined (t))
761 {
762 strcat (ret, t);
763 }
764
765 return ret;
766}
767
768/*@notnull@*/ /*@only@*/ cstring
769cstring_prependCharO (char c, /*@only@*/ cstring s1)
770{
771 cstring res = cstring_prependChar (c, s1);
772
773 cstring_free (s1);
774 return (res);
775}
776
777/*@notnull@*/ /*@only@*/ cstring
778cstring_prependChar (char c, /*@temp@*/ cstring s1)
779{
abd7f895 780 size_t l = cstring_length (s1);
616915dd 781 char *s = (char *) dmalloc (sizeof (*s) * (l + 2));
782
86d93ed3 783/*drl bee: dm*/ *(s) = c;
616915dd 784
785 if (cstring_isDefined (s1))
786 {
787 /*@-mayaliasunique@*/
788 strcpy (s + 1, s1);
789 /*@=mayaliasunique@*/
790 }
791
86d93ed3 792 /*drl bee: dm*/ *(s + l + 1) = '\0';
616915dd 793 return s;
794}
795
796# ifndef NOLCL
797bool
798cstring_hasNonAlphaNumBar (cstring s)
799{
800 int c;
801
802 if (cstring_isUndefined (s)) return FALSE;
803
86d93ed3 804/*drl bee: lhnt*/ while ((c = (int) *s) != (int) '\0')
616915dd 805 {
806 if ((isalnum (c) == 0) && (c != (int) '_')
807 && (c != (int) '.') && (c != (int) CONNECTCHAR))
808 {
809 return TRUE;
810 }
811
812 s++;
813 }
814 return FALSE;
815}
816# endif
817
818/*@only@*/ /*@notnull@*/ cstring
abd7f895 819cstring_create (size_t n)
616915dd 820{
821 char *s = dmalloc (sizeof (*s) * (n + 1));
abd7f895 822
823 /*drl bee: dm*/ *s = '\0';
616915dd 824 return s;
825}
826
2934b455 827/*@only@*/ /*@notnull@*/ cstring
abd7f895 828cstring_copySegment (cstring s, size_t findex, size_t tindex)
2934b455 829{
830 cstring res = cstring_create (tindex - findex + 1);
831
832 llassert (cstring_isDefined (s));
833 llassert (cstring_length (s) > tindex);
834
835 strncpy (res, (s + findex), size_fromInt ((tindex - findex + 1)));
836 return res;
837}
838
616915dd 839# ifndef NOLCL
840lsymbol cstring_toSymbol (cstring s)
841{
2934b455 842 lsymbol res = lsymbol_fromString (s);
616915dd 843
844 cstring_free (s);
845 return res;
846}
847# endif
848
849cstring cstring_bsearch (cstring key, char **table, int nentries)
850{
851 if (cstring_isDefined (key))
852 {
853 int low = 0;
854 int high = nentries;
855 int mid = (high + low + 1) / 2;
856 int last = -1;
857 cstring res = cstring_undefined;
858
859 while (low <= high && mid < nentries)
860 {
861 int cmp;
862
863 llassert (mid != last);
864 llassert (mid >= 0 && mid < nentries);
865
866 cmp = cstring_compare (key, table[mid]);
867
868 if (cmp == 0)
869 {
870 res = table[mid];
871 break;
872 }
873 else if (cmp < 0) /* key is before table[mid] */
874 {
875 high = mid - 1;
876 }
877 else /* key of after table[mid] */
878 {
879 low = mid + 1;
880 }
881
882 last = mid;
883 mid = (high + low + 1) / 2;
884 }
885
886 if (mid != 0 && mid < nentries - 1)
887 {
888 llassert (cstring_compare (key, table[mid - 1]) > 0);
86d93ed3 889 /*drl bee: ndv*/ llassert (cstring_compare (key, table[mid + 1]) < 0);
616915dd 890 }
891
892 return res;
893 }
894
895 return cstring_undefined;
896}
897
898extern /*@observer@*/ cstring cstring_advanceWhiteSpace (cstring s)
899{
900 if (cstring_isDefined (s)) {
901 char *t = s;
902
86d93ed3 903 /*drl bee: lhnt*/ while (*t != '\0' && isspace ((int) *t)) {
616915dd 904 t++;
905 }
906
907 return t;
908 }
909
910 return cstring_undefined;
911}
1810fe2a 912
393e573f 913/*@i3534 @*/
37ae0b5e 914/*@ignore@*/ /* !!! DRL don't ignore large segments like this without a good reason! */
393e573f 915
46edc85c 916/* changes strings like "sdf" "sdfsd" into "sdfsdfsd"*/
917/* This function understands that "sdf\" \"sdfsdf" is okay*/
918static mstring doMergeString (cstring s)
919{
920 char *ptr;
921 mstring ret;
922 char * retPtr;
923 bool escape;
924
925 llassert(cstring_isDefined (s));
926
927 ret = mstring_create (cstring_length(s) );
928
929 ptr = s;
930
931 retPtr = ret;
932 /*
933 llassert(*ptr == '\"');
934
935 *retPtr = *ptr;
936
937 retPtr++;
938 ptr++;
939 */
940
941 while (*ptr != '\0')
942 {
943 escape = FALSE;
944
945 if (*ptr == '\\')
946 {
947 *retPtr = *ptr;
948
949 if (!escape)
950 escape = TRUE;
951 else
952 /* case of escaped \ ('\\') */
953 escape = FALSE;
954 }
955 else if ( (*ptr == '\"') && (!escape) )
956 {
957 while ( (ptr[1] != '\"') && (ptr[1] != '\0') )
958 {
959 ptr++;
960 }
961 if (ptr[1] == '\0')
962 {
963 llassert(*ptr == '\"');
964 *retPtr = '\"';
965 retPtr++;
966 *retPtr = '\0';
967 BADEXIT;
968 return ret;
969 }
970 else
971 {
972 ptr++;
973 }
974 }
975 else
976 {
977 *retPtr = *ptr;
978 }
979
980 retPtr++;
981 ptr++;
982
983 }/* end while */
984 retPtr = '\0';
985 return ret;
986}
987
37ae0b5e 988static mstring doExpandEscapes (cstring s, /*@out@*/ size_t *len)
1810fe2a 989{
990 char *ptr;
991 mstring ret;
992 char * retPtr;
1810fe2a 993
994 llassert(cstring_isDefined (s));
995
abd7f895 996 ret = mstring_create (cstring_length(s));
1810fe2a 997
998 ptr = s;
999
1000 retPtr = ret;
1001 while (*ptr != '\0')
1002 {
1003 if (*ptr != '\\')
1004 {
1005 *retPtr = *ptr;
1006 retPtr++;
1007 ptr++;
1008 continue;
1009 }
1010
1011 if (*ptr == '\\')
1012 {
1013 ptr++;
1014 if (*ptr == '\0')
1015 {
1016 /*not a legal escape sequence but try to handle it in a sesible way*/
1017 *retPtr = '\\';
1018 retPtr++;
1019 }
1020
0e5499ac 1021 /* Handle Octal escapes */
1810fe2a 1022 else if (*ptr >= '0' && *ptr <= '9' )
1023 {
1024 int total;
1025 total = (int)(*ptr - '0');
1026 ptr++;
1027 /*octal can only be 3 characters long */
1028 if (*ptr != '\0' && (*ptr >= '0' && *ptr <= '9' ) )
1029 {
1030 total *= 8;
1031 ptr++;
1032 if (*ptr != '\0' && (*ptr >= '0' && *ptr <= '9' ) )
1033 {
1034 total *= 8;
1035 total += (int) (*ptr - '0');
1036 ptr++;
1037 }
1038 }
1039
1040 *retPtr = (char) total;
1041 retPtr++;
1042 }
1043
1044 else if (*ptr == 'x')
1045 {
1046 int total;
1047 total = 0;
1048 ptr++;
1049 if (!(*ptr != '\0' &&
1050 ( (*ptr >= '0' && *ptr <= '9' ) ||
1051 (toupper(*ptr) >= (int)('A') && toupper(*ptr) <= (int)('F') ) )
1052 ))
1053 {
1054 total = (int)'x';
1055 }
1056 else
1057 {
1058 while (*ptr != '\0' &&
1059 ( (*ptr >= '0' && *ptr <= '9' ) ||
1060 (toupper(*ptr) >= ((int)('A')) && toupper(*ptr) <= ((int)'F') ) )
1061 )
1062 {
1063 total *= 16;
1064 if (*ptr >= '0' && *ptr <= '9' )
1065 total += (int)(*ptr - '0');
1066 else
1067 total += ( (toupper(*ptr) - 'A') + 10);
1068 ptr++;
1069 }
1070 }
1071 *retPtr = (char) total;
1072 retPtr++;
1073 }
1074 else
1075 {
1076 switch ( *ptr )
1077 {
1078 case 'a':
1079 *retPtr = '\a';
1080 retPtr++;
1081 /*@switchbreak@*/ break;
1082
1083 case 'b':
1084 *retPtr = '\b';
1085 retPtr++;
1086 /*@switchbreak@*/ break;
1087
1088 case 'f':
1089 *retPtr = '\f';
1090 retPtr++;
1091 /*@switchbreak@*/ break;
1092
1093 case 'n':
1094 *retPtr = '\n';
1095 retPtr++;
1096 /*@switchbreak@*/ break;
1097
1098 case 'r':
1099 *retPtr = '\r';
1100 retPtr++;
1101 /*@switchbreak@*/ break;
1102
1103 case 't':
1104 *retPtr = '\t';
1105 retPtr++;
1106 /*@switchbreak@*/ break;
1107 /* ' " ? \ */
1108 /* we assume invalid sequences are handled somewhere else
1109 so we handle an invalid sequence of the form \char by replacing
1110 it with char (this is what gcc does) the C standard says a diagnostic is
1111 required..*/
1112 default:
1113 *retPtr = *ptr;
1114 retPtr++;
1115 }
1116 ptr++;
1117 }
1118
1119 }/*end outer if*/
1120
1121 }/*end while */
1122
1123 /* add the null character */
1124 *retPtr = '\0';
1125
1126 *len = retPtr - ret;
1127 return ret;
1128}
1129
1130
1131/*this function is like sctring_expandEscapses */
1132mstring cstring_expandEscapes (cstring s)
1133{
37ae0b5e 1134 size_t len;
1810fe2a 1135
1136 mstring ret;
1810fe2a 1137 ret = doExpandEscapes (s, &len);
1138 return ret;
1139}
1140
37ae0b5e 1141size_t cstring_lengthExpandEscapes (cstring s)
1810fe2a 1142{
37ae0b5e 1143 size_t len;
46edc85c 1144 mstring tmpStr, tmpStr2;
1145
1146 tmpStr = doMergeString (s);
1147 tmpStr2 = doExpandEscapes (tmpStr, &len);
46edc85c 1148
1810fe2a 1149 cstring_free(tmpStr);
46edc85c 1150 cstring_free(tmpStr2);
1810fe2a 1151
1152 return len;
1153}
1154
9276a168 1155cstring cstring_replaceChar(/*@returned@*/ cstring c, char oldChar, char newChar)
1156{
1157 char *ptr;
1158 llassert(oldChar != '\0');
1159 if (cstring_isUndefined(c) )
1160 {
1161 llcontbug(cstring_makeLiteral("cstring_replaceChar called with undefined string"));
1162 return c;
1163 }
1164
1165 ptr = c;
1166 while (*ptr != '\0')
1167 {
1168 if (*ptr == oldChar)
1169 *ptr = newChar;
1170 ptr++;
1171 }
1172
1173 return c;
1174}
393e573f 1175/*@end@*/
1176
1177
1178
1179
1180
1181
1182
This page took 0.223365 seconds and 5 git commands to generate.