1
/* $Id: strsep.c,v 1.1.2.1 2008/03/20 21:42:39 joerg_wunsch Exp $ */
9
# define PRINTFLN(line, fmt, ...) \
10
printf("\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
11
# define EXIT(code) exit ((code) < 255 ? (code) : 255)
12
# define memcmp_P memcmp
14
# if defined(__AVR_ATmega128__)
15
/* ATmega128 has enough RAM for sprintf(), print to 0x2000 in XRAM. */
16
# define PRINTFLN(line, fmt, ...) \
17
sprintf ((char *)0x2000, "\nLine %d: " fmt "\n", line, ##__VA_ARGS__)
20
# define PRINTFLN(args...)
25
void Check (int line, const char *s1, const char *s2, int clr, int pnt)
32
if ((strlen_P(s1) > sizeof(t1) - 1) || (strlen_P(s2) > sizeof(t2) - 1))
37
rp = strsep (&sp, t2);
40
PRINTFLN (line, "false return value");
44
if (strcmp_P (t1, s1)) {
45
PRINTFLN (line, "string is changed");
49
if (strlen (t1) != (size_t)clr) {
50
PRINTFLN (line, "strlen: expect= %d result= %d",
54
if (memcmp_P (t1, s1, clr)
56
|| strcmp_P (t1 + clr + 1, s1 + clr + 1))
58
PRINTFLN (line, "string mismatch");
64
PRINTFLN (line, "sp is not a NULL");
69
PRINTFLN (line, "sp: expect= %d result= %d",
78
delim - delimeter list
79
clr - if (clr >= 0) s[cln] must be cleared
80
pnt - if (pnt >= 0) s[pnt] must be pointed, else NULL
82
#define CHECK(s, delim, clr, pnt) do { \
83
Check (__LINE__, PSTR(s), PSTR(delim), clr, pnt); \
90
/* NULL at first call */
92
if (strsep (&p, "") || p) exit (__LINE__);
93
if (strsep (&p, "abc") || p) exit (__LINE__);
96
CHECK ("", "", -1, -1);
97
CHECK ("", "abc", -1, -1);
99
/* Empty delimeter list */
100
CHECK ("a", "", -1, -1);
101
CHECK ("12345678", "", -1, -1);
103
/* No delimeter symbols are founded */
104
CHECK ("\tabc", " ", -1, -1);
105
CHECK ("THE QUICK BROWN FOX", "thequickbrownfox", -1, -1);
107
/* delim is 1 byte long */
108
CHECK (".", ".", 0, 1);
109
CHECK ("abc", "a", 0, 1);
110
CHECK ("abc", "b", 1, 2);
111
CHECK ("abc", "c", 2, 3);
113
/* delim is 2 bytes long */
114
CHECK ("0", "01", 0, 1);
115
CHECK ("1", "01", 0, 1);
116
CHECK ("A.", "AB", 0, 1);
117
CHECK ("B.", "AB", 0, 1);
118
CHECK ("CAD", "AB", 1, 2);
119
CHECK ("CDB", "AB", 2, 3);
121
/* delim length > 2 bytes */
122
CHECK ("the quick", "0123456789 ", 3, 4);
124
/* Very long string */
125
CHECK ("................................................................"
126
"................................................................"
127
"................................................................"
128
"...............................................................*",
130
CHECK ("................................................................"
131
"................................................................"
132
"................................................................"
133
"................................................................"
136
/* Non ASCII bytes */
137
CHECK ("\001\002\377", "\001", 0, 1);
138
CHECK ("\001\002\377", "\377", 2, 3);