blob: d6fe54ce96c78f2e30e0327fc4680b784e544cb1 [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>
29#include <pj/compat/sprintf.h>
30#include <pj/compat/vsprintf.h>
31
32
33PJ_BEGIN_DECL
34
35/**
36 * @defgroup PJ_PSTR String Operations
37 * @ingroup PJ_DS
38 * @{
39 * This module provides string manipulation API.
40 *
41 * \section pj_pstr_not_null_sec PJLIB String is NOT Null Terminated!
42 *
43 * That is the first information that developers need to know. Instead
44 * of using normal C string, strings in PJLIB are represented as
45 * pj_str_t structure below:
46 *
47 * <pre>
48 * typedef struct pj_str_t
49 * {
50 * char *ptr;
51 * pj_size_t slen;
52 * } pj_str_t;
53 * </pre>
54 *
55 * There are some advantages of using this approach:
56 * - the string can point to arbitrary location in memory even
57 * if the string in that location is not null terminated. This is
58 * most usefull for text parsing, where the parsed text can just
59 * point to the original text in the input. If we use C string,
60 * then we will have to copy the text portion from the input
61 * to a string variable.
62 * - because the length of the string is known, string copy operation
63 * can be made more efficient.
64 *
65 * Most of APIs in PJLIB that expect or return string will represent
66 * the string as pj_str_t instead of normal C string.
67 *
68 * \section pj_pstr_examples_sec Examples
69 *
70 * For some examples, please see:
71 * - @ref page_pjlib_string_test
72 */
73
74/**
75 * Create string initializer from a normal C string.
76 *
77 * @param str Null terminated string to be stored.
78 *
79 * @return pj_str_t.
80 */
81PJ_IDECL(pj_str_t) pj_str(char *str);
82
83/**
84 * Create constant string from normal C string.
85 *
86 * @param str The string to be initialized.
87 * @param s Null terminated string.
88 *
89 * @return pj_str_t.
90 */
91PJ_INLINE(const pj_str_t*) pj_cstr(pj_str_t *str, const char *s)
92{
93 str->ptr = (char*)s;
94 str->slen = s ? strlen(s) : 0;
95 return str;
96}
97
98/**
99 * Set the pointer and length to the specified value.
100 *
101 * @param str the string.
102 * @param ptr pointer to set.
103 * @param length length to set.
104 *
105 * @return the string.
106 */
107PJ_INLINE(pj_str_t*) pj_strset( pj_str_t *str, char *ptr, pj_size_t length)
108{
109 str->ptr = ptr;
110 str->slen = length;
111 return str;
112}
113
114/**
115 * Set the pointer and length of the string to the source string, which
116 * must be NULL terminated.
117 *
118 * @param str the string.
119 * @param src pointer to set.
120 *
121 * @return the string.
122 */
123PJ_INLINE(pj_str_t*) pj_strset2( pj_str_t *str, char *src)
124{
125 str->ptr = src;
126 str->slen = src ? strlen(src) : 0;
127 return str;
128}
129
130/**
131 * Set the pointer and the length of the string.
132 *
133 * @param str The target string.
134 * @param begin The start of the string.
135 * @param end The end of the string.
136 *
137 * @return the target string.
138 */
139PJ_INLINE(pj_str_t*) pj_strset3( pj_str_t *str, char *begin, char *end )
140{
141 str->ptr = begin;
142 str->slen = end-begin;
143 return str;
144}
145
146/**
147 * Assign string.
148 *
149 * @param dst The target string.
150 * @param src The source string.
151 *
152 * @return the target string.
153 */
154PJ_IDECL(pj_str_t*) pj_strassign( pj_str_t *dst, pj_str_t *src );
155
156/**
157 * Copy string contents.
158 *
159 * @param dst The target string.
160 * @param src The source string.
161 *
162 * @return the target string.
163 */
164PJ_IDECL(pj_str_t*) pj_strcpy(pj_str_t *dst, const pj_str_t *src);
165
166/**
167 * Copy string contents.
168 *
169 * @param dst The target string.
170 * @param src The source string.
171 *
172 * @return the target string.
173 */
174PJ_IDECL(pj_str_t*) pj_strcpy2(pj_str_t *dst, const char *src);
175
176/**
177 * Copy source string to destination up to the specified max length.
178 *
179 * @param dst The target string.
180 * @param src The source string.
181 * @param max Maximum characters to copy.
182 *
183 * @return the target string.
184 */
185PJ_IDECL(pj_str_t*) pj_strncpy(pj_str_t *dst, const pj_str_t *src,
186 pj_ssize_t max);
187
188/**
189 * Copy source string to destination up to the specified max length,
190 * and NULL terminate the destination. If source string length is
191 * greater than or equal to max, then max-1 will be copied.
192 *
193 * @param dst The target string.
194 * @param src The source string.
195 * @param max Maximum characters to copy.
196 *
197 * @return the target string.
198 */
199PJ_IDECL(pj_str_t*) pj_strncpy_with_null(pj_str_t *dst, const pj_str_t *src,
200 pj_ssize_t max);
201
202/**
203 * Duplicate string.
204 *
205 * @param pool The pool.
206 * @param dst The string result.
207 * @param src The string to duplicate.
208 *
209 * @return the string result.
210 */
211PJ_IDECL(pj_str_t*) pj_strdup(pj_pool_t *pool,
212 pj_str_t *dst,
213 const pj_str_t *src);
214
215/**
216 * Duplicate string and NULL terminate the destination string.
217 *
218 * @param pool
219 * @param dst
220 * @param src
221 */
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/**
240 * Duplicate string.
241 *
242 * @param pool The pool.
243 * @param src The string to duplicate.
244 *
245 * @return the string result.
246 */
247PJ_IDECL(pj_str_t) pj_strdup3(pj_pool_t *pool, const char *src);
248
249/**
250 * Return the length of the string.
251 *
252 * @param str The string.
253 *
254 * @return the length of the string.
255 */
256PJ_INLINE(pj_size_t) pj_strlen( const pj_str_t *str )
257{
258 return str->slen;
259}
260
261/**
262 * Return the pointer to the string data.
263 *
264 * @param str The string.
265 *
266 * @return the pointer to the string buffer.
267 */
268PJ_INLINE(const char*) pj_strbuf( const pj_str_t *str )
269{
270 return str->ptr;
271}
272
273/**
274 * Compare strings.
275 *
276 * @param str1 The string to compare.
277 * @param str2 The string to compare.
278 *
279 * @return
280 * - < 0 if str1 is less than str2
281 * - 0 if str1 is identical to str2
282 * - > 0 if str1 is greater than str2
283 */
284PJ_IDECL(int) pj_strcmp( const pj_str_t *str1, const pj_str_t *str2);
285
286/**
287 * Compare strings.
288 *
289 * @param str1 The string to compare.
290 * @param str2 The string to compare.
291 *
292 * @return
293 * - < 0 if str1 is less than str2
294 * - 0 if str1 is identical to str2
295 * - > 0 if str1 is greater than str2
296 */
297PJ_IDECL(int) pj_strcmp2( const pj_str_t *str1, const char *str2 );
298
299/**
300 * Compare strings.
301 *
302 * @param str1 The string to compare.
303 * @param str2 The string to compare.
304 * @param len The maximum number of characters 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_strncmp( const pj_str_t *str1, const pj_str_t *str2,
312 pj_size_t len);
313
314/**
315 * Compare strings.
316 *
317 * @param str1 The string to compare.
318 * @param str2 The string to compare.
319 * @param len The maximum number of characters to compare.
320 *
321 * @return
322 * - < 0 if str1 is less than str2
323 * - 0 if str1 is identical to str2
324 * - > 0 if str1 is greater than str2
325 */
326PJ_IDECL(int) pj_strncmp2( const pj_str_t *str1, const char *str2,
327 pj_size_t len);
328
329/**
330 * Perform lowercase comparison to the strings.
331 *
332 * @param str1 The string to compare.
333 * @param str2 The string to compare.
334 *
335 * @return
336 * - < 0 if str1 is less than str2
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000337 * - 0 if str1 is equal to str2
Benny Prijono9033e312005-11-21 02:08:39 +0000338 * - > 0 if str1 is greater than str2
339 */
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000340PJ_IDECL(int) pj_stricmp(const pj_str_t *str1, const pj_str_t *str2);
341
342/**
343 * Perform lowercase comparison to the strings which consists of only
344 * alnum characters. More over, it will only return non-zero if both
Benny Prijonocec5f4a2005-11-22 23:51:50 +0000345 * strings are not equal, not the usual negative or positive value.
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000346 *
347 * If non-alnum inputs are given, then the function may mistakenly
Benny Prijonocec5f4a2005-11-22 23:51:50 +0000348 * treat two strings as equal.
349 *
350 * @param str1 The string to compare.
351 * @param str2 The string to compare.
352 * @param len The length to compare.
353 *
354 * @return
355 * - 0 if str1 is equal to str2
356 * - (-1) if not equal.
357 */
358PJ_IDECL(int) strnicmp_alnum(const char *str1, const char *str2,
359 int len);
360
361/**
362 * Perform lowercase comparison to the strings which consists of only
363 * alnum characters. More over, it will only return non-zero if both
364 * strings are not equal, not the usual negative or positive value.
365 *
366 * If non-alnum inputs are given, then the function may mistakenly
367 * treat two strings as equal.
Benny Prijonoa7944bb2005-11-21 17:01:50 +0000368 *
369 * @param str1 The string to compare.
370 * @param str2 The string to compare.
371 *
372 * @return
373 * - 0 if str1 is equal to str2
374 * - (-1) if not equal.
375 */
376PJ_IDECL(int) pj_stricmp_alnum(const pj_str_t *str1, const pj_str_t *str2);
Benny Prijono9033e312005-11-21 02:08:39 +0000377
378/**
379 * Perform lowercase comparison to the strings.
380 *
381 * @param str1 The string to compare.
382 * @param str2 The string to compare.
383 *
384 * @return
385 * - < 0 if str1 is less than str2
386 * - 0 if str1 is identical to str2
387 * - > 0 if str1 is greater than str2
388 */
389PJ_IDECL(int) pj_stricmp2( const pj_str_t *str1, const char *str2);
390
391/**
392 * Perform lowercase comparison to the strings.
393 *
394 * @param str1 The string to compare.
395 * @param str2 The string to compare.
396 * @param len The maximum number of characters to compare.
397 *
398 * @return
399 * - < 0 if str1 is less than str2
400 * - 0 if str1 is identical to str2
401 * - > 0 if str1 is greater than str2
402 */
403PJ_IDECL(int) pj_strnicmp( const pj_str_t *str1, const pj_str_t *str2,
404 pj_size_t len);
405
406/**
407 * Perform lowercase comparison to the strings.
408 *
409 * @param str1 The string to compare.
410 * @param str2 The string to compare.
411 * @param len The maximum number of characters to compare.
412 *
413 * @return
414 * - < 0 if str1 is less than str2
415 * - 0 if str1 is identical to str2
416 * - > 0 if str1 is greater than str2
417 */
418PJ_IDECL(int) pj_strnicmp2( const pj_str_t *str1, const char *str2,
419 pj_size_t len);
420
421/**
422 * Concatenate strings.
423 *
424 * @param dst The destination string.
425 * @param src The source string.
426 */
427PJ_IDECL(void) pj_strcat(pj_str_t *dst, const pj_str_t *src);
428
429/**
430 * Finds a character in a string.
431 *
432 * @param str The string.
433 * @param chr The character to find.
434 *
435 * @return the pointer to first character found, or NULL.
436 */
437PJ_INLINE(char*) pj_strchr( const pj_str_t *str, int chr)
438{
439 return (char*) memchr(str->ptr, chr, str->slen);
440}
441
442/**
443 * Remove (trim) leading whitespaces from the string.
444 *
445 * @param str The string.
446 *
447 * @return the string.
448 */
449PJ_DECL(pj_str_t*) pj_strltrim( pj_str_t *str );
450
451/**
452 * Remove (trim) the trailing whitespaces from the string.
453 *
454 * @param str The string.
455 *
456 * @return the string.
457 */
458PJ_DECL(pj_str_t*) pj_strrtrim( pj_str_t *str );
459
460/**
461 * Remove (trim) leading and trailing whitespaces from the string.
462 *
463 * @param str The string.
464 *
465 * @return the string.
466 */
467PJ_IDECL(pj_str_t*) pj_strtrim( pj_str_t *str );
468
469/**
470 * Initialize the buffer with some random string.
471 *
472 * @param str the string to store the result.
473 * @param length the length of the random string to generate.
474 *
475 * @return the string.
476 */
477PJ_DECL(char*) pj_create_random_string(char *str, pj_size_t length);
478
479/**
480 * Convert string to unsigned integer.
481 *
482 * @param str the string.
483 *
484 * @return the unsigned integer.
485 */
486PJ_DECL(unsigned long) pj_strtoul(const pj_str_t *str);
487
488/**
489 * Utility to convert unsigned integer to string. Note that the
490 * string will be NULL terminated.
491 *
492 * @param val the unsigned integer value.
493 * @param buf the buffer
494 *
495 * @return the number of characters written
496 */
497PJ_DECL(int) pj_utoa(unsigned long val, char *buf);
498
499/**
500 * Convert unsigned integer to string with minimum digits. Note that the
501 * string will be NULL terminated.
502 *
503 * @param val The unsigned integer value.
504 * @param buf The buffer.
505 * @param min_dig Minimum digits to be printed, or zero to specify no
506 * minimum digit.
507 * @param pad The padding character to be put in front of the string
508 * when the digits is less than minimum.
509 *
510 * @return the number of characters written.
511 */
512PJ_DECL(int) pj_utoa_pad( unsigned long val, char *buf, int min_dig, int pad);
513
514/**
515 * Fill the memory location with value.
516 *
517 * @param dst The destination buffer.
518 * @param c Character to set.
519 * @param size The number of characters.
520 *
521 * @return the value of dst.
522 */
523PJ_INLINE(void*) pj_memset(void *dst, int c, pj_size_t size)
524{
525 return memset(dst, c, size);
526}
527
528/**
529 * Copy buffer.
530 *
531 * @param dst The destination buffer.
532 * @param src The source buffer.
533 * @param size The size to copy.
534 *
535 * @return the destination buffer.
536 */
537PJ_INLINE(void*) pj_memcpy(void *dst, const void *src, pj_size_t size)
538{
539 return memcpy(dst, src, size);
540}
541
542/**
543 * Move memory.
544 *
545 * @param dst The destination buffer.
546 * @param src The source buffer.
547 * @param size The size to copy.
548 *
549 * @return the destination buffer.
550 */
551PJ_INLINE(void*) pj_memmove(void *dst, const void *src, pj_size_t size)
552{
553 return memmove(dst, src, size);
554}
555
556/**
557 * Compare buffers.
558 *
559 * @param buf1 The first buffer.
560 * @param buf2 The second buffer.
561 * @param size The size to compare.
562 *
563 * @return negative, zero, or positive value.
564 */
565PJ_INLINE(int) pj_memcmp(const void *buf1, const void *buf2, pj_size_t size)
566{
567 return memcmp(buf1, buf2, size);
568}
569
570/**
571 * Find character in the buffer.
572 *
573 * @param buf The buffer.
574 * @param c The character to find.
575 * @param size The size to check.
576 *
577 * @return the pointer to location where the character is found, or NULL if
578 * not found.
579 */
580PJ_INLINE(void*) pj_memchr(const void *buf, int c, pj_size_t size)
581{
582 return memchr(buf, c, size);
583}
584
585
586/**
587 * @}
588 */
589
590#if PJ_FUNCTIONS_ARE_INLINED
591# include <pj/string_i.h>
592#endif
593
594PJ_END_DECL
595
596#endif /* __PJ_STRING_H__ */
597