8 - split - divide a string into fields, like awk split()
9 == int split(char *string, char *fields[], int nfields, char *sep);
10 - fields: list is not NULL-terminated
11 - nfields: number of entries available in fields[]
12 - sep: "" white, "c" single char, "ab" [ab]+
14 int /* number of fields, including overflow */
15 split(char *string, char *fields[], int nfields, char *sep)
18 char c; /* latest character */
28 while ((c = *p++) == ' ' || c == '\t')
32 sep = " \t"; /* note, code below knows this is 2 long */
36 sepc2 = sep[1]; /* now we can safely pick this up */
42 /* single separator */
50 while ((c = *p++) != sepc)
55 /* we have overflowed the fields vector -- just count them */
58 while ((c = *p++) != sepc)
72 while ((c = *p++) != sepc && c != sepc2)
74 if (trimtrail && **(fp-1) == '\0')
81 while ((c = *p++) == sepc || c == sepc2)
85 /* we have overflowed the fields vector -- just count them */
88 while ((c = *p++) == sepc || c == sepc2)
92 while ((c = *p++) != '\0' && c != sepc && c != sepc2)
95 /* might have to trim trailing white space */
98 while ((c = *--p) == sepc || c == sepc2)
121 while ((sepc = *sepp++) != '\0' && sepc != c)
123 if (sepc != '\0') /* it was a separator */
131 while ((sepc = *sepp++) != '\0' && sepc != c)
133 if (sepc == '\0') /* it wasn't a separator */
147 * pgm runs regression
148 * pgm sep splits stdin lines by sep
149 * pgm str sep splits str by sep
150 * pgm str sep n splits str by sep n times
153 main(int argc, char *argv[])
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]);
183 dosplit(char *string, char *seps)
189 nf = split(string, fields, NF, seps);
190 print(nf, NF, fields);
194 print(int nf, int nfp, char *fields[])
199 bound = (nf > nfp) ? nfp : nf;
201 for (fn = 0; fn < bound; fn++)
202 printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
205 #define RNF 5 /* some table entries know this */
213 " ", " ", 2, { "", "" },
214 "x", " ", 1, { "x" },
215 "xy", " ", 1, { "xy" },
216 "x y", " ", 2, { "x", "y" },
217 "abc def g ", " ", 5, { "abc", "def", "", "g", "" },
218 " a bcd", " ", 4, { "", "", "a", "bcd" },
219 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
220 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
223 " ", " _", 2, { "", "" },
224 "x", " _", 1, { "x" },
225 "x y", " _", 2, { "x", "y" },
226 "ab _ cd", " _", 2, { "ab", "cd" },
227 " a_b c ", " _", 5, { "", "a", "b", "c", "" },
228 "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
229 " a b c d ", " _", 6, { "", "a", "b", "c", "d " },
231 "", " _~", 0, { "" },
232 " ", " _~", 2, { "", "" },
233 "x", " _~", 1, { "x" },
234 "x y", " _~", 2, { "x", "y" },
235 "ab _~ cd", " _~", 2, { "ab", "cd" },
236 " a_b c~", " _~", 5, { "", "a", "b", "c", "" },
237 "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
238 "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
240 "", " _~-", 0, { "" },
241 " ", " _~-", 2, { "", "" },
242 "x", " _~-", 1, { "x" },
243 "x y", " _~-", 2, { "x", "y" },
244 "ab _~- cd", " _~-", 2, { "ab", "cd" },
245 " a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
246 "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
247 "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
250 " ", " ", 2, { "", "" },
251 "x", " ", 1, { "x" },
252 "xy", " ", 1, { "xy" },
253 "x y", " ", 2, { "x", "y" },
254 "abc def g ", " ", 4, { "abc", "def", "g", "" },
255 " a bcd", " ", 3, { "", "a", "bcd" },
256 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
257 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
262 "xy", "", 1, { "xy" },
263 "x y", "", 2, { "x", "y" },
264 "abc def g ", "", 3, { "abc", "def", "g" },
265 "\t a bcd", "", 2, { "a", "bcd" },
266 " a \tb\t c ", "", 3, { "a", "b", "c" },
267 "a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
268 "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
269 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
271 NULL, NULL, 0, { NULL },
285 for (n = 0; tests[n].str != NULL; n++) {
286 (void) strcpy(buf, tests[n].str);
288 nf = split(buf, fields, RNF, tests[n].seps);
290 if (nf != tests[n].nf) {
291 printf("split `%s' by `%s' gave %d fields, not %d\n",
292 tests[n].str, tests[n].seps, nf, tests[n].nf);
294 } else if (fields[RNF] != NULL) {
295 printf("split() went beyond array end\n");
298 for (i = 0; i < nf && i < RNF; i++) {
302 if (strcmp(f, tests[n].fi[i]) != 0) {
303 printf("split `%s' by `%s' field %d is `%s', not `%s'\n",
304 tests[n].str, tests[n].seps,
305 i, fields[i], tests[n].fi[i]);
311 print(nf, RNF, fields);