2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 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: info@splint.org
21 ** To report a bug: splint-bug@splint.org
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, size_t n)
55 size_t length = cstring_length (s);
57 llassert (cstring_isDefined (s));
58 llassert (n >= 1 && n <= length);
63 cstring cstring_suffix (cstring s, size_t n)
65 llassert (cstring_isDefined (s));
66 llassert (n <= cstring_length (s));
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 @*/
77 llassert (cstring_isDefined (s));
78 llassert (n <= cstring_length (s));
81 /*@-mods@*/ /* The modifications cancel out. */
90 /* effects If s = [0-9]*, returns s as an int.
94 int cstring_toPosInt (cstring s)
100 if (isdigit ((unsigned char) c))
102 val = (val * 10) + (int)(c - '0');
108 } end_cstring_chars ;
113 cstring cstring_afterChar (cstring s, char c)
115 llassert (cstring_isDefined (s));
116 return strchr (s, c);
119 cstring cstring_beforeChar (cstring s, char c)
121 if (cstring_isDefined (s))
123 char *cp = strchr (s, c);
131 ret = cstring_copy (s);
133 /*@=mods@*/ /* modification is undone */
139 return cstring_undefined;
142 void cstring_setChar (cstring s, size_t n, char c) /*@requires maxRead(s) >= (n - 1) /\ maxSet(s) >= (n - 1) @*/
144 llassert (cstring_isDefined (s));
145 llassert (n > 0 && n <= cstring_length (s));
150 char cstring_lastChar (cstring s)
154 llassert (cstring_isDefined (s));
156 length = cstring_length (s);
157 llassert (length > 0);
159 return (s[length - 1]);
162 /*@only@*/ cstring cstring_copy (cstring s) /*@ensures maxSet(result) == maxRead(s) /\ maxRead(result) == maxRead(s) @*/
164 if (cstring_isDefined (s))
166 return (mstring_copy (s));
170 return cstring_undefined;
174 /*@only@*/ cstring cstring_copyLength (char *s, size_t len) /*@requires maxSet(s) >= (len - 1) @*/
176 char *res = mstring_create (len + 1);
178 strncpy (res, s, len);
183 bool cstring_containsChar (cstring c, char ch)
185 if (cstring_isDefined (c))
187 return (strchr (c, ch) != NULL);
196 ** Replaces all occurances of old in s with new.
199 void cstring_replaceAll (cstring s, char old, char snew)
202 llassert (old != snew);
204 if (cstring_isDefined (s))
206 char *sp = strchr (s, old);
211 sp = strchr (sp, old);
217 void cstring_replaceLit (/*@unique@*/ cstring s, char *old, char *snew)
218 /*@requires maxRead(snew) >= 0 /\ maxRead(old) >= 0 /\ maxRead(old) >= maxRead(snew) @*/
220 llassert (strlen (old) >= strlen (snew));
222 if (cstring_isDefined (s))
224 char *sp = strstr (s, old);
228 int lendiff = size_toInt (strlen (old) - strlen (snew));
231 llassert (lendiff >= 0);
233 while (*tsnew != '\0')
235 llassert (*sp != '\0');
241 while (*(sp + lendiff) != '\0')
243 *sp = *(sp + lendiff);
250 sp = strstr (s, old);
256 ** removes all chars in clist from s
259 void cstring_stripChars (cstring s, const char *clist)
261 if (cstring_isDefined (s))
264 size_t size = cstring_length (s);
266 for (i = 0; i < size_toInt (size); i++)
271 if (strchr (clist, c) != NULL)
273 /* strip this char */
278 for (j = i; j < size_toInt (size); j++)
280 /*drl bee: is*/ /*drl bee: is*/ s[j] = s[j+1];
283 /*drl bee: is*/ s[size] = '\0';
290 bool cstring_contains (/*@unique@*/ cstring c, cstring sub)
292 if (cstring_isDefined (c))
294 llassert (cstring_isDefined (sub));
296 return (strstr (c, sub) != NULL);
304 static char lookLike (char c) /*@*/
306 if (c == 'I' || c == 'l')
310 else if (c == 'O' || c == 'o')
328 cmpcode cstring_genericEqual (cstring s, cstring t,
330 bool caseinsensitive,
332 /*@requires maxRead(s) >= nchars /\ maxRead(t) >= nchars @*/
334 if (s == t) return CGE_SAME;
335 else if (cstring_isUndefined (s))
337 return cstring_isEmpty (t) ? CGE_SAME : CGE_DISTINCT;
339 else if (cstring_isUndefined (t))
341 return cstring_isEmpty (s) ? CGE_SAME : CGE_DISTINCT;
346 bool diffcase = FALSE;
347 bool difflookalike = FALSE;
351 if (nchars > 0 && i >= size_toInt (nchars))
358 ; /* no difference */
360 else if (caseinsensitive
361 && (toupper ((int) *t) == toupper ((int) *s)))
365 else if (lookalike && (lookLike (*t) == lookLike (*s)))
367 difflookalike = TRUE;
380 if (*s == '\0' && *t != '\0')
389 else if (difflookalike)
391 return CGE_LOOKALIKE;
402 bool cstring_equalFree (/*@only@*/ cstring c1, /*@only@*/ cstring c2)
404 bool res = cstring_equal (c1, c2);
410 bool cstring_equal (cstring c1, cstring c2)
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);
418 bool cstring_equalLen (cstring c1, cstring c2, size_t len)
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);
423 else return (strncmp (c1, c2, len) == 0);
426 bool cstring_equalCaseInsensitive (cstring c1, cstring c2)
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);
434 bool cstring_equalLenCaseInsensitive (cstring c1, cstring c2, size_t len)
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);
442 bool cstring_equalPrefix (cstring c1, cstring c2)
444 llassert (c2 != NULL);
446 if (cstring_isUndefined (c1))
448 return (strlen (c2) == 0);
451 return (strncmp (c1, c2, strlen (c2)) == 0);
454 bool cstring_equalPrefixLit (cstring c1, const char *c2)
456 llassert (c2 != NULL);
458 if (cstring_isUndefined (c1))
460 return (strlen (c2) == 0);
463 return (strncmp (c1, c2, strlen (c2)) == 0);
466 int cstring_xcompare (cstring *c1, cstring *c2)
468 return (cstring_compare (*c1, *c2));
471 int cstring_compare (cstring c1, cstring c2)
479 else if (cstring_isUndefined (c1))
481 if (cstring_isEmpty (c2))
490 else if (cstring_isUndefined (c2))
492 if (cstring_isEmpty (c1))
503 res = strcmp (c1, c2);
509 void cstring_markOwned (/*@owned@*/ cstring s)
514 void cstring_free (/*@only@*/ cstring s)
516 if (cstring_isDefined (s))
525 cstring cstring_fromChars (/*@exposed@*/ const char *cp)
530 /*@exposed@*/ char *cstring_toCharsSafe (cstring s)
532 static /*@only@*/ cstring emptystring = cstring_undefined;
534 if (cstring_isDefined (s))
540 if (cstring_isUndefined (emptystring))
542 emptystring = cstring_newEmpty ();
549 size_t cstring_length (cstring s)
551 if (cstring_isDefined (s))
560 cstring_capitalize (cstring s) /*@requires maxSet(s) >= 0 @*/
562 if (!cstring_isEmpty (s))
564 cstring ret = cstring_copy (s);
566 cstring_setChar (ret, 1, (char) toupper ((int) cstring_firstChar (ret)));
570 return cstring_undefined;
574 cstring_capitalizeFree (cstring s) /*@requires maxSet(s) >= 0 /\ maxRead(s) >= 0 @*/
576 if (!cstring_isEmpty (s))
578 cstring_setChar (s, 1, (char) toupper ((int) cstring_firstChar (s)));
586 cstring_clip (cstring s, size_t len)
588 if (cstring_isUndefined (s) || cstring_length (s) <= len)
594 llassert (s != NULL);
603 cstring_elide (cstring s, size_t len)
605 if (cstring_isUndefined (s) || cstring_length (s) <= len)
607 return cstring_copy (s);
611 cstring sc = cstring_create (len);
613 strncpy (sc, s, len);
615 *(sc + len - 1) = '\0';
616 *(sc + len - 2) = '.';
617 *(sc + len - 3) = '.';
618 *(sc + len - 4) = '.';
625 cstring_fill (cstring s, size_t n) /*@requires n >= 0 @*/
627 cstring t = cstring_create (n + 1);
629 size_t len = cstring_length (s);
634 for (i = 0; i < n; i++)
644 for (i = 0; i < len; i++)
650 for (i = 0; i < n - len; i++)
662 cstring_downcase (cstring s)
664 if (cstring_isDefined (s))
666 cstring t = cstring_create (strlen (s) + 1);
670 /*drl bee: lhnt*/ while ((c = *s) != '\0')
672 if (c >= 'A' && c <= 'Z')
679 /*drl bee: is*/ *t = '\0';
685 return cstring_undefined;
689 /*@notnull@*/ cstring
690 cstring_appendChar (/*@only@*/ cstring s1, char c)
692 size_t l = cstring_length (s1);
695 s = (char *) dmalloc (sizeof (*s) * (l + 2));
697 if (cstring_isDefined (s1))
701 /*drl bee: dm*/ *(s + l + 1) = '\0';
707 /*drl bee: dm*/ *(s + 1) = '\0';
714 cstring_concatFree (cstring s, cstring t)
716 cstring res = cstring_concat (s, t);
723 cstring_concatFree1 (cstring s, cstring t)
725 cstring res = cstring_concat (s, t);
731 cstring_concatChars (cstring s, char *t)
733 cstring res = cstring_concat (s, cstring_fromChars (t));
739 cstring_concatLength (cstring s1, char *s2, size_t len) /*@requires maxSet(s2) >= (len - 1) @*/
741 cstring tmp = cstring_copyLength (s2, len);
742 cstring res = cstring_concat (s1, tmp);
750 cstring_concat (cstring s, cstring t) /*@requires maxSet(s) >= 0 @*/
752 char *ret = mstring_create (cstring_length (s) + cstring_length (t));
754 if (cstring_isDefined (s))
756 /*drl bee: sl*/ strcpy (ret, s);
758 if (cstring_isDefined (t))
766 /*@notnull@*/ /*@only@*/ cstring
767 cstring_prependCharO (char c, /*@only@*/ cstring s1)
769 cstring res = cstring_prependChar (c, s1);
775 /*@notnull@*/ /*@only@*/ cstring
776 cstring_prependChar (char c, /*@temp@*/ cstring s1)
778 size_t l = cstring_length (s1);
779 char *s = (char *) dmalloc (sizeof (*s) * (l + 2));
781 /*drl bee: dm*/ *(s) = c;
783 if (cstring_isDefined (s1))
785 /*@-mayaliasunique@*/
787 /*@=mayaliasunique@*/
790 /*drl bee: dm*/ *(s + l + 1) = '\0';
795 cstring_hasNonAlphaNumBar (cstring s)
799 if (cstring_isUndefined (s)) return FALSE;
801 /*drl bee: lhnt*/ while ((c = (int) *s) != (int) '\0')
803 if ((isalnum (c) == 0) && (c != (int) '_')
804 && (c != (int) '.') && (c != (int) CONNECTCHAR))
814 /*@only@*/ /*@notnull@*/ cstring
815 cstring_create (size_t n)
817 char *s = dmalloc (sizeof (*s) * (n + 1));
819 /*drl bee: dm*/ *s = '\0';
823 /*@only@*/ /*@notnull@*/ cstring
824 cstring_copySegment (cstring s, size_t findex, size_t tindex)
826 cstring res = cstring_create (tindex - findex + 1);
828 llassert (cstring_isDefined (s));
829 llassert (cstring_length (s) > tindex);
831 strncpy (res, (s + findex), size_fromInt (size_toInt (tindex - findex) + 1));
835 lsymbol cstring_toSymbol (cstring s)
837 lsymbol res = lsymbol_fromString (s);
843 cstring cstring_bsearch (cstring key, char **table, int nentries)
845 if (cstring_isDefined (key))
849 int mid = (high + low + 1) / 2;
851 cstring res = cstring_undefined;
853 while (low <= high && mid < nentries)
857 llassert (mid != last);
858 llassert (mid >= 0 && mid < nentries);
860 cmp = cstring_compare (key, table[mid]);
867 else if (cmp < 0) /* key is before table[mid] */
871 else /* key of after table[mid] */
877 mid = (high + low + 1) / 2;
880 if (mid != 0 && mid < nentries - 1)
882 llassert (cstring_compare (key, table[mid - 1]) > 0);
883 /*drl bee: ndv*/ llassert (cstring_compare (key, table[mid + 1]) < 0);
889 return cstring_undefined;
892 extern /*@observer@*/ cstring cstring_advanceWhiteSpace (cstring s)
894 if (cstring_isDefined (s)) {
897 /*drl bee: lhnt*/ while (*t != '\0' && isspace ((int) *t)) {
904 return cstring_undefined;
908 /*@ignore@*/ /* !!! DRL don't ignore large segments like this without a good reason! */
910 /* changes strings like "sdf" "sdfsd" into "sdfsdfsd"*/
911 /* This function understands that "sdf\" \"sdfsdf" is okay*/
912 static mstring doMergeString (cstring s)
919 llassert(cstring_isDefined (s));
921 ret = mstring_create (cstring_length(s) );
927 llassert(*ptr == '\"');
946 /* case of escaped \ ('\\') */
949 else if ( (*ptr == '\"') && (!escape) )
951 while ( (ptr[1] != '\"') && (ptr[1] != '\0') )
957 llassert(*ptr == '\"');
982 static mstring doExpandEscapes (cstring s, /*@out@*/ size_t *len)
988 llassert(cstring_isDefined (s));
990 ret = mstring_create (cstring_length(s));
1010 /*not a legal escape sequence but try to handle it in a sesible way*/
1015 /* Handle Octal escapes */
1016 else if (*ptr >= '0' && *ptr <= '9' )
1019 total = (int)(*ptr - '0');
1021 /*octal can only be 3 characters long */
1022 if (*ptr != '\0' && (*ptr >= '0' && *ptr <= '9' ) )
1026 if (*ptr != '\0' && (*ptr >= '0' && *ptr <= '9' ) )
1029 total += (int) (*ptr - '0');
1034 *retPtr = (char) total;
1038 else if (*ptr == 'x')
1043 if (!(*ptr != '\0' &&
1044 ( (*ptr >= '0' && *ptr <= '9' ) ||
1045 (toupper(*ptr) >= (int)('A') && toupper(*ptr) <= (int)('F') ) )
1052 while (*ptr != '\0' &&
1053 ( (*ptr >= '0' && *ptr <= '9' ) ||
1054 (toupper(*ptr) >= ((int)('A')) && toupper(*ptr) <= ((int)'F') ) )
1058 if (*ptr >= '0' && *ptr <= '9' )
1059 total += (int)(*ptr - '0');
1061 total += ( (toupper(*ptr) - 'A') + 10);
1065 *retPtr = (char) total;
1075 /*@switchbreak@*/ break;
1080 /*@switchbreak@*/ break;
1085 /*@switchbreak@*/ break;
1090 /*@switchbreak@*/ break;
1095 /*@switchbreak@*/ break;
1100 /*@switchbreak@*/ break;
1102 /* we assume invalid sequences are handled somewhere else
1103 so we handle an invalid sequence of the form \char by replacing
1104 it with char (this is what gcc does) the C standard says a diagnostic is
1117 /* add the null character */
1120 *len = retPtr - ret;
1125 /*this function is like sctring_expandEscapses */
1126 mstring cstring_expandEscapes (cstring s)
1131 ret = doExpandEscapes (s, &len);
1135 size_t cstring_lengthExpandEscapes (cstring s)
1138 mstring tmpStr, tmpStr2;
1140 tmpStr = doMergeString (s);
1141 tmpStr2 = doExpandEscapes (tmpStr, &len);
1143 cstring_free(tmpStr);
1144 cstring_free(tmpStr2);
1149 cstring cstring_replaceChar(/*@returned@*/ cstring c, char oldChar, char newChar)
1152 llassert(oldChar != '\0');
1153 if (cstring_isUndefined(c) )
1155 llcontbug(cstring_makeLiteral("cstring_replaceChar called with undefined string"));
1160 while (*ptr != '\0')
1162 if (*ptr == oldChar)