2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
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.
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.
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.
20 ** For information on splint: splint@cs.virginia.edu
21 ** To report a bug: splint-bug@cs.virginia.edu
22 ** For more information: http://www.splint.org
30 * - use drive spec specials with OS2 like with WIN32
31 * - cstring_replaceAll () needed in cpplib.c
34 # include "splintMacros.nf"
39 /*@only@*/ /*@notnull@*/
40 cstring cstring_newEmpty (void)
42 return (cstring_create (0));
45 char cstring_firstChar (cstring s)
47 llassert (cstring_isDefined (s));
48 llassert (cstring_length (s) > 0);
53 char cstring_getChar (cstring s, int n)
55 int length = cstring_length (s);
57 llassert (cstring_isDefined (s));
58 llassert (n >= 1 && n <= length);
63 cstring cstring_suffix (cstring s, int n)
65 llassert (cstring_isDefined (s));
66 llassert (n <= cstring_length (s));
71 cstring cstring_prefix (cstring s, int n)
75 llassert (cstring_isDefined (s));
76 llassert (n <= cstring_length (s));
79 /*@-mods@*/ /* The modifications cancel out. */
88 /* effects If s = [0-9]*, returns s as an int.
92 int cstring_toPosInt (cstring s)
98 if (isdigit ((unsigned char) c))
100 val = (val * 10) + (int)(c - '0');
106 } end_cstring_chars ;
111 cstring cstring_afterChar (cstring s, char c)
113 llassert (cstring_isDefined (s));
114 return strchr (s, c);
117 cstring cstring_beforeChar (cstring s, char c)
119 if (cstring_isDefined (s))
121 char *cp = strchr (s, c);
129 ret = cstring_copy (s);
131 /*@=mods@*/ /* modification is undone */
137 return cstring_undefined;
140 void cstring_setChar (cstring s, int n, char c)
142 llassert (cstring_isDefined (s));
143 llassert (n > 0 && n <= cstring_length (s));
148 char cstring_lastChar (cstring s)
152 llassert (cstring_isDefined (s));
154 length = cstring_length (s);
155 llassert (length > 0);
157 return (s[length - 1]);
160 /*@only@*/ cstring cstring_copy (cstring s)
162 if (cstring_isDefined (s))
164 return (mstring_copy (s));
168 return cstring_undefined;
172 /*@only@*/ cstring cstring_copyLength (char *s, int len)
174 char *res = mstring_create (len + 1);
176 strncpy (res, s, size_fromInt (len));
181 bool cstring_containsChar (cstring c, char ch)
183 if (cstring_isDefined (c))
185 return (strchr (c, ch) != NULL);
194 ** Replaces all occurances of old in s with new.
197 void cstring_replaceAll (cstring s, char old, char snew)
200 llassert (old != snew);
202 if (cstring_isDefined (s))
204 char *sp = strchr (s, old);
209 sp = strchr (sp, old);
215 void cstring_replaceLit (/*@unique@*/ cstring s, char *old, char *snew)
218 llassert (strlen (old) >= strlen (snew));
220 if (cstring_isDefined (s))
222 char *sp = strstr (s, old);
226 int lendiff = size_toInt (strlen (old) - strlen (snew));
229 while (*tsnew != '\0')
236 while (*(sp + lendiff) != '\0')
238 *sp = *(sp + lendiff);
245 sp = strstr (s, old);
251 ** removes all chars in clist from s
254 void cstring_stripChars (cstring s, const char *clist)
256 if (cstring_isDefined (s))
259 int size = cstring_length (s);
261 for (i = 0; i < size; i++)
265 if (strchr (clist, c) != NULL)
267 /* strip this char */
272 for (j = i; j < size; j++)
284 bool cstring_contains (/*@unique@*/ cstring c, cstring sub)
286 if (cstring_isDefined (c))
288 llassert (cstring_isDefined (sub));
290 return (strstr (c, sub) != NULL);
298 static char lookLike (char c) /*@*/
300 if (c == 'I' || c == 'l')
304 else if (c == 'O' || c == 'o')
322 cmpcode cstring_genericEqual (cstring s, cstring t,
324 bool caseinsensitive,
327 if (s == t) return CGE_SAME;
328 else if (cstring_isUndefined (s))
330 return cstring_isEmpty (t) ? CGE_SAME : CGE_DISTINCT;
332 else if (cstring_isUndefined (t))
334 return cstring_isEmpty (s) ? CGE_SAME : CGE_DISTINCT;
339 bool diffcase = FALSE;
340 bool difflookalike = FALSE;
344 if (nchars > 0 && i >= nchars)
351 ; /* no difference */
353 else if (caseinsensitive
354 && (toupper ((int) *t) == toupper ((int) *s)))
358 else if (lookalike && (lookLike (*t) == lookLike (*s)))
360 difflookalike = TRUE;
371 if (*s == '\0' && *t != '\0')
380 else if (difflookalike)
382 return CGE_LOOKALIKE;
393 bool cstring_equalFree (/*@only@*/ cstring c1, /*@only@*/ cstring c2)
395 bool res = cstring_equal (c1, c2);
401 bool cstring_equal (cstring c1, cstring c2)
403 if (c1 == c2) return TRUE;
404 else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2);
405 else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1);
406 else return (strcmp (c1, c2) == 0);
409 bool cstring_equalLen (cstring c1, cstring c2, int len)
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 (strncmp (c1, c2, size_fromInt (len)) == 0);
417 bool cstring_equalCaseInsensitive (cstring c1, cstring c2)
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);
422 else return (cstring_genericEqual (c1, c2, 0, TRUE, FALSE) != CGE_DISTINCT);
425 bool cstring_equalLenCaseInsensitive (cstring c1, cstring c2, int len)
429 if (c1 == c2) return TRUE;
430 else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2);
431 else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1);
432 else return (cstring_genericEqual (c1, c2, len, TRUE, FALSE) != CGE_DISTINCT);
435 bool cstring_equalPrefix (cstring c1, cstring c2)
437 llassert (c2 != NULL);
439 if (cstring_isUndefined (c1))
441 return (strlen (c2) == 0);
444 return (strncmp (c1, c2, strlen (c2)) == 0);
447 bool cstring_equalPrefixLit (cstring c1, const char *c2)
449 llassert (c2 != NULL);
451 if (cstring_isUndefined (c1))
453 return (strlen (c2) == 0);
456 return (strncmp (c1, c2, strlen (c2)) == 0);
459 int cstring_xcompare (cstring *c1, cstring *c2)
461 return (cstring_compare (*c1, *c2));
464 int cstring_compare (cstring c1, cstring c2)
472 else if (cstring_isUndefined (c1))
474 if (cstring_isEmpty (c2))
483 else if (cstring_isUndefined (c2))
485 if (cstring_isEmpty (c1))
496 res = strcmp (c1, c2);
502 void cstring_markOwned (/*@owned@*/ cstring s)
507 void cstring_free (/*@only@*/ cstring s)
509 if (cstring_isDefined (s))
515 cstring cstring_fromChars (/*@exposed@*/ const char *cp)
520 /*@exposed@*/ char *cstring_toCharsSafe (cstring s)
522 static /*@only@*/ cstring emptystring = cstring_undefined;
524 if (cstring_isDefined (s))
530 if (cstring_isUndefined (emptystring))
532 emptystring = cstring_newEmpty ();
539 int cstring_length (cstring s)
541 if (cstring_isDefined (s))
543 return size_toInt (strlen (s));
549 cstring_capitalize (cstring s)
551 if (!cstring_isEmpty (s))
553 cstring ret = cstring_copy (s);
555 cstring_setChar (ret, 1, (char) toupper ((int) cstring_firstChar (ret)));
559 return cstring_undefined;
563 cstring_capitalizeFree (cstring s)
565 if (!cstring_isEmpty (s))
567 cstring_setChar (s, 1, (char) toupper ((int) cstring_firstChar (s)));
575 cstring_clip (cstring s, int len)
577 if (cstring_isUndefined (s) || cstring_length (s) <= len)
583 llassert (s != NULL);
591 cstring_elide (cstring s, int len)
593 if (cstring_isUndefined (s) || cstring_length (s) <= len)
595 return cstring_copy (s);
599 cstring sc = cstring_create (len);
601 strncpy (sc, s, size_fromInt (len));
602 *(sc + len - 1) = '\0';
603 *(sc + len - 2) = '.';
604 *(sc + len - 3) = '.';
605 *(sc + len - 4) = '.';
611 cstring_fill (cstring s, int n)
613 cstring t = cstring_create (n + 1);
615 int len = cstring_length (s);
620 for (i = 0; i < n; i++)
628 for (i = 0; i < len; i++)
632 for (i = 0; i < n - len; i++)
643 cstring_downcase (cstring s)
645 if (cstring_isDefined (s))
647 cstring t = cstring_create (strlen (s) + 1);
651 while ((c = *s) != '\0')
653 if (c >= 'A' && c <= 'Z')
666 return cstring_undefined;
670 /*@notnull@*/ cstring
671 cstring_appendChar (/*@only@*/ cstring s1, char c)
673 int l = cstring_length (s1);
676 s = (char *) dmalloc (sizeof (*s) * (l + 2));
678 if (cstring_isDefined (s1))
695 cstring_concatFree (cstring s, cstring t)
697 cstring res = cstring_concat (s, t);
704 cstring_concatFree1 (cstring s, cstring t)
706 cstring res = cstring_concat (s, t);
713 cstring_concatChars (cstring s, char *t)
715 cstring res = cstring_concat (s, cstring_fromChars (t));
722 cstring_concatLength (cstring s1, char *s2, int len)
724 cstring tmp = cstring_copyLength (s2, len);
725 cstring res = cstring_concat (s1, tmp);
733 cstring_concat (cstring s, cstring t)
735 char *ret = mstring_create (cstring_length (s) + cstring_length (t));
737 if (cstring_isDefined (s))
741 if (cstring_isDefined (t))
749 /*@notnull@*/ /*@only@*/ cstring
750 cstring_prependCharO (char c, /*@only@*/ cstring s1)
752 cstring res = cstring_prependChar (c, s1);
758 /*@notnull@*/ /*@only@*/ cstring
759 cstring_prependChar (char c, /*@temp@*/ cstring s1)
761 int l = cstring_length (s1);
762 char *s = (char *) dmalloc (sizeof (*s) * (l + 2));
766 if (cstring_isDefined (s1))
768 /*@-mayaliasunique@*/
770 /*@=mayaliasunique@*/
779 cstring_hasNonAlphaNumBar (cstring s)
783 if (cstring_isUndefined (s)) return FALSE;
785 while ((c = (int) *s) != (int) '\0')
787 if ((isalnum (c) == 0) && (c != (int) '_')
788 && (c != (int) '.') && (c != (int) CONNECTCHAR))
799 /*@only@*/ /*@notnull@*/ cstring
800 cstring_create (int n)
802 char *s = dmalloc (sizeof (*s) * (n + 1));
808 /*@only@*/ /*@notnull@*/ cstring
809 cstring_copySegment (cstring s, int findex, int tindex)
811 cstring res = cstring_create (tindex - findex + 1);
813 llassert (cstring_isDefined (s));
814 llassert (cstring_length (s) > tindex);
816 strncpy (res, (s + findex), size_fromInt ((tindex - findex + 1)));
821 lsymbol cstring_toSymbol (cstring s)
823 lsymbol res = lsymbol_fromString (s);
830 cstring cstring_bsearch (cstring key, char **table, int nentries)
832 if (cstring_isDefined (key))
836 int mid = (high + low + 1) / 2;
838 cstring res = cstring_undefined;
840 while (low <= high && mid < nentries)
844 llassert (mid != last);
845 llassert (mid >= 0 && mid < nentries);
847 cmp = cstring_compare (key, table[mid]);
854 else if (cmp < 0) /* key is before table[mid] */
858 else /* key of after table[mid] */
864 mid = (high + low + 1) / 2;
867 if (mid != 0 && mid < nentries - 1)
869 llassert (cstring_compare (key, table[mid - 1]) > 0);
870 llassert (cstring_compare (key, table[mid + 1]) < 0);
876 return cstring_undefined;
879 extern /*@observer@*/ cstring cstring_advanceWhiteSpace (cstring s)
881 if (cstring_isDefined (s)) {
884 while (*t != '\0' && isspace ((int) *t)) {
891 return cstring_undefined;