Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libc / regex / split.c
blobdeee278460cabb5464a0f27632d1b74fa7a59d5c
1 /* $NetBSD: split.c,v 1.4 2005/02/06 06:05:19 perry Exp $ */
3 /*-
4 * Copyright (c) 1993 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
29 #include <stdio.h>
30 #include <string.h>
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)
38 char *string;
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]+ */
43 char *p = string;
44 char c; /* latest character */
45 char sepc = sep[0];
46 char sepc2;
47 int fn;
48 char **fp = fields;
49 char *sepp;
50 int trimtrail;
52 /* white space */
53 if (sepc == '\0') {
54 while ((c = *p++) == ' ' || c == '\t')
55 continue;
56 p--;
57 trimtrail = 1;
58 sep = " \t"; /* note, code below knows this is 2 long */
59 sepc = ' ';
60 } else
61 trimtrail = 0;
62 sepc2 = sep[1]; /* now we can safely pick this up */
64 /* catch empties */
65 if (*p == '\0')
66 return(0);
68 /* single separator */
69 if (sepc2 == '\0') {
70 fn = nfields;
71 for (;;) {
72 *fp++ = p;
73 fn--;
74 if (fn == 0)
75 break;
76 while ((c = *p++) != sepc)
77 if (c == '\0')
78 return(nfields - fn);
79 *(p-1) = '\0';
81 /* we have overflowed the fields vector -- just count them */
82 fn = nfields;
83 for (;;) {
84 while ((c = *p++) != sepc)
85 if (c == '\0')
86 return(fn);
87 fn++;
89 /* not reached */
92 /* two separators */
93 if (sep[2] == '\0') {
94 fn = nfields;
95 for (;;) {
96 *fp++ = p;
97 fn--;
98 while ((c = *p++) != sepc && c != sepc2)
99 if (c == '\0') {
100 if (trimtrail && **(fp-1) == '\0')
101 fn++;
102 return(nfields - fn);
104 if (fn == 0)
105 break;
106 *(p-1) = '\0';
107 while ((c = *p++) == sepc || c == sepc2)
108 continue;
109 p--;
111 /* we have overflowed the fields vector -- just count them */
112 fn = nfields;
113 while (c != '\0') {
114 while ((c = *p++) == sepc || c == sepc2)
115 continue;
116 p--;
117 fn++;
118 while ((c = *p++) != '\0' && c != sepc && c != sepc2)
119 continue;
121 /* might have to trim trailing white space */
122 if (trimtrail) {
123 p--;
124 while ((c = *--p) == sepc || c == sepc2)
125 continue;
126 p++;
127 if (*p != '\0') {
128 if (fn == nfields+1)
129 *p = '\0';
130 fn--;
133 return(fn);
136 /* n separators */
137 fn = 0;
138 for (;;) {
139 if (fn < nfields)
140 *fp++ = p;
141 fn++;
142 for (;;) {
143 c = *p++;
144 if (c == '\0')
145 return(fn);
146 sepp = sep;
147 while ((sepc = *sepp++) != '\0' && sepc != c)
148 continue;
149 if (sepc != '\0') /* it was a separator */
150 break;
152 if (fn < nfields)
153 *(p-1) = '\0';
154 for (;;) {
155 c = *p++;
156 sepp = sep;
157 while ((sepc = *sepp++) != '\0' && sepc != c)
158 continue;
159 if (sepc == '\0') /* it wasn't a separator */
160 break;
162 p--;
165 /* not reached */
168 #ifdef TEST_SPLIT
172 * test program
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
179 main(argc, argv)
180 int argc;
181 char *argv[];
183 char buf[512];
184 int n;
185 # define MNF 10
186 char *fields[MNF];
188 if (argc > 4)
189 for (n = atoi(argv[3]); n > 0; n--) {
190 (void) strcpy(buf, argv[1]);
192 else if (argc > 3)
193 for (n = atoi(argv[3]); n > 0; n--) {
194 (void) strcpy(buf, argv[1]);
195 (void) split(buf, fields, MNF, argv[2]);
197 else if (argc > 2)
198 dosplit(argv[1], argv[2]);
199 else if (argc > 1)
200 while (fgets(buf, sizeof(buf), stdin) != NULL) {
201 buf[strlen(buf)-1] = '\0'; /* stomp newline */
202 dosplit(buf, argv[1]);
204 else
205 regress();
207 exit(0);
210 dosplit(string, seps)
211 char *string;
212 char *seps;
214 # define NF 5
215 char *fields[NF];
216 int nf;
218 nf = split(string, fields, NF, seps);
219 print(nf, NF, fields);
222 print(nf, nfp, fields)
223 int nf;
224 int nfp;
225 char *fields[];
227 int fn;
228 int bound;
230 bound = (nf > nfp) ? nfp : nf;
231 printf("%d:\t", 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 */
237 struct {
238 char *str;
239 char *seps;
240 int nf;
241 char *fi[RNF];
242 } tests[] = {
243 "", " ", 0, { "" },
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 " },
253 "", " _", 0, { "" },
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 " },
280 "", " ", 0, { "" },
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 " },
290 "", "", 0, { "" },
291 " ", "", 0, { "" },
292 "x", "", 1, { "x" },
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 },
305 regress()
307 char buf[512];
308 int n;
309 char *fields[RNF+1];
310 int nf;
311 int i;
312 int printit;
313 char *f;
315 for (n = 0; tests[n].str != NULL; n++) {
316 (void) strcpy(buf, tests[n].str);
317 fields[RNF] = NULL;
318 nf = split(buf, fields, RNF, tests[n].seps);
319 printit = 0;
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);
323 printit = 1;
324 } else if (fields[RNF] != NULL) {
325 printf("split() went beyond array end\n");
326 printit = 1;
327 } else {
328 for (i = 0; i < nf && i < RNF; i++) {
329 f = fields[i];
330 if (f == NULL)
331 f = "(NULL)";
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]);
336 printit = 1;
340 if (printit)
341 print(nf, RNF, fields);
344 #endif