Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJ_STRING_H__ |
| 20 | #define __PJ_STRING_H__ |
| 21 | |
| 22 | /** |
| 23 | * @file string.h |
| 24 | * @brief PJLIB String Operations. |
| 25 | */ |
| 26 | |
| 27 | #include <pj/types.h> |
| 28 | #include <pj/compat/string.h> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | PJ_BEGIN_DECL |
| 32 | |
| 33 | /** |
| 34 | * @defgroup PJ_PSTR String Operations |
| 35 | * @ingroup PJ_DS |
| 36 | * @{ |
| 37 | * This module provides string manipulation API. |
| 38 | * |
| 39 | * \section pj_pstr_not_null_sec PJLIB String is NOT Null Terminated! |
| 40 | * |
| 41 | * That is the first information that developers need to know. Instead |
| 42 | * of using normal C string, strings in PJLIB are represented as |
| 43 | * pj_str_t structure below: |
| 44 | * |
| 45 | * <pre> |
| 46 | * typedef struct pj_str_t |
| 47 | * { |
| 48 | * char *ptr; |
| 49 | * pj_size_t slen; |
| 50 | * } pj_str_t; |
| 51 | * </pre> |
| 52 | * |
| 53 | * There are some advantages of using this approach: |
| 54 | * - the string can point to arbitrary location in memory even |
| 55 | * if the string in that location is not null terminated. This is |
| 56 | * most usefull for text parsing, where the parsed text can just |
| 57 | * point to the original text in the input. If we use C string, |
| 58 | * then we will have to copy the text portion from the input |
| 59 | * to a string variable. |
| 60 | * - because the length of the string is known, string copy operation |
| 61 | * can be made more efficient. |
| 62 | * |
| 63 | * Most of APIs in PJLIB that expect or return string will represent |
| 64 | * the string as pj_str_t instead of normal C string. |
| 65 | * |
| 66 | * \section pj_pstr_examples_sec Examples |
| 67 | * |
| 68 | * For some examples, please see: |
| 69 | * - @ref page_pjlib_string_test |
| 70 | */ |
| 71 | |
| 72 | /** |
| 73 | * Create string initializer from a normal C string. |
| 74 | * |
| 75 | * @param str Null terminated string to be stored. |
| 76 | * |
| 77 | * @return pj_str_t. |
| 78 | */ |
| 79 | PJ_IDECL(pj_str_t) pj_str(char *str); |
| 80 | |
| 81 | /** |
| 82 | * Create constant string from normal C string. |
| 83 | * |
| 84 | * @param str The string to be initialized. |
| 85 | * @param s Null terminated string. |
| 86 | * |
| 87 | * @return pj_str_t. |
| 88 | */ |
| 89 | PJ_INLINE(const pj_str_t*) pj_cstr(pj_str_t *str, const char *s) |
| 90 | { |
| 91 | str->ptr = (char*)s; |
Benny Prijono | 385153d | 2006-05-24 10:25:34 +0000 | [diff] [blame] | 92 | str->slen = s ? (pj_ssize_t)strlen(s) : 0; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 93 | return str; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Set the pointer and length to the specified value. |
| 98 | * |
| 99 | * @param str the string. |
| 100 | * @param ptr pointer to set. |
| 101 | * @param length length to set. |
| 102 | * |
| 103 | * @return the string. |
| 104 | */ |
| 105 | PJ_INLINE(pj_str_t*) pj_strset( pj_str_t *str, char *ptr, pj_size_t length) |
| 106 | { |
| 107 | str->ptr = ptr; |
Benny Prijono | 385153d | 2006-05-24 10:25:34 +0000 | [diff] [blame] | 108 | str->slen = (pj_ssize_t)length; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 109 | return str; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Set the pointer and length of the string to the source string, which |
| 114 | * must be NULL terminated. |
| 115 | * |
| 116 | * @param str the string. |
| 117 | * @param src pointer to set. |
| 118 | * |
| 119 | * @return the string. |
| 120 | */ |
| 121 | PJ_INLINE(pj_str_t*) pj_strset2( pj_str_t *str, char *src) |
| 122 | { |
| 123 | str->ptr = src; |
Benny Prijono | 385153d | 2006-05-24 10:25:34 +0000 | [diff] [blame] | 124 | str->slen = src ? (pj_ssize_t)strlen(src) : 0; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 125 | return str; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Set the pointer and the length of the string. |
| 130 | * |
| 131 | * @param str The target string. |
| 132 | * @param begin The start of the string. |
| 133 | * @param end The end of the string. |
| 134 | * |
| 135 | * @return the target string. |
| 136 | */ |
| 137 | PJ_INLINE(pj_str_t*) pj_strset3( pj_str_t *str, char *begin, char *end ) |
| 138 | { |
| 139 | str->ptr = begin; |
Benny Prijono | 385153d | 2006-05-24 10:25:34 +0000 | [diff] [blame] | 140 | str->slen = (pj_ssize_t)(end-begin); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 141 | return str; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Assign string. |
| 146 | * |
| 147 | * @param dst The target string. |
| 148 | * @param src The source string. |
| 149 | * |
| 150 | * @return the target string. |
| 151 | */ |
| 152 | PJ_IDECL(pj_str_t*) pj_strassign( pj_str_t *dst, pj_str_t *src ); |
| 153 | |
| 154 | /** |
| 155 | * Copy string contents. |
| 156 | * |
| 157 | * @param dst The target string. |
| 158 | * @param src The source string. |
| 159 | * |
| 160 | * @return the target string. |
| 161 | */ |
| 162 | PJ_IDECL(pj_str_t*) pj_strcpy(pj_str_t *dst, const pj_str_t *src); |
| 163 | |
| 164 | /** |
| 165 | * Copy string contents. |
| 166 | * |
| 167 | * @param dst The target string. |
| 168 | * @param src The source string. |
| 169 | * |
| 170 | * @return the target string. |
| 171 | */ |
| 172 | PJ_IDECL(pj_str_t*) pj_strcpy2(pj_str_t *dst, const char *src); |
| 173 | |
| 174 | /** |
| 175 | * Copy source string to destination up to the specified max length. |
| 176 | * |
| 177 | * @param dst The target string. |
| 178 | * @param src The source string. |
| 179 | * @param max Maximum characters to copy. |
| 180 | * |
| 181 | * @return the target string. |
| 182 | */ |
| 183 | PJ_IDECL(pj_str_t*) pj_strncpy(pj_str_t *dst, const pj_str_t *src, |
| 184 | pj_ssize_t max); |
| 185 | |
| 186 | /** |
| 187 | * Copy source string to destination up to the specified max length, |
| 188 | * and NULL terminate the destination. If source string length is |
| 189 | * greater than or equal to max, then max-1 will be copied. |
| 190 | * |
| 191 | * @param dst The target string. |
| 192 | * @param src The source string. |
| 193 | * @param max Maximum characters to copy. |
| 194 | * |
| 195 | * @return the target string. |
| 196 | */ |
| 197 | PJ_IDECL(pj_str_t*) pj_strncpy_with_null(pj_str_t *dst, const pj_str_t *src, |
| 198 | pj_ssize_t max); |
| 199 | |
| 200 | /** |
| 201 | * Duplicate string. |
| 202 | * |
| 203 | * @param pool The pool. |
| 204 | * @param dst The string result. |
| 205 | * @param src The string to duplicate. |
| 206 | * |
| 207 | * @return the string result. |
| 208 | */ |
| 209 | PJ_IDECL(pj_str_t*) pj_strdup(pj_pool_t *pool, |
| 210 | pj_str_t *dst, |
| 211 | const pj_str_t *src); |
| 212 | |
| 213 | /** |
| 214 | * Duplicate string and NULL terminate the destination string. |
| 215 | * |
Benny Prijono | 942452b | 2006-03-02 21:12:28 +0000 | [diff] [blame] | 216 | * @param pool The pool. |
| 217 | * @param dst The string result. |
| 218 | * @param src The string to duplicate. |
| 219 | * |
| 220 | * @return The string result. |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 221 | */ |
| 222 | PJ_IDECL(pj_str_t*) pj_strdup_with_null(pj_pool_t *pool, |
| 223 | pj_str_t *dst, |
| 224 | const pj_str_t *src); |
| 225 | |
| 226 | /** |
| 227 | * Duplicate string. |
| 228 | * |
| 229 | * @param pool The pool. |
| 230 | * @param dst The string result. |
| 231 | * @param src The string to duplicate. |
| 232 | * |
| 233 | * @return the string result. |
| 234 | */ |
| 235 | PJ_IDECL(pj_str_t*) pj_strdup2(pj_pool_t *pool, |
| 236 | pj_str_t *dst, |
| 237 | const char *src); |
| 238 | |
| 239 | /** |
Benny Prijono | 942452b | 2006-03-02 21:12:28 +0000 | [diff] [blame] | 240 | * Duplicate string and NULL terminate the destination string. |
| 241 | * |
| 242 | * @param pool The pool. |
| 243 | * @param dst The string result. |
| 244 | * @param src The string to duplicate. |
| 245 | * |
| 246 | * @return The string result. |
| 247 | */ |
| 248 | PJ_IDECL(pj_str_t*) pj_strdup2_with_null(pj_pool_t *pool, |
| 249 | pj_str_t *dst, |
| 250 | const char *src); |
| 251 | |
| 252 | |
| 253 | /** |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 254 | * Duplicate string. |
| 255 | * |
| 256 | * @param pool The pool. |
| 257 | * @param src The string to duplicate. |
| 258 | * |
| 259 | * @return the string result. |
| 260 | */ |
| 261 | PJ_IDECL(pj_str_t) pj_strdup3(pj_pool_t *pool, const char *src); |
| 262 | |
| 263 | /** |
| 264 | * Return the length of the string. |
| 265 | * |
| 266 | * @param str The string. |
| 267 | * |
| 268 | * @return the length of the string. |
| 269 | */ |
| 270 | PJ_INLINE(pj_size_t) pj_strlen( const pj_str_t *str ) |
| 271 | { |
| 272 | return str->slen; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Return the pointer to the string data. |
| 277 | * |
| 278 | * @param str The string. |
| 279 | * |
| 280 | * @return the pointer to the string buffer. |
| 281 | */ |
| 282 | PJ_INLINE(const char*) pj_strbuf( const pj_str_t *str ) |
| 283 | { |
| 284 | return str->ptr; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Compare strings. |
| 289 | * |
| 290 | * @param str1 The string to compare. |
| 291 | * @param str2 The string to compare. |
| 292 | * |
| 293 | * @return |
| 294 | * - < 0 if str1 is less than str2 |
| 295 | * - 0 if str1 is identical to str2 |
| 296 | * - > 0 if str1 is greater than str2 |
| 297 | */ |
| 298 | PJ_IDECL(int) pj_strcmp( const pj_str_t *str1, const pj_str_t *str2); |
| 299 | |
| 300 | /** |
| 301 | * Compare strings. |
| 302 | * |
| 303 | * @param str1 The string to compare. |
| 304 | * @param str2 The string to compare. |
| 305 | * |
| 306 | * @return |
| 307 | * - < 0 if str1 is less than str2 |
| 308 | * - 0 if str1 is identical to str2 |
| 309 | * - > 0 if str1 is greater than str2 |
| 310 | */ |
| 311 | PJ_IDECL(int) pj_strcmp2( const pj_str_t *str1, const char *str2 ); |
| 312 | |
| 313 | /** |
| 314 | * Compare strings. |
| 315 | * |
| 316 | * @param str1 The string to compare. |
| 317 | * @param str2 The string to compare. |
| 318 | * @param len The maximum number of characters to compare. |
| 319 | * |
| 320 | * @return |
| 321 | * - < 0 if str1 is less than str2 |
| 322 | * - 0 if str1 is identical to str2 |
| 323 | * - > 0 if str1 is greater than str2 |
| 324 | */ |
| 325 | PJ_IDECL(int) pj_strncmp( const pj_str_t *str1, const pj_str_t *str2, |
| 326 | pj_size_t len); |
| 327 | |
| 328 | /** |
| 329 | * Compare strings. |
| 330 | * |
| 331 | * @param str1 The string to compare. |
| 332 | * @param str2 The string to compare. |
| 333 | * @param len The maximum number of characters to compare. |
| 334 | * |
| 335 | * @return |
| 336 | * - < 0 if str1 is less than str2 |
| 337 | * - 0 if str1 is identical to str2 |
| 338 | * - > 0 if str1 is greater than str2 |
| 339 | */ |
| 340 | PJ_IDECL(int) pj_strncmp2( const pj_str_t *str1, const char *str2, |
| 341 | pj_size_t len); |
| 342 | |
| 343 | /** |
| 344 | * Perform lowercase comparison to the strings. |
| 345 | * |
| 346 | * @param str1 The string to compare. |
| 347 | * @param str2 The string to compare. |
| 348 | * |
| 349 | * @return |
| 350 | * - < 0 if str1 is less than str2 |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 351 | * - 0 if str1 is equal to str2 |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 352 | * - > 0 if str1 is greater than str2 |
| 353 | */ |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 354 | PJ_IDECL(int) pj_stricmp(const pj_str_t *str1, const pj_str_t *str2); |
| 355 | |
| 356 | /** |
| 357 | * Perform lowercase comparison to the strings which consists of only |
| 358 | * alnum characters. More over, it will only return non-zero if both |
Benny Prijono | cec5f4a | 2005-11-22 23:51:50 +0000 | [diff] [blame] | 359 | * strings are not equal, not the usual negative or positive value. |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 360 | * |
| 361 | * If non-alnum inputs are given, then the function may mistakenly |
Benny Prijono | cec5f4a | 2005-11-22 23:51:50 +0000 | [diff] [blame] | 362 | * treat two strings as equal. |
| 363 | * |
| 364 | * @param str1 The string to compare. |
| 365 | * @param str2 The string to compare. |
| 366 | * @param len The length to compare. |
| 367 | * |
| 368 | * @return |
| 369 | * - 0 if str1 is equal to str2 |
| 370 | * - (-1) if not equal. |
| 371 | */ |
Benny Prijono | dcf2996 | 2006-03-23 18:03:40 +0000 | [diff] [blame] | 372 | #if defined(PJ_HAS_STRICMP_ALNUM) && PJ_HAS_STRICMP_ALNUM!=0 |
Benny Prijono | cec5f4a | 2005-11-22 23:51:50 +0000 | [diff] [blame] | 373 | PJ_IDECL(int) strnicmp_alnum(const char *str1, const char *str2, |
| 374 | int len); |
Benny Prijono | dcf2996 | 2006-03-23 18:03:40 +0000 | [diff] [blame] | 375 | #else |
Benny Prijono | c4c8f24 | 2006-08-01 23:01:55 +0000 | [diff] [blame] | 376 | #define strnicmp_alnum pj_ansi_strnicmp |
Benny Prijono | dcf2996 | 2006-03-23 18:03:40 +0000 | [diff] [blame] | 377 | #endif |
Benny Prijono | cec5f4a | 2005-11-22 23:51:50 +0000 | [diff] [blame] | 378 | |
| 379 | /** |
| 380 | * Perform lowercase comparison to the strings which consists of only |
| 381 | * alnum characters. More over, it will only return non-zero if both |
| 382 | * strings are not equal, not the usual negative or positive value. |
| 383 | * |
| 384 | * If non-alnum inputs are given, then the function may mistakenly |
| 385 | * treat two strings as equal. |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 386 | * |
| 387 | * @param str1 The string to compare. |
| 388 | * @param str2 The string to compare. |
| 389 | * |
| 390 | * @return |
| 391 | * - 0 if str1 is equal to str2 |
| 392 | * - (-1) if not equal. |
| 393 | */ |
Benny Prijono | dcf2996 | 2006-03-23 18:03:40 +0000 | [diff] [blame] | 394 | #if defined(PJ_HAS_STRICMP_ALNUM) && PJ_HAS_STRICMP_ALNUM!=0 |
Benny Prijono | a7944bb | 2005-11-21 17:01:50 +0000 | [diff] [blame] | 395 | PJ_IDECL(int) pj_stricmp_alnum(const pj_str_t *str1, const pj_str_t *str2); |
Benny Prijono | dcf2996 | 2006-03-23 18:03:40 +0000 | [diff] [blame] | 396 | #else |
| 397 | #define pj_stricmp_alnum pj_stricmp |
| 398 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 399 | |
| 400 | /** |
| 401 | * Perform lowercase comparison to the strings. |
| 402 | * |
| 403 | * @param str1 The string to compare. |
| 404 | * @param str2 The string to compare. |
| 405 | * |
| 406 | * @return |
| 407 | * - < 0 if str1 is less than str2 |
| 408 | * - 0 if str1 is identical to str2 |
| 409 | * - > 0 if str1 is greater than str2 |
| 410 | */ |
| 411 | PJ_IDECL(int) pj_stricmp2( const pj_str_t *str1, const char *str2); |
| 412 | |
| 413 | /** |
| 414 | * Perform lowercase comparison to the strings. |
| 415 | * |
| 416 | * @param str1 The string to compare. |
| 417 | * @param str2 The string to compare. |
| 418 | * @param len The maximum number of characters to compare. |
| 419 | * |
| 420 | * @return |
| 421 | * - < 0 if str1 is less than str2 |
| 422 | * - 0 if str1 is identical to str2 |
| 423 | * - > 0 if str1 is greater than str2 |
| 424 | */ |
| 425 | PJ_IDECL(int) pj_strnicmp( const pj_str_t *str1, const pj_str_t *str2, |
| 426 | pj_size_t len); |
| 427 | |
| 428 | /** |
| 429 | * Perform lowercase comparison to the strings. |
| 430 | * |
| 431 | * @param str1 The string to compare. |
| 432 | * @param str2 The string to compare. |
| 433 | * @param len The maximum number of characters to compare. |
| 434 | * |
| 435 | * @return |
| 436 | * - < 0 if str1 is less than str2 |
| 437 | * - 0 if str1 is identical to str2 |
| 438 | * - > 0 if str1 is greater than str2 |
| 439 | */ |
| 440 | PJ_IDECL(int) pj_strnicmp2( const pj_str_t *str1, const char *str2, |
| 441 | pj_size_t len); |
| 442 | |
| 443 | /** |
| 444 | * Concatenate strings. |
| 445 | * |
| 446 | * @param dst The destination string. |
| 447 | * @param src The source string. |
| 448 | */ |
| 449 | PJ_IDECL(void) pj_strcat(pj_str_t *dst, const pj_str_t *src); |
| 450 | |
Benny Prijono | 5a9091c | 2006-02-14 20:59:53 +0000 | [diff] [blame] | 451 | |
| 452 | /** |
| 453 | * Concatenate strings. |
| 454 | * |
| 455 | * @param dst The destination string. |
| 456 | * @param src The source string. |
| 457 | */ |
Benny Prijono | 294c253 | 2006-06-16 16:52:51 +0000 | [diff] [blame] | 458 | PJ_IDECL(void) pj_strcat2(pj_str_t *dst, const char *src); |
Benny Prijono | 5a9091c | 2006-02-14 20:59:53 +0000 | [diff] [blame] | 459 | |
| 460 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 461 | /** |
| 462 | * Finds a character in a string. |
| 463 | * |
| 464 | * @param str The string. |
| 465 | * @param chr The character to find. |
| 466 | * |
| 467 | * @return the pointer to first character found, or NULL. |
| 468 | */ |
| 469 | PJ_INLINE(char*) pj_strchr( const pj_str_t *str, int chr) |
| 470 | { |
Benny Prijono | 33cb24a | 2006-11-01 07:13:45 +0000 | [diff] [blame] | 471 | return (char*) memchr((char*)str->ptr, chr, str->slen); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Remove (trim) leading whitespaces from the string. |
| 476 | * |
| 477 | * @param str The string. |
| 478 | * |
| 479 | * @return the string. |
| 480 | */ |
| 481 | PJ_DECL(pj_str_t*) pj_strltrim( pj_str_t *str ); |
| 482 | |
| 483 | /** |
| 484 | * Remove (trim) the trailing whitespaces from the string. |
| 485 | * |
| 486 | * @param str The string. |
| 487 | * |
| 488 | * @return the string. |
| 489 | */ |
| 490 | PJ_DECL(pj_str_t*) pj_strrtrim( pj_str_t *str ); |
| 491 | |
| 492 | /** |
| 493 | * Remove (trim) leading and trailing whitespaces from the string. |
| 494 | * |
| 495 | * @param str The string. |
| 496 | * |
| 497 | * @return the string. |
| 498 | */ |
| 499 | PJ_IDECL(pj_str_t*) pj_strtrim( pj_str_t *str ); |
| 500 | |
| 501 | /** |
| 502 | * Initialize the buffer with some random string. |
| 503 | * |
| 504 | * @param str the string to store the result. |
| 505 | * @param length the length of the random string to generate. |
| 506 | * |
| 507 | * @return the string. |
| 508 | */ |
| 509 | PJ_DECL(char*) pj_create_random_string(char *str, pj_size_t length); |
| 510 | |
| 511 | /** |
| 512 | * Convert string to unsigned integer. |
| 513 | * |
| 514 | * @param str the string. |
| 515 | * |
| 516 | * @return the unsigned integer. |
| 517 | */ |
| 518 | PJ_DECL(unsigned long) pj_strtoul(const pj_str_t *str); |
| 519 | |
| 520 | /** |
Benny Prijono | c7434f6 | 2007-04-15 09:58:48 +0000 | [diff] [blame] | 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 | /** |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 538 | * Utility to convert unsigned integer to string. Note that the |
| 539 | * string will be NULL terminated. |
| 540 | * |
| 541 | * @param val the unsigned integer value. |
| 542 | * @param buf the buffer |
| 543 | * |
| 544 | * @return the number of characters written |
| 545 | */ |
| 546 | PJ_DECL(int) pj_utoa(unsigned long val, char *buf); |
| 547 | |
| 548 | /** |
| 549 | * Convert unsigned integer to string with minimum digits. Note that the |
| 550 | * string will be NULL terminated. |
| 551 | * |
| 552 | * @param val The unsigned integer value. |
| 553 | * @param buf The buffer. |
| 554 | * @param min_dig Minimum digits to be printed, or zero to specify no |
| 555 | * minimum digit. |
| 556 | * @param pad The padding character to be put in front of the string |
| 557 | * when the digits is less than minimum. |
| 558 | * |
| 559 | * @return the number of characters written. |
| 560 | */ |
| 561 | PJ_DECL(int) pj_utoa_pad( unsigned long val, char *buf, int min_dig, int pad); |
| 562 | |
Benny Prijono | 1479b65 | 2006-07-03 14:18:17 +0000 | [diff] [blame] | 563 | |
| 564 | /** |
| 565 | * Fill the memory location with zero. |
| 566 | * |
| 567 | * @param dst The destination buffer. |
| 568 | * @param size The number of bytes. |
| 569 | */ |
| 570 | PJ_INLINE(void) pj_bzero(void *dst, pj_size_t size) |
| 571 | { |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 572 | #if defined(PJ_HAS_BZERO) && PJ_HAS_BZERO!=0 |
Benny Prijono | 1479b65 | 2006-07-03 14:18:17 +0000 | [diff] [blame] | 573 | bzero(dst, size); |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 574 | #else |
| 575 | memset(dst, 0, size); |
| 576 | #endif |
Benny Prijono | 1479b65 | 2006-07-03 14:18:17 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 580 | /** |
| 581 | * Fill the memory location with value. |
| 582 | * |
| 583 | * @param dst The destination buffer. |
| 584 | * @param c Character to set. |
| 585 | * @param size The number of characters. |
| 586 | * |
| 587 | * @return the value of dst. |
| 588 | */ |
| 589 | PJ_INLINE(void*) pj_memset(void *dst, int c, pj_size_t size) |
| 590 | { |
| 591 | return memset(dst, c, size); |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Copy buffer. |
| 596 | * |
| 597 | * @param dst The destination buffer. |
| 598 | * @param src The source buffer. |
| 599 | * @param size The size to copy. |
| 600 | * |
| 601 | * @return the destination buffer. |
| 602 | */ |
| 603 | PJ_INLINE(void*) pj_memcpy(void *dst, const void *src, pj_size_t size) |
| 604 | { |
| 605 | return memcpy(dst, src, size); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Move memory. |
| 610 | * |
| 611 | * @param dst The destination buffer. |
| 612 | * @param src The source buffer. |
| 613 | * @param size The size to copy. |
| 614 | * |
| 615 | * @return the destination buffer. |
| 616 | */ |
| 617 | PJ_INLINE(void*) pj_memmove(void *dst, const void *src, pj_size_t size) |
| 618 | { |
| 619 | return memmove(dst, src, size); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Compare buffers. |
| 624 | * |
| 625 | * @param buf1 The first buffer. |
| 626 | * @param buf2 The second buffer. |
| 627 | * @param size The size to compare. |
| 628 | * |
| 629 | * @return negative, zero, or positive value. |
| 630 | */ |
| 631 | PJ_INLINE(int) pj_memcmp(const void *buf1, const void *buf2, pj_size_t size) |
| 632 | { |
| 633 | return memcmp(buf1, buf2, size); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Find character in the buffer. |
| 638 | * |
| 639 | * @param buf The buffer. |
| 640 | * @param c The character to find. |
| 641 | * @param size The size to check. |
| 642 | * |
| 643 | * @return the pointer to location where the character is found, or NULL if |
| 644 | * not found. |
| 645 | */ |
| 646 | PJ_INLINE(void*) pj_memchr(const void *buf, int c, pj_size_t size) |
| 647 | { |
Benny Prijono | 33cb24a | 2006-11-01 07:13:45 +0000 | [diff] [blame] | 648 | return (void*)memchr((void*)buf, c, size); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | |
| 652 | /** |
| 653 | * @} |
| 654 | */ |
| 655 | |
| 656 | #if PJ_FUNCTIONS_ARE_INLINED |
| 657 | # include <pj/string_i.h> |
| 658 | #endif |
| 659 | |
| 660 | PJ_END_DECL |
| 661 | |
| 662 | #endif /* __PJ_STRING_H__ */ |
| 663 | |