1 /* $NetBSD: split.c,v 1.4 2005/02/06 06:05:19 perry 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 - split - divide a string into fields, like awk split()
34 = int split(char *string, char *fields[], int nfields, char *sep);
36 int /* number of fields, including overflow */
37 split(string
, fields
, nfields
, sep
)
39 char *fields
[]; /* list is not NULL-terminated */
40 int nfields
; /* number of entries available in fields[] */
41 char *sep
; /* "" white, "c" single char, "ab" [ab]+ */
44 char c
; /* latest character */
54 while ((c
= *p
++) == ' ' || c
== '\t')
58 sep
= " \t"; /* note, code below knows this is 2 long */
62 sepc2
= sep
[1]; /* now we can safely pick this up */
68 /* single separator */
76 while ((c
= *p
++) != sepc
)
81 /* we have overflowed the fields vector -- just count them */
84 while ((c
= *p
++) != sepc
)
98 while ((c
= *p
++) != sepc
&& c
!= sepc2
)
100 if (trimtrail
&& **(fp
-1) == '\0')
102 return(nfields
- fn
);
107 while ((c
= *p
++) == sepc
|| c
== sepc2
)
111 /* we have overflowed the fields vector -- just count them */
114 while ((c
= *p
++) == sepc
|| c
== sepc2
)
118 while ((c
= *p
++) != '\0' && c
!= sepc
&& c
!= sepc2
)
121 /* might have to trim trailing white space */
124 while ((c
= *--p
) == sepc
|| c
== sepc2
)
147 while ((sepc
= *sepp
++) != '\0' && sepc
!= c
)
149 if (sepc
!= '\0') /* it was a separator */
157 while ((sepc
= *sepp
++) != '\0' && sepc
!= c
)
159 if (sepc
== '\0') /* it wasn't a separator */
173 * pgm runs regression
174 * pgm sep splits stdin lines by sep
175 * pgm str sep splits str by sep
176 * pgm str sep n splits str by sep n times
189 for (n
= atoi(argv
[3]); n
> 0; n
--) {
190 (void) strcpy(buf
, argv
[1]);
193 for (n
= atoi(argv
[3]); n
> 0; n
--) {
194 (void) strcpy(buf
, argv
[1]);
195 (void) split(buf
, fields
, MNF
, argv
[2]);
198 dosplit(argv
[1], argv
[2]);
200 while (fgets(buf
, sizeof(buf
), stdin
) != NULL
) {
201 buf
[strlen(buf
)-1] = '\0'; /* stomp newline */
202 dosplit(buf
, argv
[1]);
210 dosplit(string
, seps
)
218 nf
= split(string
, fields
, NF
, seps
);
219 print(nf
, NF
, fields
);
222 print(nf
, nfp
, fields
)
230 bound
= (nf
> nfp
) ? nfp
: nf
;
232 for (fn
= 0; fn
< bound
; fn
++)
233 printf("\"%s\"%s", fields
[fn
], (fn
+1 < nf
) ? ", " : "\n");
236 #define RNF 5 /* some table entries know this */
244 " ", " ", 2, { "", "" },
245 "x", " ", 1, { "x" },
246 "xy", " ", 1, { "xy" },
247 "x y", " ", 2, { "x", "y" },
248 "abc def g ", " ", 5, { "abc", "def", "", "g", "" },
249 " a bcd", " ", 4, { "", "", "a", "bcd" },
250 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
251 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
254 " ", " _", 2, { "", "" },
255 "x", " _", 1, { "x" },
256 "x y", " _", 2, { "x", "y" },
257 "ab _ cd", " _", 2, { "ab", "cd" },
258 " a_b c ", " _", 5, { "", "a", "b", "c", "" },
259 "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
260 " a b c d ", " _", 6, { "", "a", "b", "c", "d " },
262 "", " _~", 0, { "" },
263 " ", " _~", 2, { "", "" },
264 "x", " _~", 1, { "x" },
265 "x y", " _~", 2, { "x", "y" },
266 "ab _~ cd", " _~", 2, { "ab", "cd" },
267 " a_b c~", " _~", 5, { "", "a", "b", "c", "" },
268 "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
269 "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
271 "", " _~-", 0, { "" },
272 " ", " _~-", 2, { "", "" },
273 "x", " _~-", 1, { "x" },
274 "x y", " _~-", 2, { "x", "y" },
275 "ab _~- cd", " _~-", 2, { "ab", "cd" },
276 " a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
277 "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
278 "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
281 " ", " ", 2, { "", "" },
282 "x", " ", 1, { "x" },
283 "xy", " ", 1, { "xy" },
284 "x y", " ", 2, { "x", "y" },
285 "abc def g ", " ", 4, { "abc", "def", "g", "" },
286 " a bcd", " ", 3, { "", "a", "bcd" },
287 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
288 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
293 "xy", "", 1, { "xy" },
294 "x y", "", 2, { "x", "y" },
295 "abc def g ", "", 3, { "abc", "def", "g" },
296 "\t a bcd", "", 2, { "a", "bcd" },
297 " a \tb\t c ", "", 3, { "a", "b", "c" },
298 "a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
299 "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
300 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
302 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
);