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: 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, 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) /*@requires maxRead(s) >= n /\ maxSet(s) >= n @*/ /*@ensures maxRead(result) == n /\ maxSet(result) == 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) /*@requires maxRead(s) >= (n - 1) /\ maxSet(s) >= (n - 1) @*/
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) /*@ensures maxSet(result) == maxRead(s) /\ maxRead(result) == maxRead(s) @*/
162 if (cstring_isDefined (s))
164 return (mstring_copy (s));
168 return cstring_undefined;
172 /*@only@*/ cstring cstring_copyLength (char *s, int len) /*@requires maxSet(s) >= (len - 1) @*/
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)
216 /*@requires maxRead(snew) >= 0 /\ maxRead(old) >= 0 /\ maxRead(old) >= maxRead(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 llassert (lendiff >= 0);
231 while (*tsnew != '\0')
233 llassert (*sp != '\0');
239 while (*(sp + lendiff) != '\0')
241 *sp = *(sp + lendiff);
248 sp = strstr (s, old);
254 ** removes all chars in clist from s
257 void cstring_stripChars (cstring s, const char *clist)
259 if (cstring_isDefined (s))
262 int size = cstring_length (s);
264 for (i = 0; i < size; i++)
266 /*drl bee: is*/ char c = s[i];
268 if (strchr (clist, c) != NULL)
270 /* strip this char */
275 for (j = i; j < size; j++)
277 /*drl bee: is*/ /*drl bee: is*/ s[j] = s[j+1];
280 /*drl bee: is*/ s[size] = '\0';
287 bool cstring_contains (/*@unique@*/ cstring c, cstring sub)
289 if (cstring_isDefined (c))
291 llassert (cstring_isDefined (sub));
293 return (strstr (c, sub) != NULL);
301 static char lookLike (char c) /*@*/
303 if (c == 'I' || c == 'l')
307 else if (c == 'O' || c == 'o')
325 cmpcode cstring_genericEqual (cstring s, cstring t,
327 bool caseinsensitive,
328 bool lookalike) /*@requires maxRead(s) >= nchars /\ maxRead(t) >= nchars @*/
330 if (s == t) return CGE_SAME;
331 else if (cstring_isUndefined (s))
333 return cstring_isEmpty (t) ? CGE_SAME : CGE_DISTINCT;
335 else if (cstring_isUndefined (t))
337 return cstring_isEmpty (s) ? CGE_SAME : CGE_DISTINCT;
342 bool diffcase = FALSE;
343 bool difflookalike = FALSE;
347 if (nchars > 0 && i >= nchars)
354 ; /* no difference */
356 else if (caseinsensitive
357 && (toupper ((int) *t) == toupper ((int) *s)))
361 else if (lookalike && (lookLike (*t) == lookLike (*s)))
363 difflookalike = TRUE;
376 if (*s == '\0' && *t != '\0')
385 else if (difflookalike)
387 return CGE_LOOKALIKE;
398 bool cstring_equalFree (/*@only@*/ cstring c1, /*@only@*/ cstring c2)
400 bool res = cstring_equal (c1, c2);
406 bool cstring_equal (cstring c1, cstring c2)
408 if (c1 == c2) return TRUE;
409 else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2);
410 else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1);
411 else return (strcmp (c1, c2) == 0);
414 bool cstring_equalLen (cstring c1, cstring c2, int len)
416 if (c1 == c2) return TRUE;
417 else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2);
418 else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1);
419 else return (strncmp (c1, c2, size_fromInt (len)) == 0);
422 bool cstring_equalCaseInsensitive (cstring c1, cstring c2)
424 if (c1 == c2) return TRUE;
425 else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2);
426 else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1);
427 else return (cstring_genericEqual (c1, c2, 0, TRUE, FALSE) != CGE_DISTINCT);
430 bool cstring_equalLenCaseInsensitive (cstring c1, cstring c2, int len)
434 if (c1 == c2) return TRUE;
435 else if (cstring_isUndefined (c1)) return cstring_isEmpty (c2);
436 else if (cstring_isUndefined (c2)) return cstring_isEmpty (c1);
437 else return (cstring_genericEqual (c1, c2, len, TRUE, FALSE) != CGE_DISTINCT);
440 bool cstring_equalPrefix (cstring c1, cstring c2)
442 llassert (c2 != NULL);
444 if (cstring_isUndefined (c1))
446 return (strlen (c2) == 0);
449 return (strncmp (c1, c2, strlen (c2)) == 0);
452 bool cstring_equalPrefixLit (cstring c1, const char *c2)
454 llassert (c2 != NULL);
456 if (cstring_isUndefined (c1))
458 return (strlen (c2) == 0);
461 return (strncmp (c1, c2, strlen (c2)) == 0);
464 int cstring_xcompare (cstring *c1, cstring *c2)
466 return (cstring_compare (*c1, *c2));
469 int cstring_compare (cstring c1, cstring c2)
477 else if (cstring_isUndefined (c1))
479 if (cstring_isEmpty (c2))
488 else if (cstring_isUndefined (c2))
490 if (cstring_isEmpty (c1))
501 res = strcmp (c1, c2);
507 void cstring_markOwned (/*@owned@*/ cstring s)
512 void cstring_free (/*@only@*/ cstring s)
514 if (cstring_isDefined (s))
520 cstring cstring_fromChars (/*@exposed@*/ const char *cp)
525 /*@exposed@*/ char *cstring_toCharsSafe (cstring s)
527 static /*@only@*/ cstring emptystring = cstring_undefined;
529 if (cstring_isDefined (s))
535 if (cstring_isUndefined (emptystring))
537 emptystring = cstring_newEmpty ();
544 int cstring_length (cstring s)
546 if (cstring_isDefined (s))
548 return size_toInt (strlen (s));
554 cstring_capitalize (cstring s) /*@requires maxSet(s) >= 0 @*/
556 if (!cstring_isEmpty (s))
558 cstring ret = cstring_copy (s);
560 cstring_setChar (ret, 1, (char) toupper ((int) cstring_firstChar (ret)));
564 return cstring_undefined;
568 cstring_capitalizeFree (cstring s) /*@requires maxSet(s) >= 0 /\ maxRead(s) >= 0 @*/
570 if (!cstring_isEmpty (s))
572 cstring_setChar (s, 1, (char) toupper ((int) cstring_firstChar (s)));
580 cstring_clip (cstring s, int len)
582 if (cstring_isUndefined (s) || cstring_length (s) <= len)
588 llassert (s != NULL);
589 /*drl bee: mrms*/ *(s + len) = '\0';
596 cstring_elide (cstring s, int len)
598 if (cstring_isUndefined (s) || cstring_length (s) <= len)
600 return cstring_copy (s);
604 cstring sc = cstring_create (len);
606 strncpy (sc, s, size_fromInt (len));
607 /*drl bee: mrms*/ *(sc + len - 1) = '\0';
608 *(sc + len - 2) = '.';
609 *(sc + len - 3) = '.';
610 *(sc + len - 4) = '.';
616 cstring_fill (cstring s, int n) /*@requires n >= 0 @*/
618 cstring t = cstring_create (n + 1);
620 int len = cstring_length (s);
625 for (i = 0; i < n; i++)
635 for (i = 0; i < len; i++)
641 for (i = 0; i < n - len; i++)
653 cstring_downcase (cstring s)
655 if (cstring_isDefined (s))
657 cstring t = cstring_create (strlen (s) + 1);
661 /*drl bee: lhnt*/ while ((c = *s) != '\0')
663 if (c >= 'A' && c <= 'Z')
670 /*drl bee: is*/ *t = '\0';
676 return cstring_undefined;
680 /*@notnull@*/ cstring
681 cstring_appendChar (/*@only@*/ cstring s1, char c)
683 int l = cstring_length (s1);
686 s = (char *) dmalloc (sizeof (*s) * (l + 2));
688 if (cstring_isDefined (s1))
692 /*drl bee: dm*/ *(s + l + 1) = '\0';
698 /*drl bee: dm*/ *(s + 1) = '\0';
705 cstring_concatFree (cstring s, cstring t)
707 cstring res = cstring_concat (s, t);
714 cstring_concatFree1 (cstring s, cstring t)
716 cstring res = cstring_concat (s, t);
723 cstring_concatChars (cstring s, char *t)
725 cstring res = cstring_concat (s, cstring_fromChars (t));
732 cstring_concatLength (cstring s1, char *s2, int len) /*@requires maxSet(s2) >= (len - 1) @*/
734 cstring tmp = cstring_copyLength (s2, len);
735 cstring res = cstring_concat (s1, tmp);
743 cstring_concat (cstring s, cstring t) /*@requires maxSet(s) >= 0 @*/
745 char *ret = mstring_create (cstring_length (s) + cstring_length (t));
747 if (cstring_isDefined (s))
749 /*drl bee: sl*/ strcpy (ret, s);
751 if (cstring_isDefined (t))
759 /*@notnull@*/ /*@only@*/ cstring
760 cstring_prependCharO (char c, /*@only@*/ cstring s1)
762 cstring res = cstring_prependChar (c, s1);
768 /*@notnull@*/ /*@only@*/ cstring
769 cstring_prependChar (char c, /*@temp@*/ cstring s1)
771 int l = cstring_length (s1);
772 char *s = (char *) dmalloc (sizeof (*s) * (l + 2));
774 /*drl bee: dm*/ *(s) = c;
776 if (cstring_isDefined (s1))
778 /*@-mayaliasunique@*/
780 /*@=mayaliasunique@*/
783 /*drl bee: dm*/ *(s + l + 1) = '\0';
789 cstring_hasNonAlphaNumBar (cstring s)
793 if (cstring_isUndefined (s)) return FALSE;
795 /*drl bee: lhnt*/ while ((c = (int) *s) != (int) '\0')
797 if ((isalnum (c) == 0) && (c != (int) '_')
798 && (c != (int) '.') && (c != (int) CONNECTCHAR))
809 /*@only@*/ /*@notnull@*/ cstring
810 cstring_create (int n)
812 char *s = dmalloc (sizeof (*s) * (n + 1));
814 /*drl bee: dm*/ *s = '\0';
818 /*@only@*/ /*@notnull@*/ cstring
819 cstring_copySegment (cstring s, int findex, int tindex)
821 cstring res = cstring_create (tindex - findex + 1);
823 llassert (cstring_isDefined (s));
824 llassert (cstring_length (s) > tindex);
826 strncpy (res, (s + findex), size_fromInt ((tindex - findex + 1)));
831 lsymbol cstring_toSymbol (cstring s)
833 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 /*drl bee: ndv*/ 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 /*drl bee: lhnt*/ while (*t != '\0' && isspace ((int) *t)) {
901 return cstring_undefined;
904 static mstring doExpandEscapes (cstring s, /*@out@*/ int * len)
911 llassert(cstring_isDefined (s));
913 ret = mstring_create (cstring_length(s) );
933 /*not a legal escape sequence but try to handle it in a sesible way*/
938 /* Handle Octal escapes */
939 else if (*ptr >= '0' && *ptr <= '9' )
942 total = (int)(*ptr - '0');
944 /*octal can only be 3 characters long */
945 if (*ptr != '\0' && (*ptr >= '0' && *ptr <= '9' ) )
949 if (*ptr != '\0' && (*ptr >= '0' && *ptr <= '9' ) )
952 total += (int) (*ptr - '0');
957 *retPtr = (char) total;
961 else if (*ptr == 'x')
966 if (!(*ptr != '\0' &&
967 ( (*ptr >= '0' && *ptr <= '9' ) ||
968 (toupper(*ptr) >= (int)('A') && toupper(*ptr) <= (int)('F') ) )
975 while (*ptr != '\0' &&
976 ( (*ptr >= '0' && *ptr <= '9' ) ||
977 (toupper(*ptr) >= ((int)('A')) && toupper(*ptr) <= ((int)'F') ) )
981 if (*ptr >= '0' && *ptr <= '9' )
982 total += (int)(*ptr - '0');
984 total += ( (toupper(*ptr) - 'A') + 10);
988 *retPtr = (char) total;
998 /*@switchbreak@*/ break;
1003 /*@switchbreak@*/ break;
1008 /*@switchbreak@*/ break;
1013 /*@switchbreak@*/ break;
1018 /*@switchbreak@*/ break;
1023 /*@switchbreak@*/ break;
1025 /* we assume invalid sequences are handled somewhere else
1026 so we handle an invalid sequence of the form \char by replacing
1027 it with char (this is what gcc does) the C standard says a diagnostic is
1040 /* add the null character */
1043 *len = retPtr - ret;
1048 /*this function is like sctring_expandEscapses */
1049 mstring cstring_expandEscapes (cstring s)
1055 ret = doExpandEscapes (s, &len);
1059 int cstring_lengthExpandEscapes (cstring s)
1065 tmpStr = doExpandEscapes (s, &len);
1067 cstring_free(tmpStr);