diff options
Diffstat (limited to 'pjlib')
-rw-r--r-- | pjlib/include/pj/string.h | 11 | ||||
-rw-r--r-- | pjlib/src/pj/string.c | 13 |
2 files changed, 24 insertions, 0 deletions
diff --git a/pjlib/include/pj/string.h b/pjlib/include/pj/string.h index 6df22f12..525b4c05 100644 --- a/pjlib/include/pj/string.h +++ b/pjlib/include/pj/string.h @@ -536,6 +536,17 @@ PJ_IDECL(pj_str_t*) pj_strtrim( pj_str_t *str ); PJ_DECL(char*) pj_create_random_string(char *str, pj_size_t length); /** + * Convert string to signed integer. The conversion will stop as + * soon as non-digit character is found or all the characters have + * been processed. + * + * @param str the string. + * + * @return the integer. + */ +PJ_DECL(long) pj_strtol(const pj_str_t *str); + +/** * Convert string to unsigned integer. The conversion will stop as * soon as non-digit character is found or all the characters have * been processed. diff --git a/pjlib/src/pj/string.c b/pjlib/src/pj/string.c index 52e7c794..ca3a4605 100644 --- a/pjlib/src/pj/string.c +++ b/pjlib/src/pj/string.c @@ -109,6 +109,19 @@ PJ_DEF(char*) pj_create_random_string(char *str, pj_size_t len) return str; } +PJ_DEF(long) pj_strtol(const pj_str_t *str) +{ + PJ_CHECK_STACK(); + + if (str->slen > 0 && (str->ptr[0] == '+' || str->ptr[0] == '-')) { + pj_str_t s; + + s.ptr = str->ptr + 1; + s.slen = str->slen - 1; + return (str->ptr[0] == '-'? -(long)pj_strtoul(&s) : pj_strtoul(&s)); + } else + return pj_strtoul(str); +} PJ_DEF(unsigned long) pj_strtoul(const pj_str_t *str) { |