summaryrefslogtreecommitdiff
path: root/pjlib/src
diff options
context:
space:
mode:
Diffstat (limited to 'pjlib/src')
-rw-r--r--pjlib/src/pj/hash.c3
-rw-r--r--pjlib/src/pj/string.c37
-rw-r--r--pjlib/src/pjlib-test/string.c27
3 files changed, 67 insertions, 0 deletions
diff --git a/pjlib/src/pj/hash.c b/pjlib/src/pj/hash.c
index dc7da673..5a97e5f1 100644
--- a/pjlib/src/pj/hash.c
+++ b/pjlib/src/pj/hash.c
@@ -133,6 +133,9 @@ static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht,
if (hval && *hval != 0) {
hash = *hval;
+ if (keylen==PJ_HASH_KEY_STRING) {
+ keylen = pj_ansi_strlen((const char*)key);
+ }
} else {
/* This slightly differs with pj_hash_calc() because we need
* to get the keylen when keylen is PJ_HASH_KEY_STRING.
diff --git a/pjlib/src/pj/string.c b/pjlib/src/pj/string.c
index 62da6a0f..9a2ca0f2 100644
--- a/pjlib/src/pj/string.c
+++ b/pjlib/src/pj/string.c
@@ -17,6 +17,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <pj/string.h>
+#include <pj/assert.h>
#include <pj/pool.h>
#include <pj/ctype.h>
#include <pj/rand.h>
@@ -83,6 +84,42 @@ PJ_DEF(unsigned long) pj_strtoul(const pj_str_t *str)
return value;
}
+PJ_DEF(unsigned long) pj_strtoul2(const pj_str_t *str, pj_str_t *endptr,
+ unsigned base)
+{
+ unsigned long value;
+ unsigned i;
+
+ PJ_CHECK_STACK();
+
+ value = 0;
+ if (base <= 10) {
+ for (i=0; i<(unsigned)str->slen; ++i) {
+ unsigned c = (str->ptr[i] - '0');
+ if (c >= base)
+ break;
+ value = value * base + c;
+ }
+ } else if (base == 16) {
+ for (i=0; i<(unsigned)str->slen; ++i) {
+ if (!pj_isxdigit(str->ptr[i]))
+ break;
+ value = value * 16 + pj_hex_digit_to_val(str->ptr[i]);
+ }
+ } else {
+ pj_assert(!"Unsupported base");
+ i = 0;
+ value = 0xFFFFFFFFUL;
+ }
+
+ if (endptr) {
+ endptr->ptr = str->ptr + i;
+ endptr->slen = str->slen - i;
+ }
+
+ return value;
+}
+
PJ_DEF(int) pj_utoa(unsigned long val, char *buf)
{
return pj_utoa_pad(val, buf, 0, 0);
diff --git a/pjlib/src/pjlib-test/string.c b/pjlib/src/pjlib-test/string.c
index c8bee0bc..dd3649cc 100644
--- a/pjlib/src/pjlib-test/string.c
+++ b/pjlib/src/pjlib-test/string.c
@@ -48,6 +48,7 @@
* - pj_strtrim()
* - pj_utoa()
* - pj_strtoul()
+ * - pj_strtoul2()
* - pj_create_random_string()
* - ... and mode..
*
@@ -358,6 +359,32 @@ int string_test(void)
if (pj_strtoul(&s5) != UL_VALUE)
return -280;
+ /*
+ * pj_strtoul2()
+ */
+ s5 = pj_str("123456");
+
+ pj_strtoul2(&s5, NULL, 10); /* Crash test */
+
+ if (pj_strtoul2(&s5, &s4, 10) != 123456UL)
+ return -290;
+ if (s4.slen != 0)
+ return -291;
+ if (pj_strtoul2(&s5, &s4, 16) != 0x123456UL)
+ return -292;
+
+ s5 = pj_str("0123ABCD");
+ if (pj_strtoul2(&s5, &s4, 10) != 123)
+ return -293;
+ if (s4.slen != 4)
+ return -294;
+ if (s4.ptr == NULL || *s4.ptr != 'A')
+ return -295;
+ if (pj_strtoul2(&s5, &s4, 16) != 0x123ABCDUL)
+ return -296;
+ if (s4.slen != 0)
+ return -297;
+
/*
* pj_create_random_string()
* Check that no duplicate strings are returned.