split.c (8ca5c256cb0a0ac117493557f3fc397f68f16759) | split.c (8fb3f3f68288ae2b1b53dd65e3dd673d83c80f4c) |
---|---|
1/* $FreeBSD$ 2 */ 3#include <stdio.h> 4#include <string.h> 5 6/* 7 - split - divide a string into fields, like awk split() 8 = int split(char *string, char *fields[], int nfields, char *sep); 9 */ 10int /* number of fields, including overflow */ 11split(string, fields, nfields, sep) 12char *string; 13char *fields[]; /* list is not NULL-terminated */ 14int nfields; /* number of entries available in fields[] */ 15char *sep; /* "" white, "c" single char, "ab" [ab]+ */ 16{ | 1/* $FreeBSD$ 2 */ 3#include <stdio.h> 4#include <string.h> 5 6/* 7 - split - divide a string into fields, like awk split() 8 = int split(char *string, char *fields[], int nfields, char *sep); 9 */ 10int /* number of fields, including overflow */ 11split(string, fields, nfields, sep) 12char *string; 13char *fields[]; /* list is not NULL-terminated */ 14int nfields; /* number of entries available in fields[] */ 15char *sep; /* "" white, "c" single char, "ab" [ab]+ */ 16{ |
17 register char *p = string; 18 register char c; /* latest character */ 19 register char sepc = sep[0]; 20 register char sepc2; 21 register int fn; 22 register char **fp = fields; 23 register char *sepp; 24 register int trimtrail; | 17 char *p = string; 18 char c; /* latest character */ 19 char sepc = sep[0]; 20 char sepc2; 21 int fn; 22 char **fp = fields; 23 char *sepp; 24 int trimtrail; |
25 26 /* white space */ 27 if (sepc == '\0') { 28 while ((c = *p++) == ' ' || c == '\t') 29 continue; 30 p--; 31 trimtrail = 1; 32 sep = " \t"; /* note, code below knows this is 2 long */ --- 117 unchanged lines hidden (view full) --- 150 * pgm str sep n splits str by sep n times 151 */ 152int 153main(argc, argv) 154int argc; 155char *argv[]; 156{ 157 char buf[512]; | 25 26 /* white space */ 27 if (sepc == '\0') { 28 while ((c = *p++) == ' ' || c == '\t') 29 continue; 30 p--; 31 trimtrail = 1; 32 sep = " \t"; /* note, code below knows this is 2 long */ --- 117 unchanged lines hidden (view full) --- 150 * pgm str sep n splits str by sep n times 151 */ 152int 153main(argc, argv) 154int argc; 155char *argv[]; 156{ 157 char buf[512]; |
158 register int n; | 158 int n; |
159# define MNF 10 160 char *fields[MNF]; 161 162 if (argc > 4) 163 for (n = atoi(argv[3]); n > 0; n--) { 164 (void) strcpy(buf, argv[1]); 165 } 166 else if (argc > 3) --- 15 unchanged lines hidden (view full) --- 182} 183 184dosplit(string, seps) 185char *string; 186char *seps; 187{ 188# define NF 5 189 char *fields[NF]; | 159# define MNF 10 160 char *fields[MNF]; 161 162 if (argc > 4) 163 for (n = atoi(argv[3]); n > 0; n--) { 164 (void) strcpy(buf, argv[1]); 165 } 166 else if (argc > 3) --- 15 unchanged lines hidden (view full) --- 182} 183 184dosplit(string, seps) 185char *string; 186char *seps; 187{ 188# define NF 5 189 char *fields[NF]; |
190 register int nf; | 190 int nf; |
191 192 nf = split(string, fields, NF, seps); 193 print(nf, NF, fields); 194} 195 196print(nf, nfp, fields) 197int nf; 198int nfp; 199char *fields[]; 200{ | 191 192 nf = split(string, fields, NF, seps); 193 print(nf, NF, fields); 194} 195 196print(nf, nfp, fields) 197int nf; 198int nfp; 199char *fields[]; 200{ |
201 register int fn; 202 register int bound; | 201 int fn; 202 int bound; |
203 204 bound = (nf > nfp) ? nfp : nf; 205 printf("%d:\t", nf); 206 for (fn = 0; fn < bound; fn++) 207 printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n"); 208} 209 210#define RNF 5 /* some table entries know this */ --- 63 unchanged lines hidden (view full) --- 274 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " }, 275 276 NULL, NULL, 0, { NULL }, 277}; 278 279regress() 280{ 281 char buf[512]; | 203 204 bound = (nf > nfp) ? nfp : nf; 205 printf("%d:\t", nf); 206 for (fn = 0; fn < bound; fn++) 207 printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n"); 208} 209 210#define RNF 5 /* some table entries know this */ --- 63 unchanged lines hidden (view full) --- 274 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " }, 275 276 NULL, NULL, 0, { NULL }, 277}; 278 279regress() 280{ 281 char buf[512]; |
282 register int n; | 282 int n; |
283 char *fields[RNF+1]; | 283 char *fields[RNF+1]; |
284 register int nf; 285 register int i; 286 register int printit; 287 register char *f; | 284 int nf; 285 int i; 286 int printit; 287 char *f; |
288 289 for (n = 0; tests[n].str != NULL; n++) { 290 (void) strcpy(buf, tests[n].str); 291 fields[RNF] = NULL; 292 nf = split(buf, fields, RNF, tests[n].seps); 293 printit = 0; 294 if (nf != tests[n].nf) { 295 printf("split `%s' by `%s' gave %d fields, not %d\n", --- 23 unchanged lines hidden --- | 288 289 for (n = 0; tests[n].str != NULL; n++) { 290 (void) strcpy(buf, tests[n].str); 291 fields[RNF] = NULL; 292 nf = split(buf, fields, RNF, tests[n].seps); 293 printit = 0; 294 if (nf != tests[n].nf) { 295 printf("split `%s' by `%s' gave %d fields, not %d\n", --- 23 unchanged lines hidden --- |