Debian Codegraph @

src/bin/dwarfdump/dd_strstrnocase.c

raw
1 /*
2 Copyright (C) 2009-2016 David Anderson. All Rights Reserved.
3 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of version 2 of the GNU General
7 Public License as published by the Free Software Foundation.
8
9 This program is distributed in the hope that it would be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE.
13
14 Further, this software is distributed without any warranty
15 that it is free of the rightful claim of any third person
16 regarding infringement or the like. Any license provided
17 herein, whether implied or otherwise, applies only to this
18 software file. Patent licenses, if any, provided herein
19 do not apply to combinations of this program with other
20 software, or any other product whatsoever.
21
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write the Free
24 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 Boston MA 02110-1301, USA.
26
27 */
28
29 /*
30 This tries to find the string 'contained' in the
31 string 'container'. it returns true if found, else false.
32 The comparisons are independent of case.
33
34 Regrettably there is no generally accepted version that
35 does this job, though GNU Linux has strcasestr() which
36 does what we need. Our code here do not behave like
37 strstr or strcasestr in the case of
38 an empty 'contained' argument: we return FALSE (this
39 case is not interesting for dwarfdump).
40
41 There is a public domain stristr().
42 But given that dwarfdump is GPL,
43 it would seem (IANAL) that we cannot mix public domain code
44 into the release.
45
46 The software here is independently written and indeed trivial.
47
48 The POSIX function tolower() is only properly
49 defined on unsigned char, hence
50 the ugly casts.
51
52 strstrnocase.c
53
54 */
55
56 #include <config.h>
57
58 #include <ctype.h> /* tolower() */
59
60 #include "dwarf.h"
61 #include "libdwarf.h"
62 #include "dd_defined_types.h"
63 #include "dd_checkutil.h"
64 #include "dd_glflags.h"
65 #include "dd_globals.h"
66
67 Dwarf_Bool
68 is_strstrnocase(const char * container, const char * contained)
69 {
70 const unsigned char *ct = (const unsigned char *)container;
71 for (; *ct; ++ct )
72 {
73 const unsigned char * cntnd =
74 (const unsigned char *)contained;
75
76 for (; *cntnd && *ct ; ++cntnd,++ct)
77 {
78 unsigned char lct = tolower(*ct);
79 unsigned char tlc = tolower(*cntnd);
80 if (lct != tlc) {
81 break;
82 }
83 }
84 if (!*cntnd) {
85 /* We matched all the way to end of contained */
86 /* ASSERT: innerwrong = FALSE */
87 return TRUE;
88 }
89 if (!*ct) {
90 /* Ran out of container before contained,
91 so no future match of contained
92 is possible. */
93 return FALSE;
94
95 }
96 /* No match so far.
97 See if there is more in container to check. */
98 }
99 return FALSE;
100 }
101
102 #ifdef TEST
103 static void
104 test(const char *t1, const char *t2,int resexp)
105 {
106 Dwarf_Bool res = is_strstrnocase(t1,t2);
107 if (res == resexp) {
108 return;
109 }
110 printf("Error,mismatch %s and %s. Expected %d got %d\n",
111 t1,t2,resexp,res);
112 }
113
114 int main()
115 {
116 test("aaaaa","a",1);
117 test("aaaaa","b",0);
118 test("abaaba","ba",1);
119 test("abaaxa","x",1);
120 test("abaabA","Ba",1);
121 test("a","ab",0);
122 test("b","c",0);
123 test("b","",0);
124 test("","c",0);
125 test("","",0);
126 test("aaaaa","aaaaaaaa",0);
127 }
128 #endif