Changeset 1194
- Timestamp:
- Apr 15, 2007 9:58:48 AM (16 years ago)
- Location:
- pjproject/trunk/pjlib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/include/pj/string.h
r974 r1194 519 519 520 520 /** 521 * Convert strings to an unsigned long-integer value. 522 * This function stops reading the string input either when the number 523 * of characters has exceeded the length of the input or it has read 524 * the first character it cannot recognize as part of a number, that is 525 * a character greater than or equal to base. 526 * 527 * @param str The input string. 528 * @param endptr Optional pointer to receive the remainder/unparsed 529 * portion of the input. 530 * @param base Number base to use. 531 * 532 * @return the unsigned integer number. 533 */ 534 PJ_DECL(unsigned long) pj_strtoul2(const pj_str_t *str, pj_str_t *endptr, 535 unsigned base); 536 537 /** 521 538 * Utility to convert unsigned integer to string. Note that the 522 539 * string will be NULL terminated. -
pjproject/trunk/pjlib/src/pj/string.c
r1125 r1194 18 18 */ 19 19 #include <pj/string.h> 20 #include <pj/assert.h> 20 21 #include <pj/pool.h> 21 22 #include <pj/ctype.h> … … 84 85 } 85 86 87 PJ_DEF(unsigned long) pj_strtoul2(const pj_str_t *str, pj_str_t *endptr, 88 unsigned base) 89 { 90 unsigned long value; 91 unsigned i; 92 93 PJ_CHECK_STACK(); 94 95 value = 0; 96 if (base <= 10) { 97 for (i=0; i<(unsigned)str->slen; ++i) { 98 unsigned c = (str->ptr[i] - '0'); 99 if (c >= base) 100 break; 101 value = value * base + c; 102 } 103 } else if (base == 16) { 104 for (i=0; i<(unsigned)str->slen; ++i) { 105 if (!pj_isxdigit(str->ptr[i])) 106 break; 107 value = value * 16 + pj_hex_digit_to_val(str->ptr[i]); 108 } 109 } else { 110 pj_assert(!"Unsupported base"); 111 i = 0; 112 value = 0xFFFFFFFFUL; 113 } 114 115 if (endptr) { 116 endptr->ptr = str->ptr + i; 117 endptr->slen = str->slen - i; 118 } 119 120 return value; 121 } 122 86 123 PJ_DEF(int) pj_utoa(unsigned long val, char *buf) 87 124 { -
pjproject/trunk/pjlib/src/pjlib-test/string.c
r974 r1194 49 49 * - pj_utoa() 50 50 * - pj_strtoul() 51 * - pj_strtoul2() 51 52 * - pj_create_random_string() 52 53 * - ... and mode.. … … 359 360 return -280; 360 361 362 /* 363 * pj_strtoul2() 364 */ 365 s5 = pj_str("123456"); 366 367 pj_strtoul2(&s5, NULL, 10); /* Crash test */ 368 369 if (pj_strtoul2(&s5, &s4, 10) != 123456UL) 370 return -290; 371 if (s4.slen != 0) 372 return -291; 373 if (pj_strtoul2(&s5, &s4, 16) != 0x123456UL) 374 return -292; 375 376 s5 = pj_str("0123ABCD"); 377 if (pj_strtoul2(&s5, &s4, 10) != 123) 378 return -293; 379 if (s4.slen != 4) 380 return -294; 381 if (s4.ptr == NULL || *s4.ptr != 'A') 382 return -295; 383 if (pj_strtoul2(&s5, &s4, 16) != 0x123ABCDUL) 384 return -296; 385 if (s4.slen != 0) 386 return -297; 387 361 388 /* 362 389 * pj_create_random_string()
Note: See TracChangeset
for help on using the changeset viewer.