]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libString/s_parse.c
libaml: fix gc bug, need to amltake()/amldrop() temporary buffer
[plan9front.git] / sys / src / libString / s_parse.c
1 #include <u.h>
2 #include <libc.h>
3 #include "String.h"
4
5 #define isspace(c) ((c)==' ' || (c)=='\t' || (c)=='\n')
6
7 /* Get the next field from a String.  The field is delimited by white space,
8  * single or double quotes.
9  */
10 String *
11 s_parse(String *from, String *to)
12 {
13         if (*from->ptr == '\0')
14                 return 0;
15         if (to == 0)
16                 to = s_new();
17         if (*from->ptr == '\'') {
18                 from->ptr++;
19                 for (;*from->ptr != '\'' && *from->ptr != '\0'; from->ptr++)
20                         s_putc(to, *from->ptr);
21                 if (*from->ptr == '\'') 
22                         from->ptr++;
23         } else if (*from->ptr == '"') {
24                 from->ptr++;
25                 for (;*from->ptr != '"' && *from->ptr != '\0'; from->ptr++)
26                         s_putc(to, *from->ptr);
27                 if (*from->ptr == '"')  
28                         from->ptr++;
29         } else {
30                 for (;!isspace(*from->ptr) && *from->ptr != '\0'; from->ptr++)
31                         s_putc(to, *from->ptr);
32         }
33         s_terminate(to);
34
35         /* crunch trailing white */
36         while(isspace(*from->ptr))
37                 from->ptr++;
38
39         return to;
40 }