blob: 4466567eed9a8ddc4e2627f7cd010f50b3e47852 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
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 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/**
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 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
376#define strnicmp_alnum strnicmp
377#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/**
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 */
411PJ_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 */
425PJ_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 */
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{
471 return (char*) memchr(str->ptr, chr, str->slen);
472}
473
474/**
475 * Remove (trim) leading whitespaces from the string.
476 *
477 * @param str The string.
478 *
479 * @return the string.
480 */
481PJ_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 */
490PJ_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 */
499PJ_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 */
509PJ_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 */
518PJ_DECL(unsigned long) pj_strtoul(const pj_str_t *str);
519
520/**
521 * Utility to convert unsigned integer to string. Note that the
522 * string will be NULL terminated.
523 *
524 * @param val the unsigned integer value.
525 * @param buf the buffer
526 *
527 * @return the number of characters written
528 */
529PJ_DECL(int) pj_utoa(unsigned long val, char *buf);
530
531/**
532 * Convert unsigned integer to string with minimum digits. Note that the
533 * string will be NULL terminated.
534 *
535 * @param val The unsigned integer value.
536 * @param buf The buffer.
537 * @param min_dig Minimum digits to be printed, or zero to specify no
538 * minimum digit.
539 * @param pad The padding character to be put in front of the string
540 * when the digits is less than minimum.
541 *
542 * @return the number of characters written.
543 */
544PJ_DECL(int) pj_utoa_pad( unsigned long val, char *buf, int min_dig, int pad);
545
Benny Prijono1479b652006-07-03 14:18:17 +0000546
547/**
548 * Fill the memory location with zero.
549 *
550 * @param dst The destination buffer.
551 * @param size The number of bytes.
552 */
553PJ_INLINE(void) pj_bzero(void *dst, pj_size_t size)
554{
Benny Prijonoac623b32006-07-03 15:19:31 +0000555#if defined(PJ_HAS_BZERO) && PJ_HAS_BZERO!=0
Benny Prijono1479b652006-07-03 14:18:17 +0000556 bzero(dst, size);
Benny Prijonoac623b32006-07-03 15:19:31 +0000557#else
558 memset(dst, 0, size);
559#endif
Benny Prijono1479b652006-07-03 14:18:17 +0000560}
561
562
Benny Prijono9033e312005-11-21 02:08:39 +0000563/**
564 * Fill the memory location with value.
565 *
566 * @param dst The destination buffer.
567 * @param c Character to set.
568 * @param size The number of characters.
569 *
570 * @return the value of dst.
571 */
572PJ_INLINE(void*) pj_memset(void *dst, int c, pj_size_t size)
573{
574 return memset(dst, c, size);
575}
576
577/**
578 * Copy buffer.
579 *
580 * @param dst The destination buffer.
581 * @param src The source buffer.
582 * @param size The size to copy.
583 *
584 * @return the destination buffer.
585 */
586PJ_INLINE(void*) pj_memcpy(void *dst, const void *src, pj_size_t size)
587{
588 return memcpy(dst, src, size);
589}
590
591/**
592 * Move memory.
593 *
594 * @param dst The destination buffer.
595 * @param src The source buffer.
596 * @param size The size to copy.
597 *
598 * @return the destination buffer.
599 */
600PJ_INLINE(void*) pj_memmove(void *dst, const void *src, pj_size_t size)
601{
602 return memmove(dst, src, size);
603}
604
605/**
606 * Compare buffers.
607 *
608 * @param buf1 The first buffer.
609 * @param buf2 The second buffer.
610 * @param size The size to compare.
611 *
612 * @return negative, zero, or positive value.
613 */
614PJ_INLINE(int) pj_memcmp(const void *buf1, const void *buf2, pj_size_t size)
615{
616 return memcmp(buf1, buf2, size);
617}
618
619/**
620 * Find character in the buffer.
621 *
622 * @param buf The buffer.
623 * @param c The character to find.
624 * @param size The size to check.
625 *
626 * @return the pointer to location where the character is found, or NULL if
627 * not found.
628 */
629PJ_INLINE(void*) pj_memchr(const void *buf, int c, pj_size_t size)
630{
631 return memchr(buf, c, size);
632}
633
634
635/**
636 * @}
637 */
638
639#if PJ_FUNCTIONS_ARE_INLINED
640# include <pj/string_i.h>
641#endif
642
643PJ_END_DECL
644
645#endif /* __PJ_STRING_H__ */
646