1
/* $Id: strcasestr.c,v 1.1.2.1 2008/03/20 21:42:39 joerg_wunsch Exp $ */
4
# define _GNU_SOURCE /* to include strcasestr() */
5
# define PRINTFLN(line, fmt, ...) \
6
printf("\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
7
# define EXIT(code) exit ((code) < 255 ? (code) : 255)
9
# if defined(__AVR_ATmega128__)
10
/* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
11
# define PRINTFLN(line, fmt, ...) \
12
sprintf ((char *)0x2000, "\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
15
# define PRINTFLN(args...)
26
void Check (int line, const char *s1, const char *s2, int expect)
32
if ((strlen_P(s1) > sizeof(t1) - 1) || (strlen_P(s2) > sizeof(t2) - 1))
36
p = strcasestr (t1, t2);
40
PRINTFLN (line, "return nonzero");
44
if (p != t1 + expect) {
45
PRINTFLN (line, "expect= %d result= %d", expect, p - t1);
49
if (strcmp_P (t1, s1) || strcmp_P (t2, s2)) {
50
PRINTFLN (line, "string(s) is changed");
55
#define CHECK(s1, s2, expect) do { \
56
Check (__LINE__, PSTR(s1), PSTR(s2), expect); \
67
CHECK ("12345", "", 0);
70
CHECK ("ababac", "abac", 2);
72
/* 'needle' of 1 byte long */
76
CHECK ("abcbef", "a", 0);
78
CHECK (".a.", "a", 1);
79
CHECK ("ABCDEFGH", "H", 7);
81
/* 'needle' of 2 bytes long */
83
CHECK ("13", "12", -1);
84
CHECK ("32", "12", -1);
85
CHECK ("12", "12", 0);
86
CHECK ("123", "12", 0);
87
CHECK ("012", "12", 1);
88
CHECK ("01200", "12", 1);
90
/* partially mathing */
91
CHECK ("a_ab_abc_abcd_abcde", "abcdef", -1);
92
CHECK ("a_ab_abc_abcd_abcde_abcdef", "abcdef", 20);
93
CHECK ("aababcabcdabcde", "abcdef", -1);
94
CHECK ("aababcabcdabcdeabcdef", "abcdef", 15);
97
CHECK ("abaabaaabaaaab", "aaaaab", -1);
98
CHECK ("abaabaaabaaaabaaaaab", "aaaaab", 14);
100
/* A first match is returned. */
101
CHECK ("_foo_foo", "foo", 1);
103
/* Case is ignored. */
105
CHECK ("qwertyuiopasdfghjklzxcvbnm",
106
"QWERTYUIOPASDFGHJKLZXCVBNM",
108
CHECK (" QWERTYUIOPASDFGHJKLZXCVBNM",
109
"qwertyuiopasdfghjklzxcvbnm",
111
CHECK (" The Quick Brown Fox ", "thE quicK browN foX", 2);
113
/* Case is ignored for alphas only. */
114
CHECK ("", "\040", -1);
115
CHECK ("\100", "\140", -1); /* first */
116
CHECK ("\140", "\100", -1);
117
CHECK ("\133", "\173", -1);
118
CHECK ("\173", "\133", -1);
119
CHECK (".\100", ".\140", -1); /* second */
120
CHECK (".\140", ".\100", -1);
121
CHECK (".\133", ".\173", -1);
122
CHECK (".\173", ".\133", -1);
123
CHECK ("\100\140", "\140", 1); /* second match */
126
CHECK ("................................................................"
127
"................................................................"
128
"................................................................"
129
"...............................................................A",
131
CHECK ("................................................................"
132
"................................................................"
133
"................................................................"
134
"................................................................"
136
CHECK ("................................................................"
137
"................................................................"
138
"................................................................"
139
"................................................................"
141
CHECK ("................................................................"
142
"................................................................"
143
"................................................................"
144
"................................................................"
147
/* Let us check a set of possible combinations of 2 symbols. */
148
for (c1 = 1; c1 < 256; c1++) {
149
for (c2 = 1; c2 < 256; c2++) {
151
|| (isalpha(c1) && isalpha(c2) && ((c1 ^ c2) == ('A' ^ 'a'))))
154
/* first char: ("c","c") */
155
s1[0] = c1; s1[1] = 0;
156
s2[0] = c2; s2[1] = 0;
157
if (strcasestr (s1, s2) != s1)
160
/* second char: (".c", ".c") */
161
s1[0] = '.'; s1[1] = c1; s1[2] = 0;
162
s2[0] = '.'; s2[1] = c2; s2[2] = 0;
163
if (strcasestr (s1, s2) != s1)
166
/* substring is shifted: ("..c", ".c") */
167
s1[0] = '.'; s1[1] = '.'; s1[2] = c1; s1[3] = 0;
168
s2[0] = '.'; s2[1] = c2; s2[2] = 0;
169
if (strcasestr (s1, s2) != s1 + (c2 != '.'))
174
/* first char: ("c","c") */
175
s1[0] = c1; s1[1] = 0;
176
s2[0] = c2; s2[1] = 0;
177
if (strcasestr (s1, s2) != 0)
184
/* second char: (".c", ".c") */
185
s1[0] = '.'; s1[1] = c1; s1[2] = 0;
186
s2[0] = '.'; s2[1] = c2; s2[2] = 0;
187
if (strcasestr (s1, s2) != 0)