5
- split - divide a string into fields, like awk split()
6
= int split(char *string, char *fields[], int nfields, char *sep);
8
int /* number of fields, including overflow */
9
split(string, fields, nfields, sep)
11
char *fields[]; /* list is not NULL-terminated */
12
int nfields; /* number of entries available in fields[] */
13
char *sep; /* "" white, "c" single char, "ab" [ab]+ */
15
register char *p = string;
16
register char c; /* latest character */
17
register char sepc = sep[0];
20
register char **fp = fields;
22
register int trimtrail;
26
while ((c = *p++) == ' ' || c == '\t')
30
sep = (char*) " \t"; /* note, code below knows this is 2 long */
34
sepc2 = sep[1]; /* now we can safely pick this up */
40
/* single separator */
48
while ((c = *p++) != sepc)
53
/* we have overflowed the fields vector -- just count them */
56
while ((c = *p++) != sepc)
70
while ((c = *p++) != sepc && c != sepc2)
72
if (trimtrail && **(fp-1) == '\0')
79
while ((c = *p++) == sepc || c == sepc2)
83
/* we have overflowed the fields vector -- just count them */
86
while ((c = *p++) == sepc || c == sepc2)
90
while ((c = *p++) != '\0' && c != sepc && c != sepc2)
93
/* might have to trim trailing white space */
96
while ((c = *--p) == sepc || c == sepc2)
119
while ((sepc = *sepp++) != '\0' && sepc != c)
121
if (sepc != '\0') /* it was a separator */
129
while ((sepc = *sepp++) != '\0' && sepc != c)
131
if (sepc == '\0') /* it wasn't a separator */
145
* pgm runs regression
146
* pgm sep splits stdin lines by sep
147
* pgm str sep splits str by sep
148
* pgm str sep n splits str by sep n times
161
for (n = atoi(argv[3]); n > 0; n--) {
162
(void) strcpy(buf, argv[1]);
165
for (n = atoi(argv[3]); n > 0; n--) {
166
(void) strcpy(buf, argv[1]);
167
(void) split(buf, fields, MNF, argv[2]);
170
dosplit(argv[1], argv[2]);
172
while (fgets(buf, sizeof(buf), stdin) != NULL) {
173
buf[strlen(buf)-1] = '\0'; /* stomp newline */
174
dosplit(buf, argv[1]);
182
dosplit(string, seps)
190
nf = split(string, fields, NF, seps);
191
print(nf, NF, fields);
194
print(nf, nfp, fields)
202
bound = (nf > nfp) ? nfp : nf;
204
for (fn = 0; fn < bound; fn++)
205
printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
208
#define RNF 5 /* some table entries know this */
216
" ", " ", 2, { "", "" },
217
"x", " ", 1, { "x" },
218
"xy", " ", 1, { "xy" },
219
"x y", " ", 2, { "x", "y" },
220
"abc def g ", " ", 5, { "abc", "def", "", "g", "" },
221
" a bcd", " ", 4, { "", "", "a", "bcd" },
222
"a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
223
" a b c d ", " ", 6, { "", "a", "b", "c", "d " },
226
" ", " _", 2, { "", "" },
227
"x", " _", 1, { "x" },
228
"x y", " _", 2, { "x", "y" },
229
"ab _ cd", " _", 2, { "ab", "cd" },
230
" a_b c ", " _", 5, { "", "a", "b", "c", "" },
231
"a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
232
" a b c d ", " _", 6, { "", "a", "b", "c", "d " },
234
"", " _~", 0, { "" },
235
" ", " _~", 2, { "", "" },
236
"x", " _~", 1, { "x" },
237
"x y", " _~", 2, { "x", "y" },
238
"ab _~ cd", " _~", 2, { "ab", "cd" },
239
" a_b c~", " _~", 5, { "", "a", "b", "c", "" },
240
"a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
241
"~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
243
"", " _~-", 0, { "" },
244
" ", " _~-", 2, { "", "" },
245
"x", " _~-", 1, { "x" },
246
"x y", " _~-", 2, { "x", "y" },
247
"ab _~- cd", " _~-", 2, { "ab", "cd" },
248
" a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
249
"a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
250
"~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
253
" ", " ", 2, { "", "" },
254
"x", " ", 1, { "x" },
255
"xy", " ", 1, { "xy" },
256
"x y", " ", 2, { "x", "y" },
257
"abc def g ", " ", 4, { "abc", "def", "g", "" },
258
" a bcd", " ", 3, { "", "a", "bcd" },
259
"a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
260
" a b c d ", " ", 6, { "", "a", "b", "c", "d " },
265
"xy", "", 1, { "xy" },
266
"x y", "", 2, { "x", "y" },
267
"abc def g ", "", 3, { "abc", "def", "g" },
268
"\t a bcd", "", 2, { "a", "bcd" },
269
" a \tb\t c ", "", 3, { "a", "b", "c" },
270
"a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
271
"a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
272
" a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
274
NULL, NULL, 0, { NULL },
284
register int printit;
287
for (n = 0; tests[n].str != NULL; n++) {
288
(void) strcpy(buf, tests[n].str);
290
nf = split(buf, fields, RNF, tests[n].seps);
292
if (nf != tests[n].nf) {
293
printf("split `%s' by `%s' gave %d fields, not %d\n",
294
tests[n].str, tests[n].seps, nf, tests[n].nf);
296
} else if (fields[RNF] != NULL) {
297
printf("split() went beyond array end\n");
300
for (i = 0; i < nf && i < RNF; i++) {
304
if (strcmp(f, tests[n].fi[i]) != 0) {
305
printf("split `%s' by `%s', field %d is `%s', not `%s'\n",
306
tests[n].str, tests[n].seps,
307
i, fields[i], tests[n].fi[i]);
313
print(nf, RNF, fields);