diff options
Diffstat (limited to 'pjlib')
-rw-r--r-- | pjlib/include/pj/string.h | 87 | ||||
-rw-r--r-- | pjlib/src/pj/os_info.c | 18 | ||||
-rw-r--r-- | pjlib/src/pj/string.c | 107 |
3 files changed, 205 insertions, 7 deletions
diff --git a/pjlib/include/pj/string.h b/pjlib/include/pj/string.h index cfbdd458..70a1d6c8 100644 --- a/pjlib/include/pj/string.h +++ b/pjlib/include/pj/string.h @@ -474,6 +474,93 @@ PJ_INLINE(char*) pj_strchr( const pj_str_t *str, int chr) return (char*) memchr((char*)str->ptr, chr, str->slen); } + +/** + * Find the first index of character, in a string, that does not belong to a + * set of characters. + * + * @param str The string. + * @param set_char The string containing the set of characters. + * + * @return the index of the first character in the str that doesn't belong to + * set_char. If str starts with a character not in set_char, return 0. + */ +PJ_DECL(pj_ssize_t) pj_strspn(const pj_str_t *str, const pj_str_t *set_char); + + +/** + * Find the first index of character, in a string, that does not belong to a + * set of characters. + * + * @param str The string. + * @param set_char The string containing the set of characters. + * + * @return the index of the first character in the str that doesn't belong to + * set_char. If str starts with a character not in set_char, return 0. + */ +PJ_DECL(pj_ssize_t) pj_strspn2(const pj_str_t *str, const char *set_char); + + +/** + * Find the first index of character, in a string, that belong to a set of + * characters. + * + * @param str The string. + * @param set_char The string containing the set of characters. + * + * @return the index of the first character in the str that belong to + * set_char. If no match is found, return the length of str. + */ +PJ_DECL(pj_ssize_t) pj_strcspn(const pj_str_t *str, const pj_str_t *set_char); + + +/** + * Find the first index of character, in a string, that belong to a set of + * characters. + * + * @param str The string. + * @param set_char The string containing the set of characters. + * + * @return the index of the first character in the str that belong to + * set_char. If no match is found, return the length of str. + */ +PJ_DECL(pj_ssize_t) pj_strcspn2(const pj_str_t *str, const char *set_char); + + +/** + * Find tokens from a string using the delimiter. + * + * @param str The string. + * @param delim The string containing the delimiter. It might contain + * multiple character treated as unique set. If same character + * was found on the set, it will be skipped. + * @param tok The string containing the token. + * @param start_idx The search will start from this index. + * + * @return the index of token from the str, or the length of the str + * if the token is not found. + */ +PJ_DECL(pj_ssize_t) pj_strtok(const pj_str_t *str, const pj_str_t *delim, + pj_str_t *tok, pj_size_t start_idx); + + +/** + * Find tokens from a string using the delimiter. + * + * @param str The string. + * @param delim The string containing the delimiter. It might contain + * multiple character treated as unique set. If same character + * was found on the set, it will be skipped. + * @param tok The string containing the token. + * @param start_idx The search will start from this index. + * + * @return the index of token from the str, or the length of the str + * if the token is not found. + */ +PJ_DECL(pj_ssize_t) pj_strtok2(const pj_str_t *str, const char *delim, + pj_str_t *tok, pj_size_t start_idx); + + /** * Find the occurence of a substring substr in string str. * diff --git a/pjlib/src/pj/os_info.c b/pjlib/src/pj/os_info.c index 514ff242..3bf11670 100644 --- a/pjlib/src/pj/os_info.c +++ b/pjlib/src/pj/os_info.c @@ -98,24 +98,28 @@ static char *ver_info(pj_uint32_t ver, char *buf) } static pj_uint32_t parse_version(char *str) -{ - char *tok; - int i, maxtok; +{ + int i, maxtok, found_idx; pj_uint32_t version = 0; + pj_str_t in_str = pj_str(str); + pj_str_t token, delim; while (*str && !pj_isdigit(*str)) str++; maxtok = 4; - for (tok = strtok(str, ".-"), i=0; tok && i<maxtok; - ++i, tok=strtok(NULL, ".-")) + delim = pj_str(".-"); + for (found_idx = pj_strtok(&in_str, &delim, &token, 0), i=0; + found_idx != in_str.slen && i < maxtok; + ++i, found_idx = pj_strtok(&in_str, &delim, &token, + found_idx + token.slen)) { int n; - if (!pj_isdigit(*tok)) + if (!pj_isdigit(*token.ptr)) break; - n = atoi(tok); + n = atoi(token.ptr); version |= (n << ((3-i)*8)); } diff --git a/pjlib/src/pj/string.c b/pjlib/src/pj/string.c index d62b674a..307cfb47 100644 --- a/pjlib/src/pj/string.c +++ b/pjlib/src/pj/string.c @@ -28,6 +28,113 @@ # include <pj/string_i.h> #endif +PJ_DEF(pj_ssize_t) pj_strspn(const pj_str_t *str, const pj_str_t *set_char) +{ + pj_ssize_t i, j, count = 0; + for (i = 0; i < str->slen; i++) { + if (count != i) + break; + + for (j = 0; j < set_char->slen; j++) { + if (str->ptr[i] == set_char->ptr[j]) + count++; + } + } + return count; +} + + +PJ_DEF(pj_ssize_t) pj_strspn2(const pj_str_t *str, const char *set_char) +{ + pj_ssize_t i, j, count = 0; + for (i = 0; i < str->slen; i++) { + if (count != i) + break; + + for (j = 0; set_char[j] != 0; j++) { + if (str->ptr[i] == set_char[j]) + count++; + } + } + return count; +} + + +PJ_DEF(pj_ssize_t) pj_strcspn(const pj_str_t *str, const pj_str_t *set_char) +{ + pj_ssize_t i, j; + for (i = 0; i < str->slen; i++) { + for (j = 0; j < set_char->slen; j++) { + if (str->ptr[i] == set_char->ptr[j]) + return i; + } + } + return i; +} + + +PJ_DECL(pj_ssize_t) pj_strcspn2(const pj_str_t *str, const char *set_char) +{ + pj_ssize_t i, j; + for (i = 0; i < str->slen; i++) { + for (j = 0; set_char[j] != 0; j++) { + if (str->ptr[i] == set_char[j]) + return i; + } + } + return i; +} + + +PJ_DEF(pj_ssize_t) pj_strtok(const pj_str_t *str, const pj_str_t *delim, + pj_str_t *tok, pj_size_t start_idx) +{ + pj_ssize_t str_idx; + + tok->slen = 0; + if ((str->slen == 0) || ((pj_size_t)str->slen < start_idx)) { + return str->slen; + } + + tok->ptr = str->ptr + start_idx; + tok->slen = str->slen - start_idx; + + str_idx = pj_strspn(tok, delim); + if (start_idx+str_idx == (pj_size_t)str->slen) { + return str->slen; + } + tok->ptr += str_idx; + tok->slen -= str_idx; + + tok->slen = pj_strcspn(tok, delim); + return start_idx + str_idx; +} + + +PJ_DECL(pj_ssize_t) pj_strtok2(const pj_str_t *str, const char *delim, + pj_str_t *tok, pj_size_t start_idx) +{ + pj_ssize_t str_idx; + + tok->slen = 0; + if ((str->slen == 0) || ((pj_size_t)str->slen < start_idx)) { + return str->slen; + } + + tok->ptr = str->ptr + start_idx; + tok->slen = str->slen - start_idx; + + str_idx = pj_strspn2(tok, delim); + if (start_idx + str_idx == (pj_size_t)str->slen) { + return str->slen; + } + tok->ptr += str_idx; + tok->slen -= str_idx; + + tok->slen = pj_strcspn2(tok, delim); + return start_idx + str_idx; +} + PJ_DEF(char*) pj_strstr(const pj_str_t *str, const pj_str_t *substr) { |