diff options
author | Nick Mathewson <nickm@torproject.org> | 2004-09-02 18:25:50 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2004-09-02 18:25:50 +0000 |
commit | f311206d1db3bb88f686cfb52a4c503e97cbe249 (patch) | |
tree | 365adfb183859ff5a52cd2f8d5f017efaa226db7 /src/common/util.c | |
parent | 3590eb535a539911cf899511f6f3f21c745c65b8 (diff) | |
download | tor-f311206d1db3bb88f686cfb52a4c503e97cbe249.tar tor-f311206d1db3bb88f686cfb52a4c503e97cbe249.tar.gz |
Make split function smarter; add a strcmpstart function so we can stop bungling strcmp(x, y, strlen(y));
svn:r2325
Diffstat (limited to 'src/common/util.c')
-rw-r--r-- | src/common/util.c | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/src/common/util.c b/src/common/util.c index 4886f9b10..cc589bdfb 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -482,12 +482,16 @@ void smartlist_insert(smartlist_t *sl, int idx, void *val) } /** - * Split a string <b>str</b> along all occurences of <b>sep</b>, adding the - * split strings, in order, to <b>sl</b>. If <b>skipSpace</b> is true, - * remove initial and trailing space from each entry. + * Split a string <b>str</b> along all occurences of <b>sep</b>, + * adding the split strings, in order, to <b>sl</b>. If + * <b>flags</b>&SPLIT_SKIP_SPACE is true, remove initial and + * trailing space from each entry. If + * <b>flags</b>&SPLIT_IGNORE_BLANK is true, remove any entries of + * length 0. If max>0, divide the string into no more than <b>max</b> + * pieces. */ int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, - int skipSpace) + int flags, int max) { const char *cp, *end, *next; int n = 0; @@ -496,23 +500,31 @@ int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, cp = str; while (1) { - if (skipSpace) { + if (flags&SPLIT_SKIP_SPACE) { while (isspace((int)*cp)) ++cp; } - end = strstr(cp,sep); - if (!end) { + + if (max>0 && n == max-1) { end = strchr(cp,'\0'); + } else { + end = strstr(cp,sep); + if (!end) + end = strchr(cp,'\0'); + } + if (!*end) { next = NULL; } else { next = end+strlen(sep); } - if (skipSpace) { + if (flags&SPLIT_SKIP_SPACE) { while (end > cp && isspace((int)*(end-1))) --end; } - smartlist_add(sl, tor_strndup(cp, end-cp)); - ++n; + if (end != cp || !(flags&SPLIT_IGNORE_BLANK)) { + smartlist_add(sl, tor_strndup(cp, end-cp)); + ++n; + } if (!next) break; cp = next; @@ -799,6 +811,15 @@ void tor_strlower(char *s) } } +/* Compares the first strlen(s2) characters of s1 with s2. Returns as for + * strcmp. + */ +int strcmpstart(const char *s1, const char *s2) +{ + size_t n = strlen(s2); + return strncmp(s1, s2, n); +} + /** Return a pointer to the first char of s that is not whitespace and * not a comment, or to the terminating NUL if no such character exists. |