8 - split - divide a string into fields, like awk split()
9 = int split(char *string, char *fields[], int nfields, char *sep);
11 int /* number of fields, including overflow */
12 split(string, fields, nfields, sep)
14 char *fields[]; /* list is not NULL-terminated */
15 int nfields; /* number of entries available in fields[] */
16 char *sep; /* "" white, "c" single char, "ab" [ab]+ */
19 char c; /* latest character */
29 while ((c = *p++) == ' ' || c == '\t')
33 sep = " \t"; /* note, code below knows this is 2 long */
37 sepc2 = sep[1]; /* now we can safely pick this up */
43 /* single separator */
51 while ((c = *p++) != sepc)
56 /* we have overflowed the fields vector -- just count them */
59 while ((c = *p++) != sepc)
73 while ((c = *p++) != sepc && c != sepc2)
75 if (trimtrail && **(fp-1) == '\0')
82 while ((c = *p++) == sepc || c == sepc2)
86 /* we have overflowed the fields vector -- just count them */
89 while ((c = *p++) == sepc || c == sepc2)
93 while ((c = *p++) != '\0' && c != sepc && c != sepc2)
96 /* might have to trim trailing white space */
99 while ((c = *--p) == sepc || c == sepc2)
122 while ((sepc = *sepp++) != '\0' && sepc != c)
124 if (sepc != '\0') /* it was a separator */
132 while ((sepc = *sepp++) != '\0' && sepc != c)
134 if (sepc == '\0') /* it wasn't a separator */
148 * pgm runs regression
149 * pgm sep splits stdin lines by sep
150 * pgm str sep splits str by sep
151 * pgm str sep n splits str by sep n times
164 for (n = atoi(argv[3]); n > 0; n--) {
165 (void) strcpy(buf, argv[1]);
168 for (n = atoi(argv[3]); n > 0; n--) {
169 (void) strcpy(buf, argv[1]);
170 (void) split(buf, fields, MNF, argv[2]);
173 dosplit(argv[1], argv[2]);
175 while (fgets(buf, sizeof(buf), stdin) != NULL) {
176 buf[strlen(buf)-1] = '\0'; /* stomp newline */
177 dosplit(buf, argv[1]);
185 dosplit(string, seps)
193 nf = split(string, fields, NF, seps);
194 print(nf, NF, fields);
197 print(nf, nfp, fields)
205 bound = (nf > nfp) ? nfp : nf;
207 for (fn = 0; fn < bound; fn++)
208 printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
211 #define RNF 5 /* some table entries know this */
219 " ", " ", 2, { "", "" },
220 "x", " ", 1, { "x" },
221 "xy", " ", 1, { "xy" },
222 "x y", " ", 2, { "x", "y" },
223 "abc def g ", " ", 5, { "abc", "def", "", "g", "" },
224 " a bcd", " ", 4, { "", "", "a", "bcd" },
225 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
226 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
229 " ", " _", 2, { "", "" },
230 "x", " _", 1, { "x" },
231 "x y", " _", 2, { "x", "y" },
232 "ab _ cd", " _", 2, { "ab", "cd" },
233 " a_b c ", " _", 5, { "", "a", "b", "c", "" },
234 "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
235 " a b c d ", " _", 6, { "", "a", "b", "c", "d " },
237 "", " _~", 0, { "" },
238 " ", " _~", 2, { "", "" },
239 "x", " _~", 1, { "x" },
240 "x y", " _~", 2, { "x", "y" },
241 "ab _~ cd", " _~", 2, { "ab", "cd" },
242 " a_b c~", " _~", 5, { "", "a", "b", "c", "" },
243 "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
244 "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
246 "", " _~-", 0, { "" },
247 " ", " _~-", 2, { "", "" },
248 "x", " _~-", 1, { "x" },
249 "x y", " _~-", 2, { "x", "y" },
250 "ab _~- cd", " _~-", 2, { "ab", "cd" },
251 " a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
252 "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
253 "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
256 " ", " ", 2, { "", "" },
257 "x", " ", 1, { "x" },
258 "xy", " ", 1, { "xy" },
259 "x y", " ", 2, { "x", "y" },
260 "abc def g ", " ", 4, { "abc", "def", "g", "" },
261 " a bcd", " ", 3, { "", "a", "bcd" },
262 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
263 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
268 "xy", "", 1, { "xy" },
269 "x y", "", 2, { "x", "y" },
270 "abc def g ", "", 3, { "abc", "def", "g" },
271 "\t a bcd", "", 2, { "a", "bcd" },
272 " a \tb\t c ", "", 3, { "a", "b", "c" },
273 "a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
274 "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
275 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
277 NULL, NULL, 0, { NULL },
290 for (n = 0; tests[n].str != NULL; n++) {
291 (void) strcpy(buf, tests[n].str);
293 nf = split(buf, fields, RNF, tests[n].seps);
295 if (nf != tests[n].nf) {
296 printf("split `%s' by `%s' gave %d fields, not %d\n",
297 tests[n].str, tests[n].seps, nf, tests[n].nf);
299 } else if (fields[RNF] != NULL) {
300 printf("split() went beyond array end\n");
303 for (i = 0; i < nf && i < RNF; i++) {
307 if (strcmp(f, tests[n].fi[i]) != 0) {
308 printf("split `%s' by `%s' field %d is `%s', not `%s'\n",
309 tests[n].str, tests[n].seps,
310 i, fields[i], tests[n].fi[i]);
316 print(nf, RNF, fields);