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 = " \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 strncpy(buf, argv[1], sizeof(buf)-1);
163 buf[sizeof(buf)-1] = '\0';
166 for (n = atoi(argv[3]); n > 0; n--) {
167 strncpy(buf, argv[1], sizeof(buf)-1);
168 buf[sizeof(buf)-1] = '\0';
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]);
184 dosplit(string, seps)
192 nf = split(string, fields, NF, seps);
193 print(nf, NF, fields);
196 print(nf, nfp, fields)
204 bound = (nf > nfp) ? nfp : nf;
206 for (fn = 0; fn < bound; fn++)
207 printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
210 #define RNF 5 /* some table entries know this */
218 " ", " ", 2, { "", "" },
219 "x", " ", 1, { "x" },
220 "xy", " ", 1, { "xy" },
221 "x y", " ", 2, { "x", "y" },
222 "abc def g ", " ", 5, { "abc", "def", "", "g", "" },
223 " a bcd", " ", 4, { "", "", "a", "bcd" },
224 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
225 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
228 " ", " _", 2, { "", "" },
229 "x", " _", 1, { "x" },
230 "x y", " _", 2, { "x", "y" },
231 "ab _ cd", " _", 2, { "ab", "cd" },
232 " a_b c ", " _", 5, { "", "a", "b", "c", "" },
233 "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
234 " a b c d ", " _", 6, { "", "a", "b", "c", "d " },
236 "", " _~", 0, { "" },
237 " ", " _~", 2, { "", "" },
238 "x", " _~", 1, { "x" },
239 "x y", " _~", 2, { "x", "y" },
240 "ab _~ cd", " _~", 2, { "ab", "cd" },
241 " a_b c~", " _~", 5, { "", "a", "b", "c", "" },
242 "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
243 "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
245 "", " _~-", 0, { "" },
246 " ", " _~-", 2, { "", "" },
247 "x", " _~-", 1, { "x" },
248 "x y", " _~-", 2, { "x", "y" },
249 "ab _~- cd", " _~-", 2, { "ab", "cd" },
250 " a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
251 "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
252 "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
255 " ", " ", 2, { "", "" },
256 "x", " ", 1, { "x" },
257 "xy", " ", 1, { "xy" },
258 "x y", " ", 2, { "x", "y" },
259 "abc def g ", " ", 4, { "abc", "def", "g", "" },
260 " a bcd", " ", 3, { "", "a", "bcd" },
261 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
262 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
267 "xy", "", 1, { "xy" },
268 "x y", "", 2, { "x", "y" },
269 "abc def g ", "", 3, { "abc", "def", "g" },
270 "\t a bcd", "", 2, { "a", "bcd" },
271 " a \tb\t c ", "", 3, { "a", "b", "c" },
272 "a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
273 "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
274 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
276 NULL, NULL, 0, { NULL },
286 register int printit;
289 for (n = 0; tests[n].str != NULL; n++) {
290 strncpy(buf, tests[n].str, sizeof(buf)-1);
291 buf[sizeof(buf)-1] = '\0';
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);