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++)
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;
379 if (*s == '\0' && *t != '\0')
388 else if (difflookalike)
390 return CGE_LOOKALIKE;
401 bool cstring_equalFree (/*@only@*/ cstring c1, /*@only@*/ cstring c2)
403 bool res = cstring_equal (c1, c2);
409 bool cstring_equal (cstring c1, cstring c2)
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);
417 bool cstring_equalLen (cstring c1, cstring c2, size_t len)
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 (strncmp (c1, c2, len) == 0);
425 bool cstring_equalCaseInsensitive (cstring c1, cstring c2)
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);
433 bool cstring_equalLenCaseInsensitive (cstring c1, cstring c2, size_t len)
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);
441 bool cstring_equalPrefix (cstring c1, cstring c2)
443 llassert (c2 != NULL);
445 if (cstring_isUndefined (c1))
447 return (strlen (c2) == 0);
450 return (strncmp (c1, c2, strlen (c2)) == 0);
453 bool cstring_equalPrefixLit (cstring c1, const char *c2)
455 llassert (c2 != NULL);
457 if (cstring_isUndefined (c1))
459 return (strlen (c2) == 0);
462 return (strncmp (c1, c2, strlen (c2)) == 0);
465 int cstring_xcompare (cstring *c1, cstring *c2)
467 return (cstring_compare (*c1, *c2));
470 int cstring_compare (cstring c1, cstring c2)
478 else if (cstring_isUndefined (c1))
480 if (cstring_isEmpty (c2))
489 else if (cstring_isUndefined (c2))
491 if (cstring_isEmpty (c1))
502 res = strcmp (c1, c2);
508 void cstring_markOwned (/*@owned@*/ cstring s)
513 void cstring_free (/*@only@*/ cstring s)
515 if (cstring_isDefined (s))
524 cstring cstring_fromChars (/*@exposed@*/ const char *cp)
529 /*@exposed@*/ char *cstring_toCharsSafe (cstring s)
531 static /*@only@*/ cstring emptystring = cstring_undefined;
533 if (cstring_isDefined (s))
539 if (cstring_isUndefined (emptystring))
541 emptystring = cstring_newEmpty ();
548 size_t cstring_length (cstring s)
550 if (cstring_isDefined (s))
559 cstring_capitalize (cstring s) /*@requires maxSet(s) >= 0 @*/
561 if (!cstring_isEmpty (s))
563 cstring ret = cstring_copy (s);
565 cstring_setChar (ret, 1, (char) toupper ((int) cstring_firstChar (ret)));
569 return cstring_undefined;
573 cstring_capitalizeFree (cstring s) /*@requires maxSet(s) >= 0 /\ maxRead(s) >= 0 @*/
575 if (!cstring_isEmpty (s))
577 cstring_setChar (s, 1, (char) toupper ((int) cstring_firstChar (s)));
585 cstring_clip (cstring s, size_t len)
587 if (cstring_isUndefined (s) || cstring_length (s) <= len)
593 llassert (s != NULL);
602 cstring_elide (cstring s, size_t len)
604 if (cstring_isUndefined (s) || cstring_length (s) <= len)
606 return cstring_copy (s);
610 cstring sc = cstring_create (len);
612 strncpy (sc, s, len);
614 *(sc + len - 1) = '\0';
615 *(sc + len - 2) = '.';
616 *(sc + len - 3) = '.';
617 *(sc + len - 4) = '.';
624 cstring_fill (cstring s, size_t n) /*@requires n >= 0 @*/
626 cstring t = cstring_create (n + 1);
628 size_t len = cstring_length (s);
633 for (i = 0; i < n; i++)
642 for (i = 0; i < len; i++)
647 for (i = 0; i < n - len; i++)
659 cstring_downcase (cstring s)
661 if (cstring_isDefined (s))
663 cstring t = cstring_create (strlen (s) + 1);
667 while ((c = *s) != '\0')
669 if (c >= 'A' && c <= 'Z')
682 return cstring_undefined;
686 /*@notnull@*/ cstring
687 cstring_appendChar (/*@only@*/ cstring s1, char c)
689 size_t l = cstring_length (s1);
692 s = (char *) dmalloc (sizeof (*s) * (l + 2));
694 if (cstring_isDefined (s1))
711 cstring_concatFree (cstring s, cstring t)
713 cstring res = cstring_concat (s, t);
720 cstring_concatFree1 (cstring s, cstring t)
722 cstring res = cstring_concat (s, t);
728 cstring_concatChars (cstring s, char *t)
730 cstring res = cstring_concat (s, cstring_fromChars (t));
736 cstring_concatLength (cstring s1, char *s2, size_t len) /*@requires maxSet(s2) >= (len - 1) @*/
738 cstring tmp = cstring_copyLength (s2, len);
739 cstring res = cstring_concat (s1, tmp);
747 cstring_concat (cstring s, cstring t) /*@requires maxSet(s) >= 0 @*/
749 char *ret = mstring_create (cstring_length (s) + cstring_length (t));
751 if (cstring_isDefined (s))
755 if (cstring_isDefined (t))
763 /*@notnull@*/ /*@only@*/ cstring
764 cstring_prependCharO (char c, /*@only@*/ cstring s1)
766 cstring res = cstring_prependChar (c, s1);
772 /*@notnull@*/ /*@only@*/ cstring
773 cstring_prependChar (char c, /*@temp@*/ cstring s1)
775 size_t l = cstring_length (s1);
776 char *s = (char *) dmalloc (sizeof (*s) * (l + 2));
780 if (cstring_isDefined (s1))
782 /*@-mayaliasunique@*/
784 /*@=mayaliasunique@*/
792 cstring_hasNonAlphaNumBar (cstring s)
796 if (cstring_isUndefined (s)) return FALSE;
798 while ((c = (int) *s) != (int) '\0')
800 if ((isalnum (c) == 0) && (c != (int) '_')
801 && (c != (int) '.') && (c != (int) CONNECTCHAR))
811 /*@only@*/ /*@notnull@*/ cstring
812 cstring_create (size_t n)
814 char *s = dmalloc (sizeof (*s) * (n + 1));
820 /*@only@*/ /*@notnull@*/ cstring
821 cstring_copySegment (cstring s, size_t findex, size_t tindex)
823 cstring res = cstring_create (tindex - findex + 1);
825 llassert (cstring_isDefined (s));
826 llassert (cstring_length (s) > tindex);
828 strncpy (res, (s + findex), size_fromInt (size_toInt (tindex - findex) + 1));
832 lsymbol cstring_toSymbol (cstring s)
834 lsymbol res = lsymbol_fromString (s);
840 cstring cstring_bsearch (cstring key, char **table, int nentries)
842 if (cstring_isDefined (key))
846 int mid = (high + low + 1) / 2;
848 cstring res = cstring_undefined;
850 while (low <= high && mid < nentries)
854 llassert (mid != last);
855 llassert (mid >= 0 && mid < nentries);
857 cmp = cstring_compare (key, table[mid]);
864 else if (cmp < 0) /* key is before table[mid] */
868 else /* key of after table[mid] */
874 mid = (high + low + 1) / 2;
877 if (mid != 0 && mid < nentries - 1)
879 llassert (cstring_compare (key, table[mid - 1]) > 0);
880 llassert (cstring_compare (key, table[mid + 1]) < 0);
886 return cstring_undefined;
889 extern /*@observer@*/ cstring cstring_advanceWhiteSpace (cstring s)
891 if (cstring_isDefined (s)) {
894 while (*t != '\0' && isspace ((int) *t)) {
901 return cstring_undefined;
904 /* changes strings like "sdf" "sdfsd" into "sdfsdfsd"*/
905 /* This function understands that "sdf\" \"sdfsdf" is okay*/
906 static mstring doMergeString (cstring s)
913 llassert(cstring_isDefined (s));
915 ret = mstring_create (cstring_length(s) );
921 llassert(*ptr == '\"');
940 /* case of escaped \ ('\\') */
943 else if ( (*ptr == '\"') && (!escape) )
945 while ( (ptr[1] != '\"') && (ptr[1] != '\0') )
951 llassert(*ptr == '\"');
976 static mstring doExpandEscapes (cstring s, /*@out@*/ size_t *len)
982 llassert(cstring_isDefined (s));
984 ret = mstring_create (cstring_length(s));
1004 /*not a legal escape sequence but try to handle it in a sesible way*/
1009 /* Handle Octal escapes */
1010 else if (*ptr >= '0' && *ptr <= '9' )
1013 total = (int)(*ptr - '0');
1015 /*octal can only be 3 characters long */
1016 if (*ptr != '\0' && (*ptr >= '0' && *ptr <= '9' ) )
1020 if (*ptr != '\0' && (*ptr >= '0' && *ptr <= '9' ) )
1023 total += (int) (*ptr - '0');
1028 *retPtr = (char) total;
1032 else if (*ptr == 'x')
1037 if (!(*ptr != '\0' &&
1038 ( (*ptr >= '0' && *ptr <= '9' ) ||
1039 (toupper(*ptr) >= (int)('A') && toupper(*ptr) <= (int)('F') ) )
1046 while (*ptr != '\0' &&
1047 ( (*ptr >= '0' && *ptr <= '9' ) ||
1048 (toupper(*ptr) >= ((int)('A')) && toupper(*ptr) <= ((int)'F') ) )
1052 if (*ptr >= '0' && *ptr <= '9' )
1053 total += (int)(*ptr - '0');
1055 total += ( (toupper(*ptr) - 'A') + 10);
1059 *retPtr = (char) total;
1069 /*@switchbreak@*/ break;
1074 /*@switchbreak@*/ break;
1079 /*@switchbreak@*/ break;
1084 /*@switchbreak@*/ break;
1089 /*@switchbreak@*/ break;
1094 /*@switchbreak@*/ break;
1096 /* we assume invalid sequences are handled somewhere else
1097 so we handle an invalid sequence of the form \char by replacing
1098 it with char (this is what gcc does) the C standard says a diagnostic is
1111 /* add the null character */
1114 *len = retPtr - ret;
1119 /*this function is like sctring_expandEscapses */
1120 mstring cstring_expandEscapes (cstring s)
1125 ret = doExpandEscapes (s, &len);
1129 size_t cstring_lengthExpandEscapes (cstring s)
1132 mstring tmpStr, tmpStr2;
1134 tmpStr = doMergeString (s);
1135 tmpStr2 = doExpandEscapes (tmpStr, &len);
1137 cstring_free(tmpStr);
1138 cstring_free(tmpStr2);
1143 cstring cstring_replaceChar(/*@returned@*/ cstring c, char oldChar, char newChar)
1146 llassert(oldChar != '\0');
1147 if (cstring_isUndefined(c) )
1149 llcontbug(cstring_makeLiteral("cstring_replaceChar called with undefined string"));
1154 while (*ptr != '\0')
1156 if (*ptr == oldChar)