2 * Copyright (c) 1993 The NetBSD Foundation, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
31 #include "test_regex.h"
34 * split - divide a string into fields, like awk split()
36 * returns number of fields, including overflow
38 * fields[] list is not NULL-terminated
39 * nfields number of entries available in fields[]
40 * sep "" white, "c" single char, "ab" [ab]+
43 split(char *string
, char *fields
[], int nfields
, const char *sep
)
46 char c
; /* latest character */
56 while ((c
= *p
++) == ' ' || c
== '\t')
60 sep
= " \t"; /* note, code below knows this is 2 long */
64 sepc2
= sep
[1]; /* now we can safely pick this up */
70 /* single separator */
78 while ((c
= *p
++) != sepc
)
80 return (nfields
- fn
);
83 /* we have overflowed the fields vector -- just count them */
86 while ((c
= *p
++) != sepc
)
100 while ((c
= *p
++) != sepc
&& c
!= sepc2
)
102 if (trimtrail
&& **(fp
-1) == '\0')
104 return (nfields
- fn
);
109 while ((c
= *p
++) == sepc
|| c
== sepc2
)
113 /* we have overflowed the fields vector -- just count them */
116 while ((c
= *p
++) == sepc
|| c
== sepc2
)
120 while ((c
= *p
++) != '\0' && c
!= sepc
&& c
!= sepc2
)
123 /* might have to trim trailing white space */
126 while ((c
= *--p
) == sepc
|| c
== sepc2
)
149 while ((sepc
= *sepp
++) != '\0' && sepc
!= c
)
151 if (sepc
!= '\0') /* it was a separator */
159 while ((sepc
= *sepp
++) != '\0' && sepc
!= c
)
161 if (sepc
== '\0') /* it wasn't a separator */