1
/* $Id: memmem_P.c,v 1.1.2.1 2008/03/20 21:42:30 joerg_wunsch Exp $ */
4
# define _GNU_SOURCE /* to include memmem() */
5
# define PRINTFLN(line, fmt, ...) \
6
printf("\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
7
# define EXIT(code) exit ((code) < 255 ? (code) : 255)
8
# define memcmp_P memcmp
9
# define memmem_P memmem
11
# if defined(__AVR_ATmega128__)
12
/* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
13
# define PRINTFLN(line, fmt, ...) \
14
sprintf ((char *)0x2000, "\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
17
# define PRINTFLN(args...)
28
const void *s1, size_t n1, const char *s2, size_t n2, int expect)
35
memcpy_P (t1, s1, n1);
36
p = memmem_P (t1, n1, s2, n2);
40
PRINTFLN (line, "return nonzero");
44
if (p != t1 + expect) {
45
PRINTFLN (line, "expect= %d result= %d", expect, p - t1);
49
if (memcmp_P (t1, s1, n1)) {
50
PRINTFLN (line, "string is changed");
55
#define CHECK(s1, n1, s2, n2, expect) do { \
56
Check (__LINE__, PSTR(s1), n1, PSTR(s2), n2, expect); \
62
CHECK ("", 0, "", 0, 0);
63
CHECK ("12345", 5, "1", 0, 0);
65
/* 'needle' of 1 byte long */
66
CHECK ("", 0, "a", 1, -1);
67
CHECK ("b", 1, "a", 1, -1);
68
CHECK ("a", 1, "a", 1, 0);
69
CHECK ("abcbef", 6, "a", 1, 0);
70
CHECK (".a", 2, "a", 1, 1);
71
CHECK (".a.", 3, "a", 1, 1);
72
CHECK ("ABCDEFGH", 8, "H", 1, 7);
73
CHECK ("", 0, "\000", 1, -1);
74
CHECK ("", 1, "\000", 1, 0);
75
CHECK (".", 2, "\000", 1, 1);
76
CHECK ("\000\001", 2, "\001", 1, 1);
78
/* 'needle' of 2 bytes long */
79
CHECK ("", 0, "12", 2, -1);
80
CHECK ("1", 1, "12", 2, -1);
81
CHECK ("13", 2, "12", 2, -1);
82
CHECK ("32", 2, "12", 2, -1);
83
CHECK ("12", 2, "12", 2, 0);
84
CHECK ("123", 3, "12", 2, 0);
85
CHECK ("012", 3, "12", 2, 1);
86
CHECK ("01200", 5, "12", 2, 1);
87
CHECK ("\000", 1, "\000\000", 2, -1);
88
CHECK ("\000\000", 2, "\000\000", 2, 0);
90
/* partially mathing */
91
CHECK ("a_ab_abc_abcd_abcde", 19, "abcdef", 6, -1);
92
CHECK ("a_ab_abc_abcd_abcde_abcdef", 26, "abcdef", 6, 20);
93
CHECK ("aababcabcdabcde", 15, "abcdef", 6, -1);
94
CHECK ("aababcabcdabcdeabcdef", 21, "abcdef", 6, 15);
97
CHECK ("abaabaaabaaaab", 14, "aaaaab", 6, -1);
98
CHECK ("abaabaaabaaaabaaaaab", 20, "aaaaab", 6, 14);
100
/* A first match is returned. */
101
CHECK ("_foo_foo", 8, "foo", 3, 1);
103
/* Case is not ignored. */
104
CHECK ("A", 1, "a", 1, -1);
105
CHECK (".z", 2, ".Z", 2, -1);
108
CHECK ("................................................................"
109
"................................................................"
110
"................................................................"
111
"...............................................................*",
113
CHECK ("................................................................"
114
"................................................................"
115
"................................................................"
116
"................................................................"
117
"*", 257, "*", 1, 256);
118
CHECK ("................................................................"
119
"................................................................"
120
"................................................................"
121
"................................................................"
122
".*.", 258, "*", 1, 257);
123
CHECK ("................................................................"
124
"................................................................"
125
"................................................................"
126
"................................................................"
127
"...*..", 260, ".*", 2, 258);
131
"................................................................"
132
"................................................................"
133
"................................................................"
134
"...............................................................",
137
"................................................................"
138
"................................................................"
139
"................................................................"
140
"................................................................",
143
/* Both strings are long. */
144
CHECK ("................................................................"
145
"................................................................"
146
"................................................................"
147
"................................................................"
149
"................................................................"
150
"................................................................"
151
"................................................................"
152
"...............................................................*"
154
CHECK ("................................................................"
155
"................................................................"
156
"................................................................"
157
"................................................................"
159
"................................................................"
160
"................................................................"
161
"................................................................"
162
"................................................................"