1 /* $NetBSD: split.c,v 1.1 2011/01/08 18:10:31 pgoyette Exp $ */
4 * Copyright (c) 1993 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
33 #include "test_regex.h"
36 * split - divide a string into fields, like awk split()
38 * returns number of fields, including overflow
40 * fields[] list is not NULL-terminated
41 * nfields number of entries available in fields[]
42 * sep "" white, "c" single char, "ab" [ab]+
45 split(char *string
, char *fields
[], int nfields
, const char *sep
)
48 char c
; /* latest character */
58 while ((c
= *p
++) == ' ' || c
== '\t')
62 sep
= " \t"; /* note, code below knows this is 2 long */
66 sepc2
= sep
[1]; /* now we can safely pick this up */
72 /* single separator */
80 while ((c
= *p
++) != sepc
)
85 /* we have overflowed the fields vector -- just count them */
88 while ((c
= *p
++) != sepc
)
102 while ((c
= *p
++) != sepc
&& c
!= sepc2
)
104 if (trimtrail
&& **(fp
-1) == '\0')
106 return(nfields
- fn
);
111 while ((c
= *p
++) == sepc
|| c
== sepc2
)
115 /* we have overflowed the fields vector -- just count them */
118 while ((c
= *p
++) == sepc
|| c
== sepc2
)
122 while ((c
= *p
++) != '\0' && c
!= sepc
&& c
!= sepc2
)
125 /* might have to trim trailing white space */
128 while ((c
= *--p
) == sepc
|| c
== sepc2
)
151 while ((sepc
= *sepp
++) != '\0' && sepc
!= c
)
153 if (sepc
!= '\0') /* it was a separator */
161 while ((sepc
= *sepp
++) != '\0' && sepc
!= c
)
163 if (sepc
== '\0') /* it wasn't a separator */
177 * pgm runs regression
178 * pgm sep splits stdin lines by sep
179 * pgm str sep splits str by sep
180 * pgm str sep n splits str by sep n times
183 main(int argc
, char *argv
[])
191 for (n
= atoi(argv
[3]); n
> 0; n
--) {
192 (void) strcpy(buf
, argv
[1]);
195 for (n
= atoi(argv
[3]); n
> 0; n
--) {
196 (void) strcpy(buf
, argv
[1]);
197 (void) split(buf
, fields
, MNF
, argv
[2]);
200 dosplit(argv
[1], argv
[2]);
202 while (fgets(buf
, sizeof(buf
), stdin
) != NULL
) {
203 buf
[strlen(buf
)-1] = '\0'; /* stomp newline */
204 dosplit(buf
, argv
[1]);
213 dosplit(char *string
, char *seps
)
219 nf
= split(string
, fields
, NF
, seps
);
220 print(nf
, NF
, fields
);
224 print(int nf
, int nfp
, char *fields
)
229 bound
= (nf
> nfp
) ? nfp
: nf
;
231 for (fn
= 0; fn
< bound
; fn
++)
232 printf("\"%s\"%s", fields
[fn
], (fn
+1 < nf
) ? ", " : "\n");
235 #define RNF 5 /* some table entries know this */
243 " ", " ", 2, { "", "" },
244 "x", " ", 1, { "x" },
245 "xy", " ", 1, { "xy" },
246 "x y", " ", 2, { "x", "y" },
247 "abc def g ", " ", 5, { "abc", "def", "", "g", "" },
248 " a bcd", " ", 4, { "", "", "a", "bcd" },
249 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
250 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
253 " ", " _", 2, { "", "" },
254 "x", " _", 1, { "x" },
255 "x y", " _", 2, { "x", "y" },
256 "ab _ cd", " _", 2, { "ab", "cd" },
257 " a_b c ", " _", 5, { "", "a", "b", "c", "" },
258 "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
259 " a b c d ", " _", 6, { "", "a", "b", "c", "d " },
261 "", " _~", 0, { "" },
262 " ", " _~", 2, { "", "" },
263 "x", " _~", 1, { "x" },
264 "x y", " _~", 2, { "x", "y" },
265 "ab _~ cd", " _~", 2, { "ab", "cd" },
266 " a_b c~", " _~", 5, { "", "a", "b", "c", "" },
267 "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
268 "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
270 "", " _~-", 0, { "" },
271 " ", " _~-", 2, { "", "" },
272 "x", " _~-", 1, { "x" },
273 "x y", " _~-", 2, { "x", "y" },
274 "ab _~- cd", " _~-", 2, { "ab", "cd" },
275 " a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
276 "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
277 "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
280 " ", " ", 2, { "", "" },
281 "x", " ", 1, { "x" },
282 "xy", " ", 1, { "xy" },
283 "x y", " ", 2, { "x", "y" },
284 "abc def g ", " ", 4, { "abc", "def", "g", "" },
285 " a bcd", " ", 3, { "", "a", "bcd" },
286 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
287 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
292 "xy", "", 1, { "xy" },
293 "x y", "", 2, { "x", "y" },
294 "abc def g ", "", 3, { "abc", "def", "g" },
295 "\t a bcd", "", 2, { "a", "bcd" },
296 " a \tb\t c ", "", 3, { "a", "b", "c" },
297 "a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
298 "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
299 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
301 NULL
, NULL
, 0, { NULL
},
315 for (n
= 0; tests
[n
].str
!= NULL
; n
++) {
316 (void) strcpy(buf
, tests
[n
].str
);
318 nf
= split(buf
, fields
, RNF
, tests
[n
].seps
);
320 if (nf
!= tests
[n
].nf
) {
321 printf("split `%s' by `%s' gave %d fields, not %d\n",
322 tests
[n
].str
, tests
[n
].seps
, nf
, tests
[n
].nf
);
324 } else if (fields
[RNF
] != NULL
) {
325 printf("split() went beyond array end\n");
328 for (i
= 0; i
< nf
&& i
< RNF
; i
++) {
332 if (strcmp(f
, tests
[n
].fi
[i
]) != 0) {
333 printf("split `%s' by `%s', field %d is `%s', not `%s'\n",
334 tests
[n
].str
, tests
[n
].seps
,
335 i
, fields
[i
], tests
[n
].fi
[i
]);
341 print(nf
, RNF
, fields
);