]> andersk Git - splint.git/blame - src/cstring.c
Fixed problem with identifyFlag revealed by splint -help flags full
[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
53char cstring_getChar (cstring s, int n)
54{
55 int length = cstring_length (s);
56
57 llassert (cstring_isDefined (s));
58 llassert (n >= 1 && n <= length);
59
60 return (s[n - 1]);
61}
62
63cstring cstring_suffix (cstring s, int n)
64{
65 llassert (cstring_isDefined (s));
66 llassert (n <= cstring_length (s));
67
68 return (s + n);
69}
70
86d93ed3 71cstring cstring_prefix (cstring s, int n) /*@requires maxRead(s) >= n /\ maxSet(s) >= n @*/ /*@ensures maxRead(result) == n /\ maxSet(result) == n @*/
616915dd 72{
73 cstring t;
74 char c;
75 llassert (cstring_isDefined (s));
76 llassert (n <= cstring_length (s));
77
78 c = *(s + n);
79 /*@-mods@*/ /* The modifications cancel out. */
80 *(s + n) = '\0';
81 t = cstring_copy (s);
82 *(s + n) = c;
83 /*@=mods@*/
84
85 return t;
86}
87
88/* effects If s = [0-9]*, returns s as an int.
89** else returns -1.
90*/
91
92int cstring_toPosInt (cstring s)
93{
94 int val = 0;
95
96 cstring_chars (s, c)
97 {
98 if (isdigit ((unsigned char) c))
99 {
100 val = (val * 10) + (int)(c - '0');
101 }
102 else
103 {
104 return -1;
105 }
106 } end_cstring_chars ;
107
108 return val;
109}
110
68de3f33 111cstring cstring_afterChar (cstring s, char c)
112{
113 llassert (cstring_isDefined (s));
114 return strchr (s, c);
115}
116
616915dd 117cstring cstring_beforeChar (cstring s, char c)
118{
119 if (cstring_isDefined (s))
120 {
121 char *cp = strchr (s, c);
122
123 if (cp != NULL)
124 {
125 cstring ret;
126
127 /*@-mods@*/
128 *cp = '\0';
129 ret = cstring_copy (s);
130 *cp = c;
131 /*@=mods@*/ /* modification is undone */
132
133 return ret;
134 }
135 }
136
137 return cstring_undefined;
138}
139
86d93ed3 140void cstring_setChar (cstring s, int n, char c) /*@requires maxRead(s) >= (n - 1) /\ maxSet(s) >= (n - 1) @*/
616915dd 141{
142 llassert (cstring_isDefined (s));
143 llassert (n > 0 && n <= cstring_length (s));
144
145 s[n - 1] = c;
146}
147
148char cstring_lastChar (cstring s)
149{
150 int length;
151
152 llassert (cstring_isDefined (s));
153
154 length = cstring_length (s);
155 llassert (length > 0);
156
157 return (s[length - 1]);
158}
159
86d93ed3 160/*@only@*/ cstring cstring_copy (cstring s) /*@ensures maxSet(result) == maxRead(s) /\ maxRead(result) == maxRead(s) @*/
616915dd 161{
162 if (cstring_isDefined (s))
163 {
164 return (mstring_copy (s));
165 }
166 else
167 {
168 return cstring_undefined;
169 }
170}
171
86d93ed3 172/*@only@*/ cstring cstring_copyLength (char *s, int len) /*@requires maxSet(s) >= (len - 1) @*/
616915dd 173{
174 char *res = mstring_create (len + 1);
175
176 strncpy (res, s, size_fromInt (len));
177 res[len] = '\0';
178 return res;
179}
180
181bool cstring_containsChar (cstring c, char ch)
182{
183 if (cstring_isDefined (c))
184 {
185 return (strchr (c, ch) != NULL);
186 }
187 else
188 {
189 return FALSE;
190 }
191}
192
193/*
194** Replaces all occurances of old in s with new.
195*/
196
616915dd 197void cstring_replaceAll (cstring s, char old, char snew)
198{
199
200 llassert (old != snew);
201
202 if (cstring_isDefined (s))
203 {
204 char *sp = strchr (s, old);
205
206 while (sp != NULL)
207 {
208 *sp = snew;
209 sp = strchr (sp, old);
210 }
211
212 }
213}
616915dd 214
92b2ba2c 215void cstring_replaceLit (/*@unique@*/ cstring s, char *old, char *snew)
216 /*@requires maxRead(snew) >= 0 /\ maxRead(old) >= 0 /\ maxRead(old) >= maxRead(snew) @*/
616915dd 217{
616915dd 218 llassert (strlen (old) >= strlen (snew));
92b2ba2c 219
616915dd 220 if (cstring_isDefined (s))
221 {
222 char *sp = strstr (s, old);
223
224 while (sp != NULL)
225 {
226 int lendiff = size_toInt (strlen (old) - strlen (snew));
227 char *tsnew = snew;
92b2ba2c 228
229 llassert (lendiff >= 0);
230
616915dd 231 while (*tsnew != '\0')
232 {
92b2ba2c 233 llassert (*sp != '\0');
616915dd 234 *sp++ = *tsnew++;
235 }
92b2ba2c 236
616915dd 237 if (lendiff > 0)
238 {
239 while (*(sp + lendiff) != '\0')
240 {
241 *sp = *(sp + lendiff);
242 sp++;
243 }
dcdc46c8 244
616915dd 245 *sp = '\0';
246 }
247
248 sp = strstr (s, old);
249 }
dcdc46c8 250 }
616915dd 251}
252
253/*
254** removes all chars in clist from s
255*/
256
257void cstring_stripChars (cstring s, const char *clist)
258{
259 if (cstring_isDefined (s))
260 {
261 int i;
262 int size = cstring_length (s);
263
264 for (i = 0; i < size; i++)
265 {
86d93ed3 266/*drl bee: is*/ char c = s[i];
616915dd 267
268 if (strchr (clist, c) != NULL)
269 {
270 /* strip this char */
271 int j;
272
273 size--;
274
275 for (j = i; j < size; j++)
276 {
86d93ed3 277 /*drl bee: is*/ /*drl bee: is*/ s[j] = s[j+1];
616915dd 278 }
279
86d93ed3 280 /*drl bee: is*/ s[size] = '\0';
616915dd 281 i--;
282 }
283 }
284 }
285}
286
287bool cstring_contains (/*@unique@*/ cstring c, cstring sub)
288{
289 if (cstring_isDefined (c))
290 {
291 llassert (cstring_isDefined (sub));
292
293 return (strstr (c, sub) != NULL);
294 }
295 else
296 {
297 return FALSE;
298 }
299}
300
301static char lookLike (char c) /*@*/
302{
303 if (c == 'I' || c == 'l')
304 {
305 return '1';
306 }
307 else if (c == 'O' || c == 'o')
308 {
309 return '0';
310 }
311 else if (c == 'Z')
312 {
313 return '2';
314 }
315 else if (c == 'S')
316 {
317 return '5';
318 }
319 else
320 {
321 return c;
322 }
323}
324
325cmpcode cstring_genericEqual (cstring s, cstring t,
326 int nchars,
327 bool caseinsensitive,
86d93ed3 328 bool lookalike) /*@requires maxRead(s) >= nchars /\ maxRead(t) >= nchars @*/
616915dd 329{
330 if (s == t) return CGE_SAME;
331 else if (cstring_isUndefined (s))
332 {
333 return cstring_isEmpty (t) ? CGE_SAME : CGE_DISTINCT;
334 }
335 else if (cstring_isUndefined (t))
336 {
337 return cstring_isEmpty (s) ? CGE_SAME : CGE_DISTINCT;
338 }
339 else
340 {
341 int i = 0;
342 bool diffcase = FALSE;
343 bool difflookalike = FALSE;
344
345 while (*s != '\0')
346 {
347 if (nchars > 0 && i >= nchars)
348 {
349 break;
350 }
351
352 if (*t == *s)
353 {
354 ; /* no difference */
355 }
356 else if (caseinsensitive
357 && (toupper ((int) *t) == toupper ((int) *s)))
358 {
359 diffcase = TRUE;
360 }
361 else if (lookalike && (lookLike (*t) == lookLike (*s)))
362 {
363 difflookalike = TRUE;
364 }
365 else
366 {
367 return CGE_DISTINCT;
368 }
369 i++;
370 s++;
371 t++;
372 }
373
86d93ed3 374 /*drl bee: ib*/
375 /*drl bee: ib*/
616915dd 376 if (*s == '\0' && *t != '\0')
377 {
378 return CGE_DISTINCT;
379 }
380
381 if (diffcase)
382 {
383 return CGE_CASE;
384 }
385 else if (difflookalike)
386 {
387 return CGE_LOOKALIKE;
388 }
389 else
390 {
391 return CGE_SAME;
392 }
393 }
394}
395
396
397
398bool cstring_equalFree (/*@only@*/ cstring c1, /*@only@*/ cstring c2)
399{
400 bool res = cstring_equal (c1, c2);
401 cstring_free (c1);
402 cstring_free (c2);
403 return res;
404}
405
406bool cstring_equal (cstring c1, cstring c2)
407{
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);
412}
413
414bool cstring_equalLen (cstring c1, cstring c2, int len)
415{
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);
420}
421
422bool cstring_equalCaseInsensitive (cstring c1, cstring c2)
423{
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);
428}
429
430bool cstring_equalLenCaseInsensitive (cstring c1, cstring c2, int len)
431{
432 llassert (len >= 0);
433
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);
438}
439
68de3f33 440bool cstring_equalPrefix (cstring c1, cstring c2)
616915dd 441{
442 llassert (c2 != NULL);
443
444 if (cstring_isUndefined (c1))
445 {
446 return (strlen (c2) == 0);
447 }
448
449 return (strncmp (c1, c2, strlen (c2)) == 0);
450}
451
68de3f33 452bool cstring_equalPrefixLit (cstring c1, const char *c2)
616915dd 453{
454 llassert (c2 != NULL);
455
456 if (cstring_isUndefined (c1))
457 {
458 return (strlen (c2) == 0);
459 }
460
616915dd 461 return (strncmp (c1, c2, strlen (c2)) == 0);
616915dd 462}
463
464int cstring_xcompare (cstring *c1, cstring *c2)
465{
466 return (cstring_compare (*c1, *c2));
467}
468
469int cstring_compare (cstring c1, cstring c2)
470{
471 int res;
472
473 if (c1 == c2)
474 {
475 res = 0;
476 }
477 else if (cstring_isUndefined (c1))
478 {
479 if (cstring_isEmpty (c2))
480 {
481 res = 0;
482 }
483 else
484 {
485 res = 1;
486 }
487 }
488 else if (cstring_isUndefined (c2))
489 {
490 if (cstring_isEmpty (c1))
491 {
492 res = 0;
493 }
494 else
495 {
496 res = -1;
497 }
498 }
499 else
500 {
501 res = strcmp (c1, c2);
502 }
503
504 return (res);
505}
506
507void cstring_markOwned (/*@owned@*/ cstring s)
508{
509 sfreeEventually (s);
510}
511
512void cstring_free (/*@only@*/ cstring s)
513{
514 if (cstring_isDefined (s))
515 {
516 sfree (s);
517 }
518}
519
520cstring cstring_fromChars (/*@exposed@*/ const char *cp)
521{
522 return (cstring) cp;
523}
524
525/*@exposed@*/ char *cstring_toCharsSafe (cstring s)
526{
527 static /*@only@*/ cstring emptystring = cstring_undefined;
528
529 if (cstring_isDefined (s))
530 {
531 return (char *) s;
532 }
533 else
534 {
535 if (cstring_isUndefined (emptystring))
536 {
537 emptystring = cstring_newEmpty ();
538 }
539
540 return emptystring;
541 }
542}
543
544int cstring_length (cstring s)
545{
546 if (cstring_isDefined (s))
547 {
548 return size_toInt (strlen (s));
549 }
550 return 0;
551}
552
553cstring
86d93ed3 554cstring_capitalize (cstring s) /*@requires maxSet(s) >= 0 @*/
616915dd 555{
556 if (!cstring_isEmpty (s))
557 {
558 cstring ret = cstring_copy (s);
559
560 cstring_setChar (ret, 1, (char) toupper ((int) cstring_firstChar (ret)));
561 return ret;
562 }
563
564 return cstring_undefined;
565}
566
567cstring
86d93ed3 568cstring_capitalizeFree (cstring s) /*@requires maxSet(s) >= 0 /\ maxRead(s) >= 0 @*/
616915dd 569{
570 if (!cstring_isEmpty (s))
571 {
572 cstring_setChar (s, 1, (char) toupper ((int) cstring_firstChar (s)));
573 return s;
574 }
575
576 return s;
577}
578
579cstring
580cstring_clip (cstring s, int len)
581{
582 if (cstring_isUndefined (s) || cstring_length (s) <= len)
583 {
584 ;
585 }
586 else
587 {
588 llassert (s != NULL);
86d93ed3 589 /*drl bee: mrms*/ *(s + len) = '\0';
616915dd 590 }
591
592 return s;
593}
594
595/*@only@*/ cstring
596cstring_elide (cstring s, int len)
597{
598 if (cstring_isUndefined (s) || cstring_length (s) <= len)
599 {
600 return cstring_copy (s);
601 }
602 else
603 {
604 cstring sc = cstring_create (len);
605
606 strncpy (sc, s, size_fromInt (len));
86d93ed3 607 /*drl bee: mrms*/ *(sc + len - 1) = '\0';
616915dd 608 *(sc + len - 2) = '.';
609 *(sc + len - 3) = '.';
610 *(sc + len - 4) = '.';
611 return sc;
612 }
613}
614
615/*@only@*/ cstring
86d93ed3 616cstring_fill (cstring s, int n) /*@requires n >= 0 @*/
616915dd 617{
618 cstring t = cstring_create (n + 1);
619 cstring ot = t;
620 int len = cstring_length (s);
621 int i;
622
623 if (len > n)
624 {
625 for (i = 0; i < n; i++)
626 {
86d93ed3 627 /*drl bee: is*/
628 /*drl bee: is*/
616915dd 629 *t++ = *s++;
630 }
631 *t = '\0';
632 }
633 else
634 {
635 for (i = 0; i < len; i++)
636 {
86d93ed3 637 /*drl bee: is*/
638/*drl bee: is*/
616915dd 639 *t++ = *s++;
640 }
641 for (i = 0; i < n - len; i++)
642 {
86d93ed3 643/*drl bee: is*/
616915dd 644 *t++ = ' ';
645 }
646 *t = '\0';
647 }
648
649 return ot;
650}
651
652cstring
653cstring_downcase (cstring s)
654{
655 if (cstring_isDefined (s))
656 {
657 cstring t = cstring_create (strlen (s) + 1);
658 cstring ot = t;
659 char c;
660
86d93ed3 661 /*drl bee: lhnt*/ while ((c = *s) != '\0')
616915dd 662 {
663 if (c >= 'A' && c <= 'Z')
664 {
665 c = c - 'A' + 'a';
666 }
667 *t++ = c;
668 s++;
669 }
86d93ed3 670 /*drl bee: is*/ *t = '\0';
616915dd 671
672 return ot;
673 }
674 else
675 {
676 return cstring_undefined;
677 }
678}
679
680/*@notnull@*/ cstring
681cstring_appendChar (/*@only@*/ cstring s1, char c)
682{
683 int l = cstring_length (s1);
684 char *s;
685
686 s = (char *) dmalloc (sizeof (*s) * (l + 2));
687
688 if (cstring_isDefined (s1))
689 {
690 strcpy (s, s1);
691 *(s + l) = c;
86d93ed3 692 /*drl bee: dm*/ *(s + l + 1) = '\0';
616915dd 693 sfree (s1);
694 }
695 else
696 {
697 *(s) = c;
4dd72714 698 /*drl bee: dm*/ *(s + 1) = '\0';
616915dd 699 }
700
701 return s;
702}
703
704/*@only@*/ cstring
705cstring_concatFree (cstring s, cstring t)
706{
707 cstring res = cstring_concat (s, t);
708 cstring_free (s);
709 cstring_free (t);
710 return res;
711}
712
713/*@only@*/ cstring
714cstring_concatFree1 (cstring s, cstring t)
715{
716 cstring res = cstring_concat (s, t);
717 cstring_free (s);
718 return res;
719}
720
721# ifndef NOLCL
722/*@only@*/ cstring
723cstring_concatChars (cstring s, char *t)
724{
725 cstring res = cstring_concat (s, cstring_fromChars (t));
726 cstring_free (s);
727 return res;
728}
729# endif
730
731/*@only@*/ cstring
86d93ed3 732cstring_concatLength (cstring s1, char *s2, int len) /*@requires maxSet(s2) >= (len - 1) @*/
616915dd 733{
734 cstring tmp = cstring_copyLength (s2, len);
735 cstring res = cstring_concat (s1, tmp);
736 cstring_free (tmp);
737 cstring_free (s1);
738
739 return res;
740}
741
742/*@only@*/ cstring
86d93ed3 743cstring_concat (cstring s, cstring t) /*@requires maxSet(s) >= 0 @*/
616915dd 744{
745 char *ret = mstring_create (cstring_length (s) + cstring_length (t));
746
747 if (cstring_isDefined (s))
748 {
86d93ed3 749 /*drl bee: sl*/ strcpy (ret, s);
616915dd 750 }
751 if (cstring_isDefined (t))
752 {
753 strcat (ret, t);
754 }
755
756 return ret;
757}
758
759/*@notnull@*/ /*@only@*/ cstring
760cstring_prependCharO (char c, /*@only@*/ cstring s1)
761{
762 cstring res = cstring_prependChar (c, s1);
763
764 cstring_free (s1);
765 return (res);
766}
767
768/*@notnull@*/ /*@only@*/ cstring
769cstring_prependChar (char c, /*@temp@*/ cstring s1)
770{
771 int l = cstring_length (s1);
772 char *s = (char *) dmalloc (sizeof (*s) * (l + 2));
773
86d93ed3 774/*drl bee: dm*/ *(s) = c;
616915dd 775
776 if (cstring_isDefined (s1))
777 {
778 /*@-mayaliasunique@*/
779 strcpy (s + 1, s1);
780 /*@=mayaliasunique@*/
781 }
782
86d93ed3 783 /*drl bee: dm*/ *(s + l + 1) = '\0';
616915dd 784 return s;
785}
786
787# ifndef NOLCL
788bool
789cstring_hasNonAlphaNumBar (cstring s)
790{
791 int c;
792
793 if (cstring_isUndefined (s)) return FALSE;
794
86d93ed3 795/*drl bee: lhnt*/ while ((c = (int) *s) != (int) '\0')
616915dd 796 {
797 if ((isalnum (c) == 0) && (c != (int) '_')
798 && (c != (int) '.') && (c != (int) CONNECTCHAR))
799 {
800 return TRUE;
801 }
802
803 s++;
804 }
805 return FALSE;
806}
807# endif
808
809/*@only@*/ /*@notnull@*/ cstring
810cstring_create (int n)
811{
812 char *s = dmalloc (sizeof (*s) * (n + 1));
813
86d93ed3 814 /*drl bee: dm*/ *s = '\0';
616915dd 815 return s;
816}
817
2934b455 818/*@only@*/ /*@notnull@*/ cstring
819cstring_copySegment (cstring s, int findex, int tindex)
820{
821 cstring res = cstring_create (tindex - findex + 1);
822
823 llassert (cstring_isDefined (s));
824 llassert (cstring_length (s) > tindex);
825
826 strncpy (res, (s + findex), size_fromInt ((tindex - findex + 1)));
827 return res;
828}
829
616915dd 830# ifndef NOLCL
831lsymbol cstring_toSymbol (cstring s)
832{
2934b455 833 lsymbol res = lsymbol_fromString (s);
616915dd 834
835 cstring_free (s);
836 return res;
837}
838# endif
839
840cstring cstring_bsearch (cstring key, char **table, int nentries)
841{
842 if (cstring_isDefined (key))
843 {
844 int low = 0;
845 int high = nentries;
846 int mid = (high + low + 1) / 2;
847 int last = -1;
848 cstring res = cstring_undefined;
849
850 while (low <= high && mid < nentries)
851 {
852 int cmp;
853
854 llassert (mid != last);
855 llassert (mid >= 0 && mid < nentries);
856
857 cmp = cstring_compare (key, table[mid]);
858
859 if (cmp == 0)
860 {
861 res = table[mid];
862 break;
863 }
864 else if (cmp < 0) /* key is before table[mid] */
865 {
866 high = mid - 1;
867 }
868 else /* key of after table[mid] */
869 {
870 low = mid + 1;
871 }
872
873 last = mid;
874 mid = (high + low + 1) / 2;
875 }
876
877 if (mid != 0 && mid < nentries - 1)
878 {
879 llassert (cstring_compare (key, table[mid - 1]) > 0);
86d93ed3 880 /*drl bee: ndv*/ llassert (cstring_compare (key, table[mid + 1]) < 0);
616915dd 881 }
882
883 return res;
884 }
885
886 return cstring_undefined;
887}
888
889extern /*@observer@*/ cstring cstring_advanceWhiteSpace (cstring s)
890{
891 if (cstring_isDefined (s)) {
892 char *t = s;
893
86d93ed3 894 /*drl bee: lhnt*/ while (*t != '\0' && isspace ((int) *t)) {
616915dd 895 t++;
896 }
897
898 return t;
899 }
900
901 return cstring_undefined;
902}
903
904
905
This page took 0.211893 seconds and 5 git commands to generate.