| 1 | /* |
| 2 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 3 | * Copyright (C) 2003-2008 Benny Prijono <[email protected]> |
| 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> |
| 29 | |
| 30 | PJ_BEGIN_DECL |
| 31 | |
| 32 | /** |
| 33 | * @defgroup PJ_PSTR String Operations |
| 34 | * @ingroup PJ_DS |
| 35 | * @{ |
| 36 | * This module provides string manipulation API. |
| 37 | * |
| 38 | * \section pj_pstr_not_null_sec PJLIB String is NOT Null Terminated! |
| 39 | * |
| 40 | * That is the first information that developers need to know. Instead |
| 41 | * of using normal C string, strings in PJLIB are represented as |
| 42 | * pj_str_t structure below: |
| 43 | * |
| 44 | * <pre> |
| 45 | * typedef struct pj_str_t |
| 46 | * { |
| 47 | * char *ptr; |
| 48 | * pj_ssize_t slen; |
| 49 | * } pj_str_t; |
| 50 | * </pre> |
| 51 | * |
| 52 | * There are some advantages of using this approach: |
| 53 | * - the string can point to arbitrary location in memory even |
| 54 | * if the string in that location is not null terminated. This is |
| 55 | * most usefull for text parsing, where the parsed text can just |
| 56 | * point to the original text in the input. If we use C string, |
| 57 | * then we will have to copy the text portion from the input |
| 58 | * to a string variable. |
| 59 | * - because the length of the string is known, string copy operation |
| 60 | * can be made more efficient. |
| 61 | * |
| 62 | * Most of APIs in PJLIB that expect or return string will represent |
| 63 | * the string as pj_str_t instead of normal C string. |
| 64 | * |
| 65 | * \section pj_pstr_examples_sec Examples |
| 66 | * |
| 67 | * For some examples, please see: |
| 68 | * - String test: \src{pjlib/src/pjlib-test/string.c} |
| 69 | */ |
| 70 | |
| 71 | /** |
| 72 | * Check if a string is truncated and if yes, put a suffix of ".." |
| 73 | * to indicate the truncation. |
| 74 | * This macro is used to check the result of pj_ansi_snprintf(). |
| 75 | * |
| 76 | * @param ret The return value of pj_ansi_snprintf(). |
| 77 | * @param str The string. |
| 78 | * @param len The length of the string buffer. |
| 79 | */ |
| 80 | #define PJ_CHECK_TRUNC_STR(ret, str, len) \ |
| 81 | if ((int)(ret) >= (int)(len) || (ret) < 0) pj_ansi_strxcpy((str) + (len) - 3, "..", 3) |
| 82 | |
| 83 | /** |
| 84 | * Create string initializer from a normal C string. |
| 85 | * |
| 86 | * @param str Null terminated string to be stored. |
| 87 | * |
| 88 | * @return pj_str_t. |
| 89 | */ |
| 90 | PJ_IDECL(pj_str_t) pj_str(char *str); |
| 91 | |
| 92 | /** |
| 93 | * Create constant string from normal C string. |
| 94 | * |
| 95 | * @param str The string to be initialized. |
| 96 | * @param s Null terminated string. |
| 97 | * |
| 98 | * @return pj_str_t. |
| 99 | */ |
| 100 | PJ_INLINE(const pj_str_t*) pj_cstr(pj_str_t *str, const char *s) |
| 101 | { |
| 102 | str->ptr = (char*)s; |
| 103 | str->slen = s ? (pj_ssize_t)strlen(s) : 0; |
| 104 | return str; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Set the pointer and length to the specified value. |
| 109 | * |
| 110 | * @param str the string. |
| 111 | * @param ptr pointer to set. |
| 112 | * @param length length to set. |
| 113 | * |
| 114 | * @return the string. |
| 115 | */ |
| 116 | PJ_INLINE(pj_str_t*) pj_strset( pj_str_t *str, char *ptr, pj_size_t length) |
| 117 | { |
| 118 | str->ptr = ptr; |
| 119 | str->slen = (pj_ssize_t)length; |
| 120 | return str; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Set the pointer and length of the string to the source string, which |
| 125 | * must be NULL terminated. |
| 126 | * |
| 127 | * @param str the string. |
| 128 | * @param src pointer to set. |
| 129 | * |
| 130 | * @return the string. |
| 131 | */ |
| 132 | PJ_INLINE(pj_str_t*) pj_strset2( pj_str_t *str, char *src) |
| 133 | { |
| 134 | str->ptr = src; |
| 135 | str->slen = src ? (pj_ssize_t)strlen(src) : 0; |
| 136 | return str; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Set the pointer and the length of the string. |
| 141 | * |
| 142 | * @param str The target string. |
| 143 | * @param begin The start of the string. |
| 144 | * @param end The end of the string. |
| 145 | * |
| 146 | * @return the target string. |
| 147 | */ |
| 148 | PJ_INLINE(pj_str_t*) pj_strset3( pj_str_t *str, char *begin, char *end ) |
| 149 | { |
| 150 | str->ptr = begin; |
| 151 | str->slen = (pj_ssize_t)(end-begin); |
| 152 | return str; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Assign string. |
| 157 | * |
| 158 | * @param dst The target string. |
| 159 | * @param src The source string. |
| 160 | * |
| 161 | * @return the target string. |
| 162 | */ |
| 163 | PJ_IDECL(pj_str_t*) pj_strassign( pj_str_t *dst, pj_str_t *src ); |
| 164 | |
| 165 | /** |
| 166 | * Copy string contents. |
| 167 | * |
| 168 | * @param dst The target string. |
| 169 | * @param src The source string. |
| 170 | * |
| 171 | * @return the target string. |
| 172 | */ |
| 173 | PJ_IDECL(pj_str_t*) pj_strcpy(pj_str_t *dst, const pj_str_t *src); |
| 174 | |
| 175 | /** |
| 176 | * Copy string contents. |
| 177 | * |
| 178 | * @param dst The target string. |
| 179 | * @param src The source string. |
| 180 | * |
| 181 | * @return the target string. |
| 182 | */ |
| 183 | PJ_IDECL(pj_str_t*) pj_strcpy2(pj_str_t *dst, const char *src); |
| 184 | |
| 185 | /** |
| 186 | * Copy source string to destination up to the specified max length. |
| 187 | * |
| 188 | * @param dst The target string. |
| 189 | * @param src The source string. |
| 190 | * @param max Maximum characters to copy. |
| 191 | * |
| 192 | * @return the target string. |
| 193 | */ |
| 194 | PJ_IDECL(pj_str_t*) pj_strncpy(pj_str_t *dst, const pj_str_t *src, |
| 195 | pj_ssize_t max); |
| 196 | |
| 197 | /** |
| 198 | * Copy source string to destination up to the specified max length, |
| 199 | * and NULL terminate the destination. If source string length is |
| 200 | * greater than or equal to max, then max-1 will be copied. |
| 201 | * |
| 202 | * @param dst The target string. |
| 203 | * @param src The source string. |
| 204 | * @param max Maximum characters to copy. |
| 205 | * |
| 206 | * @return the target string. |
| 207 | */ |
| 208 | PJ_IDECL(pj_str_t*) pj_strncpy_with_null(pj_str_t *dst, const pj_str_t *src, |
| 209 | pj_ssize_t max); |
| 210 | |
| 211 | /** |
| 212 | * Duplicate string. |
| 213 | * |
| 214 | * @param pool The pool. |
| 215 | * @param dst The string result. |
| 216 | * @param src The string to duplicate. |
| 217 | * |
| 218 | * @return the string result. |
| 219 | */ |
| 220 | PJ_IDECL(pj_str_t*) pj_strdup(pj_pool_t *pool, |
| 221 | pj_str_t *dst, |
| 222 | const pj_str_t *src); |
| 223 | |
| 224 | /** |
| 225 | * Duplicate string and NULL terminate the destination string. |
| 226 | * |
| 227 | * @param pool The pool. |
| 228 | * @param dst The string result. |
| 229 | * @param src The string to duplicate. |
| 230 | * |
| 231 | * @return The string result. |
| 232 | */ |
| 233 | PJ_IDECL(pj_str_t*) pj_strdup_with_null(pj_pool_t *pool, |
| 234 | pj_str_t *dst, |
| 235 | const pj_str_t *src); |
| 236 | |
| 237 | /** |
| 238 | * Duplicate string. |
| 239 | * |
| 240 | * @param pool The pool. |
| 241 | * @param dst The string result. |
| 242 | * @param src The string to duplicate. |
| 243 | * |
| 244 | * @return the string result. |
| 245 | */ |
| 246 | PJ_IDECL(pj_str_t*) pj_strdup2(pj_pool_t *pool, |
| 247 | pj_str_t *dst, |
| 248 | const char *src); |
| 249 | |
| 250 | /** |
| 251 | * Duplicate string and NULL terminate the destination string. |
| 252 | * |
| 253 | * @param pool The pool. |
| 254 | * @param dst The string result. |
| 255 | * @param src The string to duplicate. |
| 256 | * |
| 257 | * @return The string result. |
| 258 | */ |
| 259 | PJ_IDECL(pj_str_t*) pj_strdup2_with_null(pj_pool_t *pool, |
| 260 | pj_str_t *dst, |
| 261 | const char *src); |
| 262 | |
| 263 | |
| 264 | /** |
| 265 | * Duplicate string. |
| 266 | * |
| 267 | * @param pool The pool. |
| 268 | * @param src The string to duplicate. |
| 269 | * |
| 270 | * @return the string result. |
| 271 | */ |
| 272 | PJ_IDECL(pj_str_t) pj_strdup3(pj_pool_t *pool, const char *src); |
| 273 | |
| 274 | /** |
| 275 | * Return the length of the string. |
| 276 | * |
| 277 | * @param str The string. |
| 278 | * |
| 279 | * @return the length of the string. |
| 280 | */ |
| 281 | PJ_INLINE(pj_size_t) pj_strlen( const pj_str_t *str ) |
| 282 | { |
| 283 | return str->slen; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Return the pointer to the string data. |
| 288 | * |
| 289 | * @param str The string. |
| 290 | * |
| 291 | * @return the pointer to the string buffer. |
| 292 | */ |
| 293 | PJ_INLINE(const char*) pj_strbuf( const pj_str_t *str ) |
| 294 | { |
| 295 | return str->ptr; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Compare strings. |
| 300 | * |
| 301 | * @param str1 The string to compare. |
| 302 | * @param str2 The string to compare. |
| 303 | * |
| 304 | * @return |
| 305 | * - < 0 if str1 is less than str2 |
| 306 | * - 0 if str1 is identical to str2 |
| 307 | * - > 0 if str1 is greater than str2 |
| 308 | */ |
| 309 | PJ_IDECL(int) pj_strcmp( const pj_str_t *str1, const pj_str_t *str2); |
| 310 | |
| 311 | /** |
| 312 | * Compare strings. |
| 313 | * |
| 314 | * @param str1 The string to compare. |
| 315 | * @param str2 The string to compare. |
| 316 | * |
| 317 | * @return |
| 318 | * - < 0 if str1 is less than str2 |
| 319 | * - 0 if str1 is identical to str2 |
| 320 | * - > 0 if str1 is greater than str2 |
| 321 | */ |
| 322 | PJ_IDECL(int) pj_strcmp2( const pj_str_t *str1, const char *str2 ); |
| 323 | |
| 324 | /** |
| 325 | * Compare strings. |
| 326 | * |
| 327 | * @param str1 The string to compare. |
| 328 | * @param str2 The string to compare. |
| 329 | * @param len The maximum number of characters to compare. |
| 330 | * |
| 331 | * @return |
| 332 | * - < 0 if str1 is less than str2 |
| 333 | * - 0 if str1 is identical to str2 |
| 334 | * - > 0 if str1 is greater than str2 |
| 335 | */ |
| 336 | PJ_IDECL(int) pj_strncmp( const pj_str_t *str1, const pj_str_t *str2, |
| 337 | pj_size_t len); |
| 338 | |
| 339 | /** |
| 340 | * Compare strings. |
| 341 | * |
| 342 | * @param str1 The string to compare. |
| 343 | * @param str2 The string to compare. |
| 344 | * @param len The maximum number of characters to compare. |
| 345 | * |
| 346 | * @return |
| 347 | * - < 0 if str1 is less than str2 |
| 348 | * - 0 if str1 is identical to str2 |
| 349 | * - > 0 if str1 is greater than str2 |
| 350 | */ |
| 351 | PJ_IDECL(int) pj_strncmp2( const pj_str_t *str1, const char *str2, |
| 352 | pj_size_t len); |
| 353 | |
| 354 | /** |
| 355 | * Perform case-insensitive comparison to the strings. |
| 356 | * |
| 357 | * @param str1 The string to compare. |
| 358 | * @param str2 The string to compare. |
| 359 | * |
| 360 | * @return |
| 361 | * - < 0 if str1 is less than str2 |
| 362 | * - 0 if str1 is equal to str2 |
| 363 | * - > 0 if str1 is greater than str2 |
| 364 | */ |
| 365 | PJ_IDECL(int) pj_stricmp(const pj_str_t *str1, const pj_str_t *str2); |
| 366 | |
| 367 | /** |
| 368 | * Perform lowercase comparison to the strings which consists of only |
| 369 | * alnum characters. More over, it will only return non-zero if both |
| 370 | * strings are not equal, not the usual negative or positive value. |
| 371 | * |
| 372 | * If non-alnum inputs are given, then the function may mistakenly |
| 373 | * treat two strings as equal. |
| 374 | * |
| 375 | * @param str1 The string to compare. |
| 376 | * @param str2 The string to compare. |
| 377 | * @param len The length to compare. |
| 378 | * |
| 379 | * @return |
| 380 | * - 0 if str1 is equal to str2 |
| 381 | * - (-1) if not equal. |
| 382 | */ |
| 383 | #if defined(PJ_HAS_STRICMP_ALNUM) && PJ_HAS_STRICMP_ALNUM!=0 |
| 384 | PJ_IDECL(int) strnicmp_alnum(const char *str1, const char *str2, |
| 385 | int len); |
| 386 | #else |
| 387 | #define strnicmp_alnum pj_ansi_strnicmp |
| 388 | #endif |
| 389 | |
| 390 | /** |
| 391 | * Perform lowercase comparison to the strings which consists of only |
| 392 | * alnum characters. More over, it will only return non-zero if both |
| 393 | * strings are not equal, not the usual negative or positive value. |
| 394 | * |
| 395 | * If non-alnum inputs are given, then the function may mistakenly |
| 396 | * treat two strings as equal. |
| 397 | * |
| 398 | * @param str1 The string to compare. |
| 399 | * @param str2 The string to compare. |
| 400 | * |
| 401 | * @return |
| 402 | * - 0 if str1 is equal to str2 |
| 403 | * - (-1) if not equal. |
| 404 | */ |
| 405 | #if defined(PJ_HAS_STRICMP_ALNUM) && PJ_HAS_STRICMP_ALNUM!=0 |
| 406 | PJ_IDECL(int) pj_stricmp_alnum(const pj_str_t *str1, const pj_str_t *str2); |
| 407 | #else |
| 408 | #define pj_stricmp_alnum pj_stricmp |
| 409 | #endif |
| 410 | |
| 411 | /** |
| 412 | * Perform case-insensitive comparison to the strings. |
| 413 | * |
| 414 | * @param str1 The string to compare. |
| 415 | * @param str2 The string to compare. |
| 416 | * |
| 417 | * @return |
| 418 | * - < 0 if str1 is less than str2 |
| 419 | * - 0 if str1 is identical to str2 |
| 420 | * - > 0 if str1 is greater than str2 |
| 421 | */ |
| 422 | PJ_IDECL(int) pj_stricmp2( const pj_str_t *str1, const char *str2); |
| 423 | |
| 424 | /** |
| 425 | * Perform case-insensitive comparison to the strings. |
| 426 | * |
| 427 | * @param str1 The string to compare. |
| 428 | * @param str2 The string to compare. |
| 429 | * @param len The maximum number of characters to compare. |
| 430 | * |
| 431 | * @return |
| 432 | * - < 0 if str1 is less than str2 |
| 433 | * - 0 if str1 is identical to str2 |
| 434 | * - > 0 if str1 is greater than str2 |
| 435 | */ |
| 436 | PJ_IDECL(int) pj_strnicmp( const pj_str_t *str1, const pj_str_t *str2, |
| 437 | pj_size_t len); |
| 438 | |
| 439 | /** |
| 440 | * Perform case-insensitive comparison to the strings. |
| 441 | * |
| 442 | * @param str1 The string to compare. |
| 443 | * @param str2 The string to compare. |
| 444 | * @param len The maximum number of characters to compare. |
| 445 | * |
| 446 | * @return |
| 447 | * - < 0 if str1 is less than str2 |
| 448 | * - 0 if str1 is identical to str2 |
| 449 | * - > 0 if str1 is greater than str2 |
| 450 | */ |
| 451 | PJ_IDECL(int) pj_strnicmp2( const pj_str_t *str1, const char *str2, |
| 452 | pj_size_t len); |
| 453 | |
| 454 | /** |
| 455 | * Concatenate strings. |
| 456 | * |
| 457 | * @param dst The destination string. |
| 458 | * @param src The source string. |
| 459 | */ |
| 460 | PJ_IDECL(void) pj_strcat(pj_str_t *dst, const pj_str_t *src); |
| 461 | |
| 462 | |
| 463 | /** |
| 464 | * Concatenate strings. |
| 465 | * |
| 466 | * @param dst The destination string. |
| 467 | * @param src The source string. |
| 468 | */ |
| 469 | PJ_IDECL(void) pj_strcat2(pj_str_t *dst, const char *src); |
| 470 | |
| 471 | |
| 472 | /** |
| 473 | * Finds a character in a string. |
| 474 | * |
| 475 | * @param str The string. |
| 476 | * @param chr The character to find. |
| 477 | * |
| 478 | * @return the pointer to first character found, or NULL. |
| 479 | */ |
| 480 | PJ_INLINE(char*) pj_strchr( const pj_str_t *str, int chr) |
| 481 | { |
| 482 | if (str->slen == 0) |
| 483 | return NULL; |
| 484 | return (char*) memchr((char*)str->ptr, chr, str->slen); |
| 485 | } |
| 486 | |
| 487 | |
| 488 | /** |
| 489 | * Find the first index of character, in a string, that does not belong to a |
| 490 | * set of characters. |
| 491 | * |
| 492 | * @param str The string. |
| 493 | * @param set_char The string containing the set of characters. |
| 494 | * |
| 495 | * @return the index of the first character in the str that doesn't belong to |
| 496 | * set_char. If str starts with a character not in set_char, return 0. |
| 497 | */ |
| 498 | PJ_DECL(pj_ssize_t) pj_strspn(const pj_str_t *str, const pj_str_t *set_char); |
| 499 | |
| 500 | |
| 501 | /** |
| 502 | * Find the first index of character, in a string, that does not belong to a |
| 503 | * set of characters. |
| 504 | * |
| 505 | * @param str The string. |
| 506 | * @param set_char The string containing the set of characters. |
| 507 | * |
| 508 | * @return the index of the first character in the str that doesn't belong to |
| 509 | * set_char. If str starts with a character not in set_char, return 0. |
| 510 | */ |
| 511 | PJ_DECL(pj_ssize_t) pj_strspn2(const pj_str_t *str, const char *set_char); |
| 512 | |
| 513 | |
| 514 | /** |
| 515 | * Find the first index of character, in a string, that belong to a set of |
| 516 | * characters. |
| 517 | * |
| 518 | * @param str The string. |
| 519 | * @param set_char The string containing the set of characters. |
| 520 | * |
| 521 | * @return the index of the first character in the str that belong to |
| 522 | * set_char. If no match is found, return the length of str. |
| 523 | */ |
| 524 | PJ_DECL(pj_ssize_t) pj_strcspn(const pj_str_t *str, const pj_str_t *set_char); |
| 525 | |
| 526 | |
| 527 | /** |
| 528 | * Find the first index of character, in a string, that belong to a set of |
| 529 | * characters. |
| 530 | * |
| 531 | * @param str The string. |
| 532 | * @param set_char The string containing the set of characters. |
| 533 | * |
| 534 | * @return the index of the first character in the str that belong to |
| 535 | * set_char. If no match is found, return the length of str. |
| 536 | */ |
| 537 | PJ_DECL(pj_ssize_t) pj_strcspn2(const pj_str_t *str, const char *set_char); |
| 538 | |
| 539 | |
| 540 | /** |
| 541 | * Find tokens from a string using the delimiter. |
| 542 | * |
| 543 | * @param str The string. |
| 544 | * @param delim The string containing the delimiter. It might contain |
| 545 | * multiple character treated as unique set. If same character |
| 546 | * was found on the set, it will be skipped. |
| 547 | * @param tok The string containing the token. |
| 548 | * @param start_idx The search will start from this index. |
| 549 | * |
| 550 | * @return the index of token from the str, or the length of the str |
| 551 | * if the token is not found. |
| 552 | */ |
| 553 | PJ_DECL(pj_ssize_t) pj_strtok(const pj_str_t *str, const pj_str_t *delim, |
| 554 | pj_str_t *tok, pj_size_t start_idx); |
| 555 | |
| 556 | |
| 557 | /** |
| 558 | * Find tokens from a string using the delimiter. |
| 559 | * |
| 560 | * @param str The string. |
| 561 | * @param delim The string containing the delimiter. It might contain |
| 562 | * multiple character treated as unique set. If same character |
| 563 | * was found on the set, it will be skipped. |
| 564 | * @param tok The string containing the token. |
| 565 | * @param start_idx The search will start from this index. |
| 566 | * |
| 567 | * @return the index of token from the str, or the length of the str |
| 568 | * if the token is not found. |
| 569 | */ |
| 570 | PJ_DECL(pj_ssize_t) pj_strtok2(const pj_str_t *str, const char *delim, |
| 571 | pj_str_t *tok, pj_size_t start_idx); |
| 572 | |
| 573 | |
| 574 | /** |
| 575 | * Find the occurence of a substring substr in string str. |
| 576 | * |
| 577 | * @param str The string to search. |
| 578 | * @param substr The string to search for. |
| 579 | * |
| 580 | * @return the pointer to the position of substr in str, or NULL. Note |
| 581 | * that if str is not NULL terminated, the returned pointer |
| 582 | * is pointing to non-NULL terminated string. |
| 583 | */ |
| 584 | PJ_DECL(char*) pj_strstr(const pj_str_t *str, const pj_str_t *substr); |
| 585 | |
| 586 | /** |
| 587 | * Performs substring lookup like pj_strstr() but ignores the case of |
| 588 | * both strings. |
| 589 | * |
| 590 | * @param str The string to search. |
| 591 | * @param substr The string to search for. |
| 592 | * |
| 593 | * @return the pointer to the position of substr in str, or NULL. Note |
| 594 | * that if str is not NULL terminated, the returned pointer |
| 595 | * is pointing to non-NULL terminated string. |
| 596 | */ |
| 597 | PJ_DECL(char*) pj_stristr(const pj_str_t *str, const pj_str_t *substr); |
| 598 | |
| 599 | /** |
| 600 | * Remove (trim) leading whitespaces from the string. |
| 601 | * |
| 602 | * @param str The string. |
| 603 | * |
| 604 | * @return the string. |
| 605 | */ |
| 606 | PJ_DECL(pj_str_t*) pj_strltrim( pj_str_t *str ); |
| 607 | |
| 608 | /** |
| 609 | * Remove (trim) the trailing whitespaces from the string. |
| 610 | * |
| 611 | * @param str The string. |
| 612 | * |
| 613 | * @return the string. |
| 614 | */ |
| 615 | PJ_DECL(pj_str_t*) pj_strrtrim( pj_str_t *str ); |
| 616 | |
| 617 | /** |
| 618 | * Remove (trim) leading and trailing whitespaces from the string. |
| 619 | * |
| 620 | * @param str The string. |
| 621 | * |
| 622 | * @return the string. |
| 623 | */ |
| 624 | PJ_IDECL(pj_str_t*) pj_strtrim( pj_str_t *str ); |
| 625 | |
| 626 | /** |
| 627 | * Initialize the buffer with some random string. Note that the |
| 628 | * generated string is not NULL terminated. |
| 629 | * |
| 630 | * @param str the string to store the result. |
| 631 | * @param length the length of the random string to generate. |
| 632 | * |
| 633 | * @return the string. |
| 634 | */ |
| 635 | PJ_DECL(char*) pj_create_random_string(char *str, pj_size_t length); |
| 636 | |
| 637 | /** |
| 638 | * Convert string to signed integer. The conversion will stop as |
| 639 | * soon as non-digit character is found or all the characters have |
| 640 | * been processed. |
| 641 | * |
| 642 | * @param str the string. |
| 643 | * |
| 644 | * @return the integer. |
| 645 | */ |
| 646 | PJ_DECL(long) pj_strtol(const pj_str_t *str); |
| 647 | |
| 648 | /** |
| 649 | * Convert string to signed long integer. The conversion will stop as |
| 650 | * soon as non-digit character is found or all the characters have |
| 651 | * been processed. |
| 652 | * |
| 653 | * @param str the string. |
| 654 | * @param value Pointer to a long to receive the value. |
| 655 | * |
| 656 | * @return PJ_SUCCESS if successful. Otherwise: |
| 657 | * PJ_ETOOSMALL if the value was an impossibly long negative number. |
| 658 | * In this case *value will be set to LONG_MIN. |
| 659 | * \n |
| 660 | * PJ_ETOOBIG if the value was an impossibly long positive number. |
| 661 | * In this case, *value will be set to LONG_MAX. |
| 662 | * \n |
| 663 | * PJ_EINVAL if the input string was NULL, the value pointer was NULL |
| 664 | * or the input string could not be parsed at all such as starting with |
| 665 | * a character other than a '+', '-' or not in the '0' - '9' range. |
| 666 | * In this case, *value will be left untouched. |
| 667 | */ |
| 668 | PJ_DECL(pj_status_t) pj_strtol2(const pj_str_t *str, long *value); |
| 669 | |
| 670 | |
| 671 | /** |
| 672 | * Convert string to unsigned integer. The conversion will stop as |
| 673 | * soon as non-digit character is found or all the characters have |
| 674 | * been processed. |
| 675 | * |
| 676 | * @param str the string. |
| 677 | * |
| 678 | * @return the unsigned integer. |
| 679 | */ |
| 680 | PJ_DECL(unsigned long) pj_strtoul(const pj_str_t *str); |
| 681 | |
| 682 | /** |
| 683 | * Convert strings to an unsigned long-integer value. |
| 684 | * This function stops reading the string input either when the number |
| 685 | * of characters has exceeded the length of the input or it has read |
| 686 | * the first character it cannot recognize as part of a number, that is |
| 687 | * a character greater than or equal to base. |
| 688 | * |
| 689 | * @param str The input string. |
| 690 | * @param endptr Optional pointer to receive the remainder/unparsed |
| 691 | * portion of the input. |
| 692 | * @param base Number base to use. |
| 693 | * |
| 694 | * @return the unsigned integer number. |
| 695 | */ |
| 696 | PJ_DECL(unsigned long) pj_strtoul2(const pj_str_t *str, pj_str_t *endptr, |
| 697 | unsigned base); |
| 698 | |
| 699 | /** |
| 700 | * Convert string to unsigned long integer. The conversion will stop as |
| 701 | * soon as non-digit character is found or all the characters have |
| 702 | * been processed. |
| 703 | * |
| 704 | * @param str The input string. |
| 705 | * @param value Pointer to an unsigned long to receive the value. |
| 706 | * @param base Number base to use. |
| 707 | * |
| 708 | * @return PJ_SUCCESS if successful. Otherwise: |
| 709 | * PJ_ETOOBIG if the value was an impossibly long positive number. |
| 710 | * In this case, *value will be set to ULONG_MAX. |
| 711 | * \n |
| 712 | * PJ_EINVAL if the input string was NULL, the value pointer was NULL |
| 713 | * or the input string could not be parsed at all such as starting |
| 714 | * with a character outside the base character range. In this case, |
| 715 | * *value will be left untouched. |
| 716 | */ |
| 717 | PJ_DECL(pj_status_t) pj_strtoul3(const pj_str_t *str, unsigned long *value, |
| 718 | unsigned base); |
| 719 | |
| 720 | /** |
| 721 | * Convert string to generic unsigned integer. The conversion will stop as |
| 722 | * soon as non-digit character is found or all the characters have |
| 723 | * been processed. |
| 724 | * |
| 725 | * @param str The input string. |
| 726 | * @param value Pointer to an unsigned integer to receive the value. |
| 727 | * The value will be a 64 bit unsigned integer if the system |
| 728 | * supports it, otherwise a 32 bit unsigned integer. |
| 729 | * @param base Number base to use. |
| 730 | * |
| 731 | * @return PJ_SUCCESS if successful. Otherwise: |
| 732 | * PJ_ETOOBIG if the value was an impossibly long positive number. |
| 733 | * In this case, *value will be set to ULLONG_MAX (for 64 bit) or |
| 734 | * ULONG_MAX (for 32 bit). |
| 735 | * \n |
| 736 | * PJ_EINVAL if the input string was NULL, the value pointer was NULL |
| 737 | * or the input string could not be parsed at all such as starting |
| 738 | * with a character outside the base character range. In this case, |
| 739 | * *value will be left untouched. |
| 740 | */ |
| 741 | PJ_DECL(pj_status_t) pj_strtoul4(const pj_str_t *str, pj_uint_t *value, |
| 742 | unsigned base); |
| 743 | |
| 744 | |
| 745 | /** |
| 746 | * Convert string to float. |
| 747 | * |
| 748 | * @param str the string. |
| 749 | * |
| 750 | * @return the value. |
| 751 | */ |
| 752 | PJ_DECL(float) pj_strtof(const pj_str_t *str); |
| 753 | |
| 754 | /** |
| 755 | * Utility to convert unsigned integer to string. Note that the |
| 756 | * string will be NULL terminated. |
| 757 | * |
| 758 | * @param val the unsigned integer value. |
| 759 | * @param buf the buffer |
| 760 | * |
| 761 | * @return the number of characters written |
| 762 | */ |
| 763 | PJ_DECL(int) pj_utoa(unsigned long val, char *buf); |
| 764 | |
| 765 | /** |
| 766 | * Utility to convert generic unsigned integer to string. Note that the |
| 767 | * string will be NULL terminated. |
| 768 | * |
| 769 | * This function will take 64 bit unsigned integer if the system has one, |
| 770 | * otherwise it takes 32 bit unsigned integer. |
| 771 | * |
| 772 | * @param val the unsigned integer value. |
| 773 | * @param buf the buffer |
| 774 | * |
| 775 | * @return the number of characters written |
| 776 | */ |
| 777 | PJ_DECL(int) pj_utoa2(pj_uint_t val, char *buf); |
| 778 | |
| 779 | /** |
| 780 | * Convert unsigned integer to string with minimum digits. Note that the |
| 781 | * string will be NULL terminated. |
| 782 | * |
| 783 | * @param val The unsigned integer value. |
| 784 | * @param buf The buffer. |
| 785 | * @param min_dig Minimum digits to be printed, or zero to specify no |
| 786 | * minimum digit. |
| 787 | * @param pad The padding character to be put in front of the string |
| 788 | * when the digits is less than minimum. |
| 789 | * |
| 790 | * @return the number of characters written. |
| 791 | */ |
| 792 | PJ_DECL(int) pj_utoa_pad( unsigned long val, char *buf, int min_dig, int pad); |
| 793 | |
| 794 | /** |
| 795 | * Convert generic unsigned integer to string with minimum digits. Note that |
| 796 | * the string will be NULL terminated. |
| 797 | * |
| 798 | * This function will take 64 bit unsigned integer if the system has one, |
| 799 | * otherwise it takes 32 bit unsigned integer. |
| 800 | * |
| 801 | * @param val The unsigned integer value. |
| 802 | * @param buf The buffer. |
| 803 | * @param min_dig Minimum digits to be printed, or zero to specify no |
| 804 | * minimum digit. |
| 805 | * @param pad The padding character to be put in front of the string |
| 806 | * when the digits is less than minimum. |
| 807 | * |
| 808 | * @return the number of characters written. |
| 809 | */ |
| 810 | PJ_DECL(int) pj_utoa_pad2( pj_uint_t val, char *buf, int min_dig, int pad); |
| 811 | |
| 812 | |
| 813 | /** |
| 814 | * Fill the memory location with zero. |
| 815 | * |
| 816 | * @param dst The destination buffer. |
| 817 | * @param size The number of bytes. |
| 818 | */ |
| 819 | PJ_INLINE(void) pj_bzero(void *dst, pj_size_t size) |
| 820 | { |
| 821 | #if defined(PJ_HAS_BZERO) && PJ_HAS_BZERO!=0 |
| 822 | bzero(dst, size); |
| 823 | #else |
| 824 | memset(dst, 0, size); |
| 825 | #endif |
| 826 | } |
| 827 | |
| 828 | |
| 829 | /** |
| 830 | * Fill the memory location with value. |
| 831 | * |
| 832 | * @param dst The destination buffer. |
| 833 | * @param c Character to set. |
| 834 | * @param size The number of characters. |
| 835 | * |
| 836 | * @return the value of dst. |
| 837 | */ |
| 838 | PJ_INLINE(void*) pj_memset(void *dst, int c, pj_size_t size) |
| 839 | { |
| 840 | return memset(dst, c, size); |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Copy buffer. |
| 845 | * |
| 846 | * @param dst The destination buffer. |
| 847 | * @param src The source buffer. |
| 848 | * @param size The size to copy. |
| 849 | * |
| 850 | * @return the destination buffer. |
| 851 | */ |
| 852 | PJ_INLINE(void*) pj_memcpy(void *dst, const void *src, pj_size_t size) |
| 853 | { |
| 854 | return memcpy(dst, src, size); |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * Move memory. |
| 859 | * |
| 860 | * @param dst The destination buffer. |
| 861 | * @param src The source buffer. |
| 862 | * @param size The size to copy. |
| 863 | * |
| 864 | * @return the destination buffer. |
| 865 | */ |
| 866 | PJ_INLINE(void*) pj_memmove(void *dst, const void *src, pj_size_t size) |
| 867 | { |
| 868 | return memmove(dst, src, size); |
| 869 | } |
| 870 | |
| 871 | /** |
| 872 | * Compare buffers. |
| 873 | * |
| 874 | * @param buf1 The first buffer. |
| 875 | * @param buf2 The second buffer. |
| 876 | * @param size The size to compare. |
| 877 | * |
| 878 | * @return negative, zero, or positive value. |
| 879 | */ |
| 880 | PJ_INLINE(int) pj_memcmp(const void *buf1, const void *buf2, pj_size_t size) |
| 881 | { |
| 882 | return memcmp(buf1, buf2, size); |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * Find character in the buffer. |
| 887 | * |
| 888 | * @param buf The buffer. |
| 889 | * @param c The character to find. |
| 890 | * @param size The size to check. |
| 891 | * |
| 892 | * @return the pointer to location where the character is found, or NULL if |
| 893 | * not found. |
| 894 | */ |
| 895 | PJ_INLINE(void*) pj_memchr(const void *buf, int c, pj_size_t size) |
| 896 | { |
| 897 | return (void*)memchr((void*)buf, c, size); |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * Copy the string, or as much of it as fits, into the dest buffer. |
| 902 | * Regardless of whether all characters were copied, the destination |
| 903 | * buffer will be null terminated, unless dst_size is zero which in |
| 904 | * this case nothing will be written to dst and the function will |
| 905 | * return -PJ_ETOOBIG. |
| 906 | * |
| 907 | * @param dst The destination string. |
| 908 | * @param src The source string. |
| 909 | * @param dst_size The full size of the destination string buffer. |
| 910 | * |
| 911 | * @return The number of characters copied (not including the trailing NUL) or |
| 912 | * -PJ_ETOOBIG if the destination buffer wasn't big enough, |
| 913 | * -PJ_EINVAL if the dst or src is NULL. |
| 914 | */ |
| 915 | PJ_DECL(int) pj_ansi_strxcpy(char *dst, const char *src, pj_size_t dst_size); |
| 916 | |
| 917 | |
| 918 | /** |
| 919 | * Same as pj_ansi_strxcpy() but takes pj_str_t as the source. |
| 920 | * If src contains null character, copying will stop at the first null |
| 921 | * character in src. |
| 922 | * |
| 923 | * @param dst The destination string. |
| 924 | * @param src The source string. |
| 925 | * @param dst_size The full size of the destination string buffer. |
| 926 | * |
| 927 | * @return The number of characters copied (not including the trailing NUL) or |
| 928 | * -PJ_ETOOBIG if the destination buffer wasn't big enough, |
| 929 | * -PJ_EINVAL if the dst or src is NULL. |
| 930 | */ |
| 931 | PJ_DECL(int) pj_ansi_strxcpy2(char *dst, const pj_str_t *src, |
| 932 | pj_size_t dst_size); |
| 933 | |
| 934 | |
| 935 | /** |
| 936 | * Concatenate src, or as much of it as fits, into the dest buffer. |
| 937 | * Regardless of whether all characters were copied, the destination |
| 938 | * buffer will be null terminated, unless dst_size is zero which in |
| 939 | * this case nothing will be written to dst and the function will |
| 940 | * return -PJ_ETOOBIG. |
| 941 | * |
| 942 | * @param dst The destination string. |
| 943 | * @param src The source string. |
| 944 | * @param dst_size The full size of the destination string buffer. |
| 945 | * |
| 946 | * @return Final length of dst string (not including the trailing NUL) or |
| 947 | * -PJ_ETOOBIG if the destination buffer wasn't big enough, |
| 948 | * -PJ_EINVAL if the dst or src is NULL. |
| 949 | */ |
| 950 | PJ_DECL(int) pj_ansi_strxcat(char *dst, const char *src, pj_size_t dst_size); |
| 951 | |
| 952 | /** |
| 953 | * @} |
| 954 | */ |
| 955 | |
| 956 | #if PJ_FUNCTIONS_ARE_INLINED |
| 957 | # include <pj/string_i.h> |
| 958 | #endif |
| 959 | |
| 960 | PJ_END_DECL |
| 961 | |
| 962 | #endif /* __PJ_STRING_H__ */ |
| 963 |