summaryrefslogtreecommitdiff
path: root/dwarfdump2/strstrnocase.cc
blob: 7d68ba9fc5ebe6ff0766a0c65328fc3d36e38de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
  Copyright (C) 2009-2011 David Anderson.  All Rights Reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation.

  This program is distributed in the hope that it would be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  Further, this software is distributed without any warranty that it is
  free of the rightful claim of any third person regarding infringement
  or the like.  Any license provided herein, whether implied or
  otherwise, applies only to this software file.  Patent licenses, if
  any, provided herein do not apply to combinations of this program with
  other software, or any other product whatsoever.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write the Free Software Foundation, Inc., 51
  Franklin Street - Fifth Floor, Boston MA 02110-1301, USA.

  Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
  Mountain View, CA 94043, or:

  http://www.sgi.com

  For further information regarding this notice, see:

  http://oss.sgi.com/projects/GenInfo/NoticeExplan

*/
/*  The address of the Free Software Foundation is
    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 
    Boston, MA 02110-1301, USA.  
*/



/*
This tries to find the string 'contained' in the
string 'container'.  it returns true if found, else false.
The comparisons are independent  of case.

Regrettably there is no generally accepted version that
does this job, though GNU Linux has  strcasestr() which
does what we need.  

There is a public domain stristr().    But given that dwarfdump is GPL,
it would seem (IANAL) that we cannot mix public domain code
into the release.

The software here is independently written and indeed trivial.

The POSIX function tolower() is only properly defined on unsigned char, hence
the ugly casts.

strstrnocase.cc

*/
#include <ctype.h>
#include <stdio.h>
#include <globals.h>

bool
is_strstrnocase(const char * container, const char * contained)
{
    const unsigned char *ct = (const unsigned char *)container;
    for( ; *ct; ++ct )
    {
        const unsigned char * cntnd = (const unsigned char *)contained;
        bool innerwrong = true;
        for(  ; *cntnd; ++cntnd,++ct) 
        {
            unsigned char lct = tolower(*ct);
            unsigned char tlc = tolower(*cntnd);
            if(lct != tlc) {
                innerwrong=true;
                break; /* Go to outer loop */
            }
            innerwrong=false;
        }
        if(!innerwrong) {
            return true;
        }
    }
    return false;
}

#ifdef TEST
static void
test(const  char *t1, const char *t2,int resexp)
{
    bool  res = is_strstrnocase(t1,t2);
    if (res == resexp) {
        return;
    }
    printf("Error,mismatch %s and %s.  Expected %d got %d\n",
        t1,t2,resexp,res);
}

int main()
{
    test("aaaaa","a",1);
    test("aaaaa","b",0);
    test("abaaba","ba",1);
    test("abaabA","Ba",1);
    test("a","ab",0);
    test("b","c",0);
    test("b","",0);
    test("","c",0);
    test("","",0);
    test("aaaaa","aaaaaaaa",0);
}
#endif