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 116 117 118 119 120
|
/*
Copyright (C) 2009-2016 David Anderson. All Rights Reserved.
Portions Copyright 2012 SN Systems Ltd. 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.
*/
/*
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. Our code here do not behave like
strstr or strcasestr in the case of
an empty 'contained' argument: we return FALSE (this
case is not interesting for dwarfdump).
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.c
*/
#include <ctype.h>
#include <stdio.h>
#include "globals.h"
Dwarf_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;
for (; *cntnd && *ct ; ++cntnd,++ct)
{
unsigned char lct = tolower(*ct);
unsigned char tlc = tolower(*cntnd);
if (lct != tlc) {
break;
}
}
if (!*cntnd) {
/* We matched all the way to end of contained */
/* ASSERT: innerwrong = FALSE */
return TRUE;
}
if (!*ct) {
/* Ran out of container before contained,
so no future match of contained
is possible. */
return FALSE;
}
/* No match so far.
See if there is more in container to check. */
}
return FALSE;
}
#ifdef TEST
static void
test(const char *t1, const char *t2,int resexp)
{
Dwarf_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("abaaxa","x",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
|