6 - split - divide a string into fields, like awk split()
7 = int split(char *string, char *fields[], int nfields, char *sep);
9 int /* number of fields, including overflow */
12 char * fields[], /* list is not NULL-terminated */
13 int nfields, /* number of entries available in fields[] */
14 char * sep) /* "" white, "c" single char, "ab" [ab]+ */
16 register char *p = string;
17 register char c; /* latest character */
18 register char sepc = sep[0];
21 register char **fp = fields;
23 register int trimtrail;
27 while ((c = *p++) == ' ' || c == '\t')
31 sep = " \t"; /* note, code below knows this is 2 long */
35 sepc2 = sep[1]; /* now we can safely pick this up */
41 /* single separator */
49 while ((c = *p++) != sepc)
54 /* we have overflowed the fields vector -- just count them */
57 while ((c = *p++) != sepc)
71 while ((c = *p++) != sepc && c != sepc2)
73 if (trimtrail && **(fp-1) == '\0')
80 while ((c = *p++) == sepc || c == sepc2)
84 /* we have overflowed the fields vector -- just count them */
87 while ((c = *p++) == sepc || c == sepc2)
91 while ((c = *p++) != '\0' && c != sepc && c != sepc2)
94 /* might have to trim trailing white space */
97 while ((c = *--p) == sepc || c == sepc2)
120 while ((sepc = *sepp++) != '\0' && sepc != c)
122 if (sepc != '\0') /* it was a separator */
130 while ((sepc = *sepp++) != '\0' && sepc != c)
132 if (sepc == '\0') /* it wasn't a separator */
146 * pgm runs regression
147 * pgm sep splits stdin lines by sep
148 * pgm str sep splits str by sep
149 * pgm str sep n splits str by sep n times
162 for (n = atoi(argv[3]); n > 0; n--) {
163 strncpy(buf, argv[1], SIZEOF(buf)-1);
164 buf[SIZEOF(buf)-1] = '\0';
167 for (n = atoi(argv[3]); n > 0; n--) {
168 strncpy(buf, argv[1], SIZEOF(buf)-1);
169 buf[SIZEOF(buf)-1] = '\0';
170 (void) split(buf, fields, MNF, argv[2]);
173 dosplit(argv[1], argv[2]);
175 while (fgets(buf, (int)sizeof(buf), stdin) != NULL) {
176 buf[strlen(buf)-1] = '\0'; /* stomp newline */
177 dosplit(buf, argv[1]);
194 nf = split(string, fields, NF, seps);
195 print(nf, NF, fields);
207 bound = (nf > nfp) ? nfp : nf;
209 for (fn = 0; fn < bound; fn++)
210 printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
213 #define RNF 5 /* some table entries know this */
221 " ", " ", 2, { "", "" },
222 "x", " ", 1, { "x" },
223 "xy", " ", 1, { "xy" },
224 "x y", " ", 2, { "x", "y" },
225 "abc def g ", " ", 5, { "abc", "def", "", "g", "" },
226 " a bcd", " ", 4, { "", "", "a", "bcd" },
227 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
228 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
231 " ", " _", 2, { "", "" },
232 "x", " _", 1, { "x" },
233 "x y", " _", 2, { "x", "y" },
234 "ab _ cd", " _", 2, { "ab", "cd" },
235 " a_b c ", " _", 5, { "", "a", "b", "c", "" },
236 "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
237 " a b c d ", " _", 6, { "", "a", "b", "c", "d " },
239 "", " _~", 0, { "" },
240 " ", " _~", 2, { "", "" },
241 "x", " _~", 1, { "x" },
242 "x y", " _~", 2, { "x", "y" },
243 "ab _~ cd", " _~", 2, { "ab", "cd" },
244 " a_b c~", " _~", 5, { "", "a", "b", "c", "" },
245 "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
246 "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
248 "", " _~-", 0, { "" },
249 " ", " _~-", 2, { "", "" },
250 "x", " _~-", 1, { "x" },
251 "x y", " _~-", 2, { "x", "y" },
252 "ab _~- cd", " _~-", 2, { "ab", "cd" },
253 " a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
254 "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
255 "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
258 " ", " ", 2, { "", "" },
259 "x", " ", 1, { "x" },
260 "xy", " ", 1, { "xy" },
261 "x y", " ", 2, { "x", "y" },
262 "abc def g ", " ", 4, { "abc", "def", "g", "" },
263 " a bcd", " ", 3, { "", "a", "bcd" },
264 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
265 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
270 "xy", "", 1, { "xy" },
271 "x y", "", 2, { "x", "y" },
272 "abc def g ", "", 3, { "abc", "def", "g" },
273 "\t a bcd", "", 2, { "a", "bcd" },
274 " a \tb\t c ", "", 3, { "a", "b", "c" },
275 "a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
276 "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
277 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
279 NULL, NULL, 0, { NULL },
290 register int printit;
293 for (n = 0; tests[n].str != NULL; n++) {
294 strncpy(buf, tests[n].str, SIZEOF(buf)-1);
295 buf[SIZEOF(buf)-1] = '\0';
297 nf = split(buf, fields, RNF, tests[n].seps);
299 if (nf != tests[n].nf) {
300 printf("split `%s' by `%s' gave %d fields, not %d\n",
301 tests[n].str, tests[n].seps, nf, tests[n].nf);
303 } else if (fields[RNF] != NULL) {
304 printf("split() went beyond array end\n");
307 for (i = 0; i < nf && i < RNF; i++) {
311 if (strcmp(f, tests[n].fi[i]) != 0) {
312 printf("split `%s' by `%s', field %d is `%s', not `%s'\n",
313 tests[n].str, tests[n].seps,
314 i, fields[i], tests[n].fi[i]);
320 print(nf, RNF, fields);