diff options
author | Benny Prijono <bennylp@teluu.com> | 2008-01-26 10:45:52 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2008-01-26 10:45:52 +0000 |
commit | f93f4b87a492ba5dff2854813463ca6fc419b393 (patch) | |
tree | bf9679e2bcdd699acbf7b3c285f49e8ecc54281d /pjlib | |
parent | c0fdb7a7f1e264d0983e765873cfa6e26decc6c8 (diff) |
Added pj_strstr() and pj_stristr() in pjlib
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1757 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib')
-rw-r--r-- | pjlib/include/pj/string.h | 25 | ||||
-rw-r--r-- | pjlib/src/pj/string.c | 38 |
2 files changed, 63 insertions, 0 deletions
diff --git a/pjlib/include/pj/string.h b/pjlib/include/pj/string.h index 8124539f..945af2da 100644 --- a/pjlib/include/pj/string.h +++ b/pjlib/include/pj/string.h @@ -472,6 +472,31 @@ PJ_INLINE(char*) pj_strchr( const pj_str_t *str, int chr) } /** + * Find the occurence of a substring substr in string str. + * + * @param str The string to search. + * @param substr The string to search fo. + * + * @return the pointer to the position of substr in str, or NULL. Note + * that if str is not NULL terminated, the returned pointer + * is pointing to non-NULL terminated string. + */ +PJ_DECL(char*) pj_strstr(const pj_str_t *str, const pj_str_t *substr); + +/** + * Performs substring lookup like pj_strstr() but ignores the case of + * both strings. + * + * @param str The string to search. + * @param substr The string to search fo. + * + * @return the pointer to the position of substr in str, or NULL. Note + * that if str is not NULL terminated, the returned pointer + * is pointing to non-NULL terminated string. + */ +PJ_DECL(char*) pj_stristr(const pj_str_t *str, const pj_str_t *substr); + +/** * Remove (trim) leading whitespaces from the string. * * @param str The string. diff --git a/pjlib/src/pj/string.c b/pjlib/src/pj/string.c index 9a2ca0f2..fb520c7b 100644 --- a/pjlib/src/pj/string.c +++ b/pjlib/src/pj/string.c @@ -28,6 +28,44 @@ #endif +PJ_DEF(char*) pj_strstr(const pj_str_t *str, const pj_str_t *substr) +{ + const char *s, *ends; + + /* Special case when substr is zero */ + if (substr->slen == 0) { + return (char*)str->ptr; + } + + s = str->ptr; + ends = str->ptr + str->slen - substr->slen; + for (; s<=ends; ++s) { + if (pj_ansi_strncmp(s, substr->ptr, substr->slen)==0) + return (char*)s; + } + return NULL; +} + + +PJ_DEF(char*) pj_stristr(const pj_str_t *str, const pj_str_t *substr) +{ + const char *s, *ends; + + /* Special case when substr is zero */ + if (substr->slen == 0) { + return (char*)str->ptr; + } + + s = str->ptr; + ends = str->ptr + str->slen - substr->slen; + for (; s<=ends; ++s) { + if (pj_ansi_strnicmp(s, substr->ptr, substr->slen)==0) + return (char*)s; + } + return NULL; +} + + PJ_DEF(pj_str_t*) pj_strltrim( pj_str_t *str ) { register char *p = str->ptr; |