1*17f9a364Spgoyette /* $NetBSD: split.c,v 1.1 2011/01/08 18:10:31 pgoyette Exp $ */
2*17f9a364Spgoyette
3*17f9a364Spgoyette /*-
4*17f9a364Spgoyette * Copyright (c) 1993 The NetBSD Foundation, Inc.
5*17f9a364Spgoyette * All rights reserved.
6*17f9a364Spgoyette *
7*17f9a364Spgoyette * Redistribution and use in source and binary forms, with or without
8*17f9a364Spgoyette * modification, are permitted provided that the following conditions
9*17f9a364Spgoyette * are met:
10*17f9a364Spgoyette * 1. Redistributions of source code must retain the above copyright
11*17f9a364Spgoyette * notice, this list of conditions and the following disclaimer.
12*17f9a364Spgoyette * 2. Redistributions in binary form must reproduce the above copyright
13*17f9a364Spgoyette * notice, this list of conditions and the following disclaimer in the
14*17f9a364Spgoyette * documentation and/or other materials provided with the distribution.
15*17f9a364Spgoyette *
16*17f9a364Spgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*17f9a364Spgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*17f9a364Spgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*17f9a364Spgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*17f9a364Spgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*17f9a364Spgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*17f9a364Spgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*17f9a364Spgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*17f9a364Spgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*17f9a364Spgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*17f9a364Spgoyette * POSSIBILITY OF SUCH DAMAGE.
27*17f9a364Spgoyette */
28*17f9a364Spgoyette
29*17f9a364Spgoyette #include <regex.h>
30*17f9a364Spgoyette #include <stdio.h>
31*17f9a364Spgoyette #include <string.h>
32*17f9a364Spgoyette
33*17f9a364Spgoyette #include "test_regex.h"
34*17f9a364Spgoyette
35*17f9a364Spgoyette /*
36*17f9a364Spgoyette * split - divide a string into fields, like awk split()
37*17f9a364Spgoyette *
38*17f9a364Spgoyette * returns number of fields, including overflow
39*17f9a364Spgoyette *
40*17f9a364Spgoyette * fields[] list is not NULL-terminated
41*17f9a364Spgoyette * nfields number of entries available in fields[]
42*17f9a364Spgoyette * sep "" white, "c" single char, "ab" [ab]+
43*17f9a364Spgoyette */
44*17f9a364Spgoyette int
split(char * string,char * fields[],int nfields,const char * sep)45*17f9a364Spgoyette split(char *string, char *fields[], int nfields, const char *sep)
46*17f9a364Spgoyette {
47*17f9a364Spgoyette char *p = string;
48*17f9a364Spgoyette char c; /* latest character */
49*17f9a364Spgoyette char sepc = *sep;
50*17f9a364Spgoyette char sepc2;
51*17f9a364Spgoyette int fn;
52*17f9a364Spgoyette char **fp = fields;
53*17f9a364Spgoyette const char *sepp;
54*17f9a364Spgoyette int trimtrail;
55*17f9a364Spgoyette
56*17f9a364Spgoyette /* white space */
57*17f9a364Spgoyette if (sepc == '\0') {
58*17f9a364Spgoyette while ((c = *p++) == ' ' || c == '\t')
59*17f9a364Spgoyette continue;
60*17f9a364Spgoyette p--;
61*17f9a364Spgoyette trimtrail = 1;
62*17f9a364Spgoyette sep = " \t"; /* note, code below knows this is 2 long */
63*17f9a364Spgoyette sepc = ' ';
64*17f9a364Spgoyette } else
65*17f9a364Spgoyette trimtrail = 0;
66*17f9a364Spgoyette sepc2 = sep[1]; /* now we can safely pick this up */
67*17f9a364Spgoyette
68*17f9a364Spgoyette /* catch empties */
69*17f9a364Spgoyette if (*p == '\0')
70*17f9a364Spgoyette return(0);
71*17f9a364Spgoyette
72*17f9a364Spgoyette /* single separator */
73*17f9a364Spgoyette if (sepc2 == '\0') {
74*17f9a364Spgoyette fn = nfields;
75*17f9a364Spgoyette for (;;) {
76*17f9a364Spgoyette *fp++ = p;
77*17f9a364Spgoyette fn--;
78*17f9a364Spgoyette if (fn == 0)
79*17f9a364Spgoyette break;
80*17f9a364Spgoyette while ((c = *p++) != sepc)
81*17f9a364Spgoyette if (c == '\0')
82*17f9a364Spgoyette return(nfields - fn);
83*17f9a364Spgoyette *(p-1) = '\0';
84*17f9a364Spgoyette }
85*17f9a364Spgoyette /* we have overflowed the fields vector -- just count them */
86*17f9a364Spgoyette fn = nfields;
87*17f9a364Spgoyette for (;;) {
88*17f9a364Spgoyette while ((c = *p++) != sepc)
89*17f9a364Spgoyette if (c == '\0')
90*17f9a364Spgoyette return(fn);
91*17f9a364Spgoyette fn++;
92*17f9a364Spgoyette }
93*17f9a364Spgoyette /* not reached */
94*17f9a364Spgoyette }
95*17f9a364Spgoyette
96*17f9a364Spgoyette /* two separators */
97*17f9a364Spgoyette if (sep[2] == '\0') {
98*17f9a364Spgoyette fn = nfields;
99*17f9a364Spgoyette for (;;) {
100*17f9a364Spgoyette *fp++ = p;
101*17f9a364Spgoyette fn--;
102*17f9a364Spgoyette while ((c = *p++) != sepc && c != sepc2)
103*17f9a364Spgoyette if (c == '\0') {
104*17f9a364Spgoyette if (trimtrail && **(fp-1) == '\0')
105*17f9a364Spgoyette fn++;
106*17f9a364Spgoyette return(nfields - fn);
107*17f9a364Spgoyette }
108*17f9a364Spgoyette if (fn == 0)
109*17f9a364Spgoyette break;
110*17f9a364Spgoyette *(p-1) = '\0';
111*17f9a364Spgoyette while ((c = *p++) == sepc || c == sepc2)
112*17f9a364Spgoyette continue;
113*17f9a364Spgoyette p--;
114*17f9a364Spgoyette }
115*17f9a364Spgoyette /* we have overflowed the fields vector -- just count them */
116*17f9a364Spgoyette fn = nfields;
117*17f9a364Spgoyette while (c != '\0') {
118*17f9a364Spgoyette while ((c = *p++) == sepc || c == sepc2)
119*17f9a364Spgoyette continue;
120*17f9a364Spgoyette p--;
121*17f9a364Spgoyette fn++;
122*17f9a364Spgoyette while ((c = *p++) != '\0' && c != sepc && c != sepc2)
123*17f9a364Spgoyette continue;
124*17f9a364Spgoyette }
125*17f9a364Spgoyette /* might have to trim trailing white space */
126*17f9a364Spgoyette if (trimtrail) {
127*17f9a364Spgoyette p--;
128*17f9a364Spgoyette while ((c = *--p) == sepc || c == sepc2)
129*17f9a364Spgoyette continue;
130*17f9a364Spgoyette p++;
131*17f9a364Spgoyette if (*p != '\0') {
132*17f9a364Spgoyette if (fn == nfields+1)
133*17f9a364Spgoyette *p = '\0';
134*17f9a364Spgoyette fn--;
135*17f9a364Spgoyette }
136*17f9a364Spgoyette }
137*17f9a364Spgoyette return(fn);
138*17f9a364Spgoyette }
139*17f9a364Spgoyette
140*17f9a364Spgoyette /* n separators */
141*17f9a364Spgoyette fn = 0;
142*17f9a364Spgoyette for (;;) {
143*17f9a364Spgoyette if (fn < nfields)
144*17f9a364Spgoyette *fp++ = p;
145*17f9a364Spgoyette fn++;
146*17f9a364Spgoyette for (;;) {
147*17f9a364Spgoyette c = *p++;
148*17f9a364Spgoyette if (c == '\0')
149*17f9a364Spgoyette return(fn);
150*17f9a364Spgoyette sepp = sep;
151*17f9a364Spgoyette while ((sepc = *sepp++) != '\0' && sepc != c)
152*17f9a364Spgoyette continue;
153*17f9a364Spgoyette if (sepc != '\0') /* it was a separator */
154*17f9a364Spgoyette break;
155*17f9a364Spgoyette }
156*17f9a364Spgoyette if (fn < nfields)
157*17f9a364Spgoyette *(p-1) = '\0';
158*17f9a364Spgoyette for (;;) {
159*17f9a364Spgoyette c = *p++;
160*17f9a364Spgoyette sepp = sep;
161*17f9a364Spgoyette while ((sepc = *sepp++) != '\0' && sepc != c)
162*17f9a364Spgoyette continue;
163*17f9a364Spgoyette if (sepc == '\0') /* it wasn't a separator */
164*17f9a364Spgoyette break;
165*17f9a364Spgoyette }
166*17f9a364Spgoyette p--;
167*17f9a364Spgoyette }
168*17f9a364Spgoyette
169*17f9a364Spgoyette /* not reached */
170*17f9a364Spgoyette }
171*17f9a364Spgoyette
172*17f9a364Spgoyette #ifdef TEST_SPLIT
173*17f9a364Spgoyette
174*17f9a364Spgoyette
175*17f9a364Spgoyette /*
176*17f9a364Spgoyette * test program
177*17f9a364Spgoyette * pgm runs regression
178*17f9a364Spgoyette * pgm sep splits stdin lines by sep
179*17f9a364Spgoyette * pgm str sep splits str by sep
180*17f9a364Spgoyette * pgm str sep n splits str by sep n times
181*17f9a364Spgoyette */
182*17f9a364Spgoyette int
main(int argc,char * argv[])183*17f9a364Spgoyette main(int argc, char *argv[])
184*17f9a364Spgoyette {
185*17f9a364Spgoyette char buf[512];
186*17f9a364Spgoyette int n;
187*17f9a364Spgoyette # define MNF 10
188*17f9a364Spgoyette char *fields[MNF];
189*17f9a364Spgoyette
190*17f9a364Spgoyette if (argc > 4)
191*17f9a364Spgoyette for (n = atoi(argv[3]); n > 0; n--) {
192*17f9a364Spgoyette (void) strcpy(buf, argv[1]);
193*17f9a364Spgoyette }
194*17f9a364Spgoyette else if (argc > 3)
195*17f9a364Spgoyette for (n = atoi(argv[3]); n > 0; n--) {
196*17f9a364Spgoyette (void) strcpy(buf, argv[1]);
197*17f9a364Spgoyette (void) split(buf, fields, MNF, argv[2]);
198*17f9a364Spgoyette }
199*17f9a364Spgoyette else if (argc > 2)
200*17f9a364Spgoyette dosplit(argv[1], argv[2]);
201*17f9a364Spgoyette else if (argc > 1)
202*17f9a364Spgoyette while (fgets(buf, sizeof(buf), stdin) != NULL) {
203*17f9a364Spgoyette buf[strlen(buf)-1] = '\0'; /* stomp newline */
204*17f9a364Spgoyette dosplit(buf, argv[1]);
205*17f9a364Spgoyette }
206*17f9a364Spgoyette else
207*17f9a364Spgoyette regress();
208*17f9a364Spgoyette
209*17f9a364Spgoyette exit(0);
210*17f9a364Spgoyette }
211*17f9a364Spgoyette
212*17f9a364Spgoyette void
dosplit(char * string,char * seps)213*17f9a364Spgoyette dosplit(char *string, char *seps)
214*17f9a364Spgoyette {
215*17f9a364Spgoyette # define NF 5
216*17f9a364Spgoyette char *fields[NF];
217*17f9a364Spgoyette int nf;
218*17f9a364Spgoyette
219*17f9a364Spgoyette nf = split(string, fields, NF, seps);
220*17f9a364Spgoyette print(nf, NF, fields);
221*17f9a364Spgoyette }
222*17f9a364Spgoyette
223*17f9a364Spgoyette void
print(int nf,int nfp,char * fields)224*17f9a364Spgoyette print(int nf, int nfp, char *fields)
225*17f9a364Spgoyette {
226*17f9a364Spgoyette int fn;
227*17f9a364Spgoyette int bound;
228*17f9a364Spgoyette
229*17f9a364Spgoyette bound = (nf > nfp) ? nfp : nf;
230*17f9a364Spgoyette printf("%d:\t", nf);
231*17f9a364Spgoyette for (fn = 0; fn < bound; fn++)
232*17f9a364Spgoyette printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
233*17f9a364Spgoyette }
234*17f9a364Spgoyette
235*17f9a364Spgoyette #define RNF 5 /* some table entries know this */
236*17f9a364Spgoyette struct {
237*17f9a364Spgoyette char *str;
238*17f9a364Spgoyette char *seps;
239*17f9a364Spgoyette int nf;
240*17f9a364Spgoyette char *fi[RNF];
241*17f9a364Spgoyette } tests[] = {
242*17f9a364Spgoyette "", " ", 0, { "" },
243*17f9a364Spgoyette " ", " ", 2, { "", "" },
244*17f9a364Spgoyette "x", " ", 1, { "x" },
245*17f9a364Spgoyette "xy", " ", 1, { "xy" },
246*17f9a364Spgoyette "x y", " ", 2, { "x", "y" },
247*17f9a364Spgoyette "abc def g ", " ", 5, { "abc", "def", "", "g", "" },
248*17f9a364Spgoyette " a bcd", " ", 4, { "", "", "a", "bcd" },
249*17f9a364Spgoyette "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
250*17f9a364Spgoyette " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
251*17f9a364Spgoyette
252*17f9a364Spgoyette "", " _", 0, { "" },
253*17f9a364Spgoyette " ", " _", 2, { "", "" },
254*17f9a364Spgoyette "x", " _", 1, { "x" },
255*17f9a364Spgoyette "x y", " _", 2, { "x", "y" },
256*17f9a364Spgoyette "ab _ cd", " _", 2, { "ab", "cd" },
257*17f9a364Spgoyette " a_b c ", " _", 5, { "", "a", "b", "c", "" },
258*17f9a364Spgoyette "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
259*17f9a364Spgoyette " a b c d ", " _", 6, { "", "a", "b", "c", "d " },
260*17f9a364Spgoyette
261*17f9a364Spgoyette "", " _~", 0, { "" },
262*17f9a364Spgoyette " ", " _~", 2, { "", "" },
263*17f9a364Spgoyette "x", " _~", 1, { "x" },
264*17f9a364Spgoyette "x y", " _~", 2, { "x", "y" },
265*17f9a364Spgoyette "ab _~ cd", " _~", 2, { "ab", "cd" },
266*17f9a364Spgoyette " a_b c~", " _~", 5, { "", "a", "b", "c", "" },
267*17f9a364Spgoyette "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
268*17f9a364Spgoyette "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
269*17f9a364Spgoyette
270*17f9a364Spgoyette "", " _~-", 0, { "" },
271*17f9a364Spgoyette " ", " _~-", 2, { "", "" },
272*17f9a364Spgoyette "x", " _~-", 1, { "x" },
273*17f9a364Spgoyette "x y", " _~-", 2, { "x", "y" },
274*17f9a364Spgoyette "ab _~- cd", " _~-", 2, { "ab", "cd" },
275*17f9a364Spgoyette " a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
276*17f9a364Spgoyette "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
277*17f9a364Spgoyette "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
278*17f9a364Spgoyette
279*17f9a364Spgoyette "", " ", 0, { "" },
280*17f9a364Spgoyette " ", " ", 2, { "", "" },
281*17f9a364Spgoyette "x", " ", 1, { "x" },
282*17f9a364Spgoyette "xy", " ", 1, { "xy" },
283*17f9a364Spgoyette "x y", " ", 2, { "x", "y" },
284*17f9a364Spgoyette "abc def g ", " ", 4, { "abc", "def", "g", "" },
285*17f9a364Spgoyette " a bcd", " ", 3, { "", "a", "bcd" },
286*17f9a364Spgoyette "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
287*17f9a364Spgoyette " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
288*17f9a364Spgoyette
289*17f9a364Spgoyette "", "", 0, { "" },
290*17f9a364Spgoyette " ", "", 0, { "" },
291*17f9a364Spgoyette "x", "", 1, { "x" },
292*17f9a364Spgoyette "xy", "", 1, { "xy" },
293*17f9a364Spgoyette "x y", "", 2, { "x", "y" },
294*17f9a364Spgoyette "abc def g ", "", 3, { "abc", "def", "g" },
295*17f9a364Spgoyette "\t a bcd", "", 2, { "a", "bcd" },
296*17f9a364Spgoyette " a \tb\t c ", "", 3, { "a", "b", "c" },
297*17f9a364Spgoyette "a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
298*17f9a364Spgoyette "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
299*17f9a364Spgoyette " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
300*17f9a364Spgoyette
301*17f9a364Spgoyette NULL, NULL, 0, { NULL },
302*17f9a364Spgoyette };
303*17f9a364Spgoyette
304*17f9a364Spgoyette void
regress(void)305*17f9a364Spgoyette regress(void)
306*17f9a364Spgoyette {
307*17f9a364Spgoyette char buf[512];
308*17f9a364Spgoyette int n;
309*17f9a364Spgoyette char *fields[RNF+1];
310*17f9a364Spgoyette int nf;
311*17f9a364Spgoyette int i;
312*17f9a364Spgoyette int printit;
313*17f9a364Spgoyette char *f;
314*17f9a364Spgoyette
315*17f9a364Spgoyette for (n = 0; tests[n].str != NULL; n++) {
316*17f9a364Spgoyette (void) strcpy(buf, tests[n].str);
317*17f9a364Spgoyette fields[RNF] = NULL;
318*17f9a364Spgoyette nf = split(buf, fields, RNF, tests[n].seps);
319*17f9a364Spgoyette printit = 0;
320*17f9a364Spgoyette if (nf != tests[n].nf) {
321*17f9a364Spgoyette printf("split `%s' by `%s' gave %d fields, not %d\n",
322*17f9a364Spgoyette tests[n].str, tests[n].seps, nf, tests[n].nf);
323*17f9a364Spgoyette printit = 1;
324*17f9a364Spgoyette } else if (fields[RNF] != NULL) {
325*17f9a364Spgoyette printf("split() went beyond array end\n");
326*17f9a364Spgoyette printit = 1;
327*17f9a364Spgoyette } else {
328*17f9a364Spgoyette for (i = 0; i < nf && i < RNF; i++) {
329*17f9a364Spgoyette f = fields[i];
330*17f9a364Spgoyette if (f == NULL)
331*17f9a364Spgoyette f = "(NULL)";
332*17f9a364Spgoyette if (strcmp(f, tests[n].fi[i]) != 0) {
333*17f9a364Spgoyette printf("split `%s' by `%s', field %d is `%s', not `%s'\n",
334*17f9a364Spgoyette tests[n].str, tests[n].seps,
335*17f9a364Spgoyette i, fields[i], tests[n].fi[i]);
336*17f9a364Spgoyette printit = 1;
337*17f9a364Spgoyette }
338*17f9a364Spgoyette }
339*17f9a364Spgoyette }
340*17f9a364Spgoyette if (printit)
341*17f9a364Spgoyette print(nf, RNF, fields);
342*17f9a364Spgoyette }
343*17f9a364Spgoyette }
344*17f9a364Spgoyette #endif
345