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