blob: 6949b64c85716ed3e1313f95a4263ce86d45ce4e [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
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 Prijono9033e312005-11-21 02:08:39 +000029
30
31PJ_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 */
79PJ_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 */
89PJ_INLINE(const pj_str_t*) pj_cstr(pj_str_t *str, const char *s)
90{
91 str->ptr = (char*)s;
Benny Prijono385153d2006-05-24 10:25:34 +000092 str->slen = s ? (pj_ssize_t)strlen(s) : 0;
Benny Prijono9033e312005-11-21 02:08:39 +000093 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 */
105PJ_INLINE(pj_str_t*) pj_strset( pj_str_t *str, char *ptr, pj_size_t length)
106{
107 str->ptr = ptr;
Benny Prijono385153d2006-05-24 10:25:34 +0000108 str->slen = (pj_ssize_t)length;
Benny Prijono9033e312005-11-21 02:08:39 +0000109 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 */
121PJ_INLINE(pj_str_t*) pj_strset2( pj_str_t *str, char *src)
122{
123 str->ptr = src;
Benny Prijono385153d2006-05-24 10:25:34 +0000124 str->slen = src ? (pj_ssize_t)strlen(src) : 0;
Benny Prijono9033e312005-11-21 02:08:39 +0000125 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 */
137PJ_INLINE(pj_str_t*) pj_strset3( pj_str_t *str, char *begin, char *end )
138{
139 str->ptr = begin;
Benny Prijono385153d2006-05-24 10:25:34 +0000140 str->slen = (pj_ssize_t)(end-begin);
Benny Prijono9033e312005-11-21 02:08:39 +0000141 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 */
152PJ_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 */
162PJ_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 */
172PJ_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 */
183PJ_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 */
197PJ_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 */
209PJ_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 Prijono942452b2006-03-02 21:12:28 +0000216 * @param pool The pool.
217 * @param dst The string result.
218 * @param src The string to duplicate.
219 *
220 * @return The string result.
Benny Prijono9033e312005-11-21 02:08:39 +0000221 */
222PJ_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 */
235PJ_IDECL(pj_str_t*) pj_strdup2(pj_pool_t *pool,
236 pj_str_t *dst,
237 const char *src);
238
239/**
Benny Prijono942452b2006-03-02 21:12:28 +0000240 * 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 */
248PJ_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 Prijono9033e312005-11-21 02:08:39 +0000254 * Duplicate string.
255 *
256 * @param pool The pool.
257 * @param src The string to duplicate.
258 *
259 * @return the string result.
260 */
261PJ_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 */
270PJ_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 */
282PJ_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 */
298PJ_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 */
311PJ_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 */
325PJ_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 */
340PJ_IDECL(int) pj_strncmp2( const pj_str_t *str1, const char *str2,
341 pj_size_t len);
342
343/**
Benny Prijono18a051b2007-06-28 00:50:10 +0000344 * Perform case-insensitive comparison to the strings.
Benny Prijono9033e312005-11-21 02:08:39 +0000345 *
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 Prijonoa7944bb2005-11-21 17:01:50 +0000351 * - 0 if str1 is equal to str2
Benny Prijono9033e312005-11-21 02:08:39 +0000352 * - > 0 if str1 is greater than str2
353 */
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000354PJ_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 Prijonocec5f4a2005-11-22 23:51:50 +0000359 * strings are not equal, not the usual negative or positive value.
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000360 *
361 * If non-alnum inputs are given, then the function may mistakenly
Benny Prijonocec5f4a2005-11-22 23:51:50 +0000362 * 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 Prijonodcf29962006-03-23 18:03:40 +0000372#if defined(PJ_HAS_STRICMP_ALNUM) && PJ_HAS_STRICMP_ALNUM!=0
Benny Prijonocec5f4a2005-11-22 23:51:50 +0000373PJ_IDECL(int) strnicmp_alnum(const char *str1, const char *str2,
374 int len);
Benny Prijonodcf29962006-03-23 18:03:40 +0000375#else
Benny Prijonoc4c8f242006-08-01 23:01:55 +0000376#define strnicmp_alnum pj_ansi_strnicmp
Benny Prijonodcf29962006-03-23 18:03:40 +0000377#endif
Benny Prijonocec5f4a2005-11-22 23:51:50 +0000378
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 Prijonoa7944bb2005-11-21 17:01:50 +0000386 *
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 Prijonodcf29962006-03-23 18:03:40 +0000394#if defined(PJ_HAS_STRICMP_ALNUM) && PJ_HAS_STRICMP_ALNUM!=0
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000395PJ_IDECL(int) pj_stricmp_alnum(const pj_str_t *str1, const pj_str_t *str2);
Benny Prijonodcf29962006-03-23 18:03:40 +0000396#else
397#define pj_stricmp_alnum pj_stricmp
398#endif
Benny Prijono9033e312005-11-21 02:08:39 +0000399
400/**
Benny Prijono18a051b2007-06-28 00:50:10 +0000401 * Perform case-insensitive comparison to the strings.
Benny Prijono9033e312005-11-21 02:08:39 +0000402 *
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 */
411PJ_IDECL(int) pj_stricmp2( const pj_str_t *str1, const char *str2);
412
413/**
Benny Prijono18a051b2007-06-28 00:50:10 +0000414 * Perform case-insensitive comparison to the strings.
Benny Prijono9033e312005-11-21 02:08:39 +0000415 *
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 */
425PJ_IDECL(int) pj_strnicmp( const pj_str_t *str1, const pj_str_t *str2,
426 pj_size_t len);
427
428/**
Benny Prijono18a051b2007-06-28 00:50:10 +0000429 * Perform case-insensitive comparison to the strings.
Benny Prijono9033e312005-11-21 02:08:39 +0000430 *
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 */
440PJ_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 */
449PJ_IDECL(void) pj_strcat(pj_str_t *dst, const pj_str_t *src);
450
Benny Prijono5a9091c2006-02-14 20:59:53 +0000451
452/**
453 * Concatenate strings.
454 *
455 * @param dst The destination string.
456 * @param src The source string.
457 */
Benny Prijono294c2532006-06-16 16:52:51 +0000458PJ_IDECL(void) pj_strcat2(pj_str_t *dst, const char *src);
Benny Prijono5a9091c2006-02-14 20:59:53 +0000459
460
Benny Prijono9033e312005-11-21 02:08:39 +0000461/**
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 */
469PJ_INLINE(char*) pj_strchr( const pj_str_t *str, int chr)
470{
Benny Prijono33cb24a2006-11-01 07:13:45 +0000471 return (char*) memchr((char*)str->ptr, chr, str->slen);
Benny Prijono9033e312005-11-21 02:08:39 +0000472}
473
474/**
Benny Prijonobc2219b2008-01-26 10:45:52 +0000475 * Find the occurence of a substring substr in string str.
476 *
477 * @param str The string to search.
478 * @param substr The string to search fo.
479 *
480 * @return the pointer to the position of substr in str, or NULL. Note
481 * that if str is not NULL terminated, the returned pointer
482 * is pointing to non-NULL terminated string.
483 */
484PJ_DECL(char*) pj_strstr(const pj_str_t *str, const pj_str_t *substr);
485
486/**
487 * Performs substring lookup like pj_strstr() but ignores the case of
488 * both strings.
489 *
490 * @param str The string to search.
491 * @param substr The string to search fo.
492 *
493 * @return the pointer to the position of substr in str, or NULL. Note
494 * that if str is not NULL terminated, the returned pointer
495 * is pointing to non-NULL terminated string.
496 */
497PJ_DECL(char*) pj_stristr(const pj_str_t *str, const pj_str_t *substr);
498
499/**
Benny Prijono9033e312005-11-21 02:08:39 +0000500 * Remove (trim) leading whitespaces from the string.
501 *
502 * @param str The string.
503 *
504 * @return the string.
505 */
506PJ_DECL(pj_str_t*) pj_strltrim( pj_str_t *str );
507
508/**
509 * Remove (trim) the trailing whitespaces from the string.
510 *
511 * @param str The string.
512 *
513 * @return the string.
514 */
515PJ_DECL(pj_str_t*) pj_strrtrim( pj_str_t *str );
516
517/**
518 * Remove (trim) leading and trailing whitespaces from the string.
519 *
520 * @param str The string.
521 *
522 * @return the string.
523 */
524PJ_IDECL(pj_str_t*) pj_strtrim( pj_str_t *str );
525
526/**
Benny Prijonoba5d8e02008-06-19 13:49:20 +0000527 * Initialize the buffer with some random string. Note that the
528 * generated string is not NULL terminated.
Benny Prijono9033e312005-11-21 02:08:39 +0000529 *
530 * @param str the string to store the result.
531 * @param length the length of the random string to generate.
532 *
533 * @return the string.
534 */
535PJ_DECL(char*) pj_create_random_string(char *str, pj_size_t length);
536
537/**
538 * Convert string to unsigned integer.
539 *
540 * @param str the string.
541 *
542 * @return the unsigned integer.
543 */
544PJ_DECL(unsigned long) pj_strtoul(const pj_str_t *str);
545
546/**
Benny Prijonoc7434f62007-04-15 09:58:48 +0000547 * Convert strings to an unsigned long-integer value.
548 * This function stops reading the string input either when the number
549 * of characters has exceeded the length of the input or it has read
550 * the first character it cannot recognize as part of a number, that is
551 * a character greater than or equal to base.
552 *
553 * @param str The input string.
554 * @param endptr Optional pointer to receive the remainder/unparsed
555 * portion of the input.
556 * @param base Number base to use.
557 *
558 * @return the unsigned integer number.
559 */
560PJ_DECL(unsigned long) pj_strtoul2(const pj_str_t *str, pj_str_t *endptr,
561 unsigned base);
562
563/**
Benny Prijono9033e312005-11-21 02:08:39 +0000564 * Utility to convert unsigned integer to string. Note that the
565 * string will be NULL terminated.
566 *
567 * @param val the unsigned integer value.
568 * @param buf the buffer
569 *
570 * @return the number of characters written
571 */
572PJ_DECL(int) pj_utoa(unsigned long val, char *buf);
573
574/**
575 * Convert unsigned integer to string with minimum digits. Note that the
576 * string will be NULL terminated.
577 *
578 * @param val The unsigned integer value.
579 * @param buf The buffer.
580 * @param min_dig Minimum digits to be printed, or zero to specify no
581 * minimum digit.
582 * @param pad The padding character to be put in front of the string
583 * when the digits is less than minimum.
584 *
585 * @return the number of characters written.
586 */
587PJ_DECL(int) pj_utoa_pad( unsigned long val, char *buf, int min_dig, int pad);
588
Benny Prijono1479b652006-07-03 14:18:17 +0000589
590/**
591 * Fill the memory location with zero.
592 *
593 * @param dst The destination buffer.
594 * @param size The number of bytes.
595 */
596PJ_INLINE(void) pj_bzero(void *dst, pj_size_t size)
597{
Benny Prijonoac623b32006-07-03 15:19:31 +0000598#if defined(PJ_HAS_BZERO) && PJ_HAS_BZERO!=0
Benny Prijono1479b652006-07-03 14:18:17 +0000599 bzero(dst, size);
Benny Prijonoac623b32006-07-03 15:19:31 +0000600#else
601 memset(dst, 0, size);
602#endif
Benny Prijono1479b652006-07-03 14:18:17 +0000603}
604
605
Benny Prijono9033e312005-11-21 02:08:39 +0000606/**
607 * Fill the memory location with value.
608 *
609 * @param dst The destination buffer.
610 * @param c Character to set.
611 * @param size The number of characters.
612 *
613 * @return the value of dst.
614 */
615PJ_INLINE(void*) pj_memset(void *dst, int c, pj_size_t size)
616{
617 return memset(dst, c, size);
618}
619
620/**
621 * Copy buffer.
622 *
623 * @param dst The destination buffer.
624 * @param src The source buffer.
625 * @param size The size to copy.
626 *
627 * @return the destination buffer.
628 */
629PJ_INLINE(void*) pj_memcpy(void *dst, const void *src, pj_size_t size)
630{
631 return memcpy(dst, src, size);
632}
633
634/**
635 * Move memory.
636 *
637 * @param dst The destination buffer.
638 * @param src The source buffer.
639 * @param size The size to copy.
640 *
641 * @return the destination buffer.
642 */
643PJ_INLINE(void*) pj_memmove(void *dst, const void *src, pj_size_t size)
644{
645 return memmove(dst, src, size);
646}
647
648/**
649 * Compare buffers.
650 *
651 * @param buf1 The first buffer.
652 * @param buf2 The second buffer.
653 * @param size The size to compare.
654 *
655 * @return negative, zero, or positive value.
656 */
657PJ_INLINE(int) pj_memcmp(const void *buf1, const void *buf2, pj_size_t size)
658{
659 return memcmp(buf1, buf2, size);
660}
661
662/**
663 * Find character in the buffer.
664 *
665 * @param buf The buffer.
666 * @param c The character to find.
667 * @param size The size to check.
668 *
669 * @return the pointer to location where the character is found, or NULL if
670 * not found.
671 */
672PJ_INLINE(void*) pj_memchr(const void *buf, int c, pj_size_t size)
673{
Benny Prijono33cb24a2006-11-01 07:13:45 +0000674 return (void*)memchr((void*)buf, c, size);
Benny Prijono9033e312005-11-21 02:08:39 +0000675}
676
677
678/**
679 * @}
680 */
681
682#if PJ_FUNCTIONS_ARE_INLINED
683# include <pj/string_i.h>
684#endif
685
686PJ_END_DECL
687
688#endif /* __PJ_STRING_H__ */
689