10 - split - divide a string into fields, like awk split()
11 == int split(char *string, char *fields[], int nfields, char *sep);
12 - fields: list is not NULL-terminated
13 - nfields: number of entries available in fields[]
14 - sep: "" white, "c" single char, "ab" [ab]+
16 int /* number of fields, including overflow */
17 split(char *string, char *fields[], int nfields, char *sep)
20 char c; /* latest character */
30 while ((c = *p++) == ' ' || c == '\t')
34 sep = " \t"; /* note, code below knows this is 2 long */
38 sepc2 = sep[1]; /* now we can safely pick this up */
44 /* single separator */
52 while ((c = *p++) != sepc)
57 /* we have overflowed the fields vector -- just count them */
60 while ((c = *p++) != sepc)
74 while ((c = *p++) != sepc && c != sepc2)
76 if (trimtrail && **(fp-1) == '\0')
83 while ((c = *p++) == sepc || c == sepc2)
87 /* we have overflowed the fields vector -- just count them */
90 while ((c = *p++) == sepc || c == sepc2)
94 while ((c = *p++) != '\0' && c != sepc && c != sepc2)
97 /* might have to trim trailing white space */
100 while ((c = *--p) == sepc || c == sepc2)
123 while ((sepc = *sepp++) != '\0' && sepc != c)
125 if (sepc != '\0') /* it was a separator */
133 while ((sepc = *sepp++) != '\0' && sepc != c)
135 if (sepc == '\0') /* it wasn't a separator */
149 * pgm runs regression
150 * pgm sep splits stdin lines by sep
151 * pgm str sep splits str by sep
152 * pgm str sep n splits str by sep n times
155 main(int argc, char *argv[])
163 for (n = atoi(argv[3]); n > 0; n--) {
164 (void) strcpy(buf, argv[1]);
167 for (n = atoi(argv[3]); n > 0; n--) {
168 (void) strcpy(buf, argv[1]);
169 (void) split(buf, fields, MNF, argv[2]);
172 dosplit(argv[1], argv[2]);
174 while (fgets(buf, sizeof(buf), stdin) != NULL) {
175 buf[strlen(buf)-1] = '\0'; /* stomp newline */
176 dosplit(buf, argv[1]);
185 dosplit(char *string, char *seps)
191 nf = split(string, fields, NF, seps);
192 print(nf, NF, fields);
196 print(int nf, int nfp, char *fields[])
201 bound = (nf > nfp) ? nfp : nf;
203 for (fn = 0; fn < bound; fn++)
204 printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
207 #define RNF 5 /* some table entries know this */
215 " ", " ", 2, { "", "" },
216 "x", " ", 1, { "x" },
217 "xy", " ", 1, { "xy" },
218 "x y", " ", 2, { "x", "y" },
219 "abc def g ", " ", 5, { "abc", "def", "", "g", "" },
220 " a bcd", " ", 4, { "", "", "a", "bcd" },
221 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
222 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
225 " ", " _", 2, { "", "" },
226 "x", " _", 1, { "x" },
227 "x y", " _", 2, { "x", "y" },
228 "ab _ cd", " _", 2, { "ab", "cd" },
229 " a_b c ", " _", 5, { "", "a", "b", "c", "" },
230 "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
231 " a b c d ", " _", 6, { "", "a", "b", "c", "d " },
233 "", " _~", 0, { "" },
234 " ", " _~", 2, { "", "" },
235 "x", " _~", 1, { "x" },
236 "x y", " _~", 2, { "x", "y" },
237 "ab _~ cd", " _~", 2, { "ab", "cd" },
238 " a_b c~", " _~", 5, { "", "a", "b", "c", "" },
239 "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
240 "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
242 "", " _~-", 0, { "" },
243 " ", " _~-", 2, { "", "" },
244 "x", " _~-", 1, { "x" },
245 "x y", " _~-", 2, { "x", "y" },
246 "ab _~- cd", " _~-", 2, { "ab", "cd" },
247 " a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
248 "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
249 "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
252 " ", " ", 2, { "", "" },
253 "x", " ", 1, { "x" },
254 "xy", " ", 1, { "xy" },
255 "x y", " ", 2, { "x", "y" },
256 "abc def g ", " ", 4, { "abc", "def", "g", "" },
257 " a bcd", " ", 3, { "", "a", "bcd" },
258 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
259 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
264 "xy", "", 1, { "xy" },
265 "x y", "", 2, { "x", "y" },
266 "abc def g ", "", 3, { "abc", "def", "g" },
267 "\t a bcd", "", 2, { "a", "bcd" },
268 " a \tb\t c ", "", 3, { "a", "b", "c" },
269 "a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
270 "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
271 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
273 NULL, NULL, 0, { NULL },
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);