2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 * \brief String manipulation functions
23 #ifndef _ASTERISK_STRINGS_H
24 #define _ASTERISK_STRINGS_H
26 /* #define DEBUG_OPAQUE */
30 #include "asterisk/utils.h"
31 #include "asterisk/threadstorage.h"
33 #if defined(DEBUG_OPAQUE)
34 #define __AST_STR_USED used2
35 #define __AST_STR_LEN len2
36 #define __AST_STR_STR str2
37 #define __AST_STR_TS ts2
39 #define __AST_STR_USED used
40 #define __AST_STR_LEN len
41 #define __AST_STR_STR str
42 #define __AST_STR_TS ts
45 /* You may see casts in this header that may seem useless but they ensure this file is C++ clean */
47 #define AS_OR(a,b) (a && ast_str_strlen(a)) ? ast_str_buffer(a) : (b)
50 #define ast_strlen_zero(foo) _ast_strlen_zero(foo, __FILE__, __PRETTY_FUNCTION__, __LINE__)
51 static force_inline int _ast_strlen_zero(const char *s, const char *file, const char *function, int line)
53 if (!s || (*s == '\0')) {
56 if (!strcmp(s, "(null)")) {
57 ast_log(__LOG_WARNING, file, line, function, "Possible programming error: \"(null)\" is not NULL!\n");
63 static force_inline int attribute_pure ast_strlen_zero(const char *s)
65 return (!s || (*s == '\0'));
70 #define ast_strlen_real(a) (a) ? strlen(a) : 0
71 #define ast_strlen_imaginary(a) ast_random()
74 /*! \brief returns the equivalent of logic or for strings:
75 * first one if not empty, otherwise second one.
77 #define S_OR(a, b) ({typeof(&((a)[0])) __x = (a); ast_strlen_zero(__x) ? (b) : __x;})
79 /*! \brief returns the equivalent of logic or for strings, with an additional boolean check:
80 * second one if not empty and first one is true, otherwise third one.
81 * example: S_COR(usewidget, widget, "<no widget>")
83 #define S_COR(a, b, c) ({typeof(&((b)[0])) __x = (b); (a) && !ast_strlen_zero(__x) ? (__x) : (c);})
86 \brief Gets a pointer to the first non-whitespace character in a string.
87 \param str the input string
88 \return a pointer to the first non-whitespace character
91 char * attribute_pure ast_skip_blanks(const char *str),
93 while (*str && ((unsigned char) *str) < 33)
100 \brief Trims trailing whitespace characters from a string.
101 \param str the input string
102 \return a pointer to the modified string
105 char *ast_trim_blanks(char *str),
110 work += strlen(work) - 1;
111 /* It's tempting to only want to erase after we exit this loop,
112 but since ast_trim_blanks *could* receive a constant string
113 (which we presumably wouldn't have to touch), we shouldn't
114 actually set anything unless we must, and it's easier just
115 to set each position to \0 than to keep track of a variable
117 while ((work >= str) && ((unsigned char) *work) < 33)
125 \brief Gets a pointer to first whitespace character in a string.
126 \param str the input string
127 \return a pointer to the first whitespace character
130 char * attribute_pure ast_skip_nonblanks(const char *str),
132 while (*str && ((unsigned char) *str) > 32)
139 \brief Strip leading/trailing whitespace from a string.
140 \param s The string to be stripped (will be modified).
141 \return The stripped string.
143 This functions strips all leading and trailing whitespace
144 characters from the input string, and returns a pointer to
145 the resulting string. The string is modified in place.
148 char *ast_strip(char *s),
150 if ((s = ast_skip_blanks(s))) {
158 \brief Strip leading/trailing whitespace and quotes from a string.
159 \param s The string to be stripped (will be modified).
160 \param beg_quotes The list of possible beginning quote characters.
161 \param end_quotes The list of matching ending quote characters.
162 \return The stripped string.
164 This functions strips all leading and trailing whitespace
165 characters from the input string, and returns a pointer to
166 the resulting string. The string is modified in place.
168 It can also remove beginning and ending quote (or quote-like)
169 characters, in matching pairs. If the first character of the
170 string matches any character in beg_quotes, and the last
171 character of the string is the matching character in
172 end_quotes, then they are removed from the string.
176 ast_strip_quoted(buf, "\"", "\"");
177 ast_strip_quoted(buf, "'", "'");
178 ast_strip_quoted(buf, "[{(", "]})");
181 char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes);
184 \brief Strip backslash for "escaped" semicolons,
185 the string to be stripped (will be modified).
186 \return The stripped string.
188 char *ast_unescape_semicolon(char *s);
191 \brief Convert some C escape sequences \verbatim (\b\f\n\r\t) \endverbatim into the
192 equivalent characters. The string to be converted (will be modified).
193 \return The converted string.
195 char *ast_unescape_c(char *s);
198 \brief Size-limited null-terminating string copy.
199 \param dst The destination buffer.
200 \param src The source string
201 \param size The size of the destination buffer
204 This is similar to \a strncpy, with two important differences:
205 - the destination buffer will \b always be null-terminated
206 - the destination buffer is not filled with zeros past the copied string length
207 These differences make it slightly more efficient, and safer to use since it will
208 not leave the destination buffer unterminated. There is no need to pass an artificially
209 reduced buffer size to this function (unlike \a strncpy), and the buffer does not need
210 to be initialized to zeroes prior to calling this function.
213 void ast_copy_string(char *dst, const char *src, size_t size),
215 while (*src && size) {
219 if (__builtin_expect(!size, 0))
226 \brief Build a string in a buffer, designed to be called repeatedly
228 \note This method is not recommended. New code should use ast_str_*() instead.
230 This is a wrapper for snprintf, that properly handles the buffer pointer
231 and buffer space available.
233 \param buffer current position in buffer to place string into (will be updated on return)
234 \param space remaining space in buffer (will be updated on return)
235 \param fmt printf-style format string
237 \retval non-zero on failure.
239 int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
242 \brief Build a string in a buffer, designed to be called repeatedly
244 This is a wrapper for snprintf, that properly handles the buffer pointer
245 and buffer space available.
247 \return 0 on success, non-zero on failure.
248 \param buffer current position in buffer to place string into (will be updated on return)
249 \param space remaining space in buffer (will be updated on return)
250 \param fmt printf-style format string
251 \param ap varargs list of arguments for format
253 int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap) __attribute__((format(printf, 3, 0)));
256 \brief Given a string regex_string in the form of "/regex/", convert it into the form of "regex"
258 This function will trim one leading / and one trailing / from a given input string
259 ast_str regex_pattern must be preallocated before calling this function
261 \return 0 on success, non-zero on failure.
262 \return 1 if we only stripped a leading /
263 \return 2 if we only stripped a trailing /
264 \return 3 if we did not strip any / characters
265 \param regex_string the string containing /regex/
266 \param regex_pattern the destination ast_str which will contain "regex" after execution
268 int ast_regex_string_to_regex_pattern(const char *regex_string, struct ast_str *regex_pattern);
271 * \brief Make sure something is true.
272 * Determine if a string containing a boolean value is "true".
273 * This function checks to see whether a string passed to it is an indication of an "true" value.
274 * It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
276 * \retval 0 if val is a NULL pointer.
277 * \retval -1 if "true".
278 * \retval 0 otherwise.
280 int attribute_pure ast_true(const char *val);
283 * \brief Make sure something is false.
284 * Determine if a string containing a boolean value is "false".
285 * This function checks to see whether a string passed to it is an indication of an "false" value.
286 * It checks to see if the string is "no", "false", "n", "f", "off" or "0".
288 * \retval 0 if val is a NULL pointer.
289 * \retval -1 if "true".
290 * \retval 0 otherwise.
292 int attribute_pure ast_false(const char *val);
295 * \brief Join an array of strings into a single string.
296 * \param s the resulting string buffer
297 * \param len the length of the result buffer, s
298 * \param w an array of strings to join.
300 * This function will join all of the strings in the array 'w' into a single
301 * string. It will also place a space in the result buffer in between each
304 void ast_join(char *s, size_t len, const char * const w[]);
307 \brief Parse a time (integer) string.
308 \param src String to parse
309 \param dst Destination
310 \param _default Value to use if the string does not contain a valid time
311 \param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details)
313 \retval non-zero on failure.
315 int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed);
318 \brief Parse a time (float) string.
319 \param src String to parse
320 \param dst Destination
321 \param _default Value to use if the string does not contain a valid time
322 \param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details)
323 \return zero on success, non-zero on failure
325 int ast_get_timeval(const char *src, struct timeval *tv, struct timeval _default, int *consumed);
328 * Support for dynamic strings.
330 * A dynamic string is just a C string prefixed by a few control fields
331 * that help setting/appending/extending it using a printf-like syntax.
333 * One should never declare a variable with this type, but only a pointer
336 * struct ast_str *ds;
338 * The pointer can be initialized with the following:
340 * ds = ast_str_create(init_len);
341 * creates a malloc()'ed dynamic string;
343 * ds = ast_str_alloca(init_len);
344 * creates a string on the stack (not very dynamic!).
346 * ds = ast_str_thread_get(ts, init_len)
347 * creates a malloc()'ed dynamic string associated to
348 * the thread-local storage key ts
350 * Finally, the string can be manipulated with the following:
352 * ast_str_set(&buf, max_len, fmt, ...)
353 * ast_str_append(&buf, max_len, fmt, ...)
355 * and their varargs variant
357 * ast_str_set_va(&buf, max_len, ap)
358 * ast_str_append_va(&buf, max_len, ap)
360 * \param max_len The maximum allowed capacity of the ast_str. Note that
361 * if the value of max_len is less than the current capacity of the
362 * ast_str (as returned by ast_str_size), then the parameter is effectively
364 * 0 means unlimited, -1 means "at most the available space"
366 * \return All the functions return <0 in case of error, or the
367 * length of the string added to the buffer otherwise. Note that
368 * in most cases where an error is returned, characters ARE written
372 /*! \brief The descriptor of a dynamic string
373 * XXX storage will be optimized later if needed
374 * We use the ts field to indicate the type of storage.
375 * Three special constants indicate malloc, alloca() or static
376 * variables, all other values indicate a
377 * struct ast_threadstorage pointer.
380 size_t __AST_STR_LEN; /*!< The current maximum length of the string */
381 size_t __AST_STR_USED; /*!< Amount of space used */
382 struct ast_threadstorage *__AST_STR_TS; /*!< What kind of storage is this ? */
383 #define DS_MALLOC ((struct ast_threadstorage *)1)
384 #define DS_ALLOCA ((struct ast_threadstorage *)2)
385 #define DS_STATIC ((struct ast_threadstorage *)3) /* not supported yet */
386 char __AST_STR_STR[0]; /*!< The string buffer */
390 * \brief Create a malloc'ed dynamic length string
392 * \param init_len This is the initial length of the string buffer
394 * \return This function returns a pointer to the dynamic string length. The
395 * result will be NULL in the case of a memory allocation error.
397 * \note The result of this function is dynamically allocated memory, and must
398 * be free()'d after it is no longer needed.
400 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
401 #define ast_str_create(a) _ast_str_create(a,__FILE__,__LINE__,__PRETTY_FUNCTION__)
403 struct ast_str * attribute_malloc _ast_str_create(size_t init_len,
404 const char *file, int lineno, const char *func),
408 buf = (struct ast_str *)__ast_calloc(1, sizeof(*buf) + init_len, file, lineno, func);
412 buf->__AST_STR_LEN = init_len;
413 buf->__AST_STR_USED = 0;
414 buf->__AST_STR_TS = DS_MALLOC;
421 struct ast_str * attribute_malloc ast_str_create(size_t init_len),
425 buf = (struct ast_str *)ast_calloc(1, sizeof(*buf) + init_len);
429 buf->__AST_STR_LEN = init_len;
430 buf->__AST_STR_USED = 0;
431 buf->__AST_STR_TS = DS_MALLOC;
438 /*! \brief Reset the content of a dynamic string.
439 * Useful before a series of ast_str_append.
442 void ast_str_reset(struct ast_str *buf),
445 buf->__AST_STR_USED = 0;
446 if (buf->__AST_STR_LEN) {
447 buf->__AST_STR_STR[0] = '\0';
453 /*! \brief Update the length of the buffer, after using ast_str merely as a buffer.
454 * \param buf A pointer to the ast_str string.
457 void ast_str_update(struct ast_str *buf),
459 buf->__AST_STR_USED = strlen(buf->__AST_STR_STR);
463 /*! \brief Trims trailing whitespace characters from an ast_str string.
464 * \param buf A pointer to the ast_str string.
467 void ast_str_trim_blanks(struct ast_str *buf),
472 while (buf->__AST_STR_USED && buf->__AST_STR_STR[buf->__AST_STR_USED - 1] < 33) {
473 buf->__AST_STR_STR[--(buf->__AST_STR_USED)] = '\0';
478 /*!\brief Returns the current length of the string stored within buf.
479 * \param buf A pointer to the ast_str structure.
482 size_t attribute_pure ast_str_strlen(const struct ast_str *buf),
484 return buf->__AST_STR_USED;
488 /*!\brief Returns the current maximum length (without reallocation) of the current buffer.
489 * \param buf A pointer to the ast_str structure.
490 * \retval Current maximum length of the buffer.
493 size_t attribute_pure ast_str_size(const struct ast_str *buf),
495 return buf->__AST_STR_LEN;
499 /*!\brief Returns the string buffer within the ast_str buf.
500 * \param buf A pointer to the ast_str structure.
501 * \retval A pointer to the enclosed string.
504 char * attribute_pure ast_str_buffer(const struct ast_str *buf),
506 /* for now, cast away the const qualifier on the pointer
507 * being returned; eventually, it should become truly const
508 * and only be modified via accessor functions
510 return (char *) buf->__AST_STR_STR;
514 /*!\brief Truncates the enclosed string to the given length.
515 * \param buf A pointer to the ast_str structure.
516 * \param len Maximum length of the string. If len is larger than the
517 * current maximum length, things will explode. If it is negative
518 * at most -len characters will be trimmed off the end.
519 * \retval A pointer to the resulting string.
522 char *ast_str_truncate(struct ast_str *buf, ssize_t len),
525 if ((typeof(buf->__AST_STR_USED)) -len >= buf->__AST_STR_USED) {
526 buf->__AST_STR_USED = 0;
528 buf->__AST_STR_USED += len;
531 buf->__AST_STR_USED = len;
533 buf->__AST_STR_STR[buf->__AST_STR_USED] = '\0';
534 return buf->__AST_STR_STR;
539 * AST_INLINE_API() is a macro that takes a block of code as an argument.
540 * Using preprocessor #directives in the argument is not supported by all
541 * compilers, and it is a bit of an obfuscation anyways, so avoid it.
542 * As a workaround, define a macro that produces either its argument
543 * or nothing, and use that instead of #ifdef/#endif within the
544 * argument to AST_INLINE_API().
546 #if defined(DEBUG_THREADLOCALS)
553 * Make space in a new string (e.g. to read in data from a file)
555 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
557 int _ast_str_make_space(struct ast_str **buf, size_t new_len, const char *file, int lineno, const char *function),
559 struct ast_str *old_buf = *buf;
561 if (new_len <= (*buf)->__AST_STR_LEN)
562 return 0; /* success */
563 if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
564 return -1; /* cannot extend */
565 *buf = (struct ast_str *)__ast_realloc(*buf, new_len + sizeof(struct ast_str), file, lineno, function);
570 if ((*buf)->__AST_STR_TS != DS_MALLOC) {
571 pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
572 _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
575 (*buf)->__AST_STR_LEN = new_len;
579 #define ast_str_make_space(a,b) _ast_str_make_space(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
582 int ast_str_make_space(struct ast_str **buf, size_t new_len),
584 struct ast_str *old_buf = *buf;
586 if (new_len <= (*buf)->__AST_STR_LEN)
587 return 0; /* success */
588 if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
589 return -1; /* cannot extend */
590 *buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str));
595 if ((*buf)->__AST_STR_TS != DS_MALLOC) {
596 pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
597 _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
600 (*buf)->__AST_STR_LEN = new_len;
607 int ast_str_copy_string(struct ast_str **dst, struct ast_str *src),
610 /* make sure our destination is large enough */
611 if (src->__AST_STR_USED + 1 > (*dst)->__AST_STR_LEN) {
612 if (ast_str_make_space(dst, src->__AST_STR_USED + 1)) {
617 memcpy((*dst)->__AST_STR_STR, src->__AST_STR_STR, src->__AST_STR_USED + 1);
618 (*dst)->__AST_STR_USED = src->__AST_STR_USED;
623 #define ast_str_alloca(init_len) \
625 struct ast_str *__ast_str_buf; \
626 __ast_str_buf = alloca(sizeof(*__ast_str_buf) + init_len); \
627 __ast_str_buf->__AST_STR_LEN = init_len; \
628 __ast_str_buf->__AST_STR_USED = 0; \
629 __ast_str_buf->__AST_STR_TS = DS_ALLOCA; \
630 __ast_str_buf->__AST_STR_STR[0] = '\0'; \
635 * \brief Retrieve a thread locally stored dynamic string
637 * \param ts This is a pointer to the thread storage structure declared by using
638 * the AST_THREADSTORAGE macro. If declared with
639 * AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be
641 * \param init_len This is the initial length of the thread's dynamic string. The
642 * current length may be bigger if previous operations in this thread have
643 * caused it to increase.
645 * \return This function will return the thread locally stored dynamic string
646 * associated with the thread storage management variable passed as the
648 * The result will be NULL in the case of a memory allocation error.
652 * AST_THREADSTORAGE(my_str, my_str_init);
653 * #define MY_STR_INIT_SIZE 128
655 * void my_func(const char *fmt, ...)
657 * struct ast_str *buf;
659 * if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
665 #if !defined(DEBUG_THREADLOCALS)
667 struct ast_str *ast_str_thread_get(struct ast_threadstorage *ts,
672 buf = (struct ast_str *)ast_threadstorage_get(ts, sizeof(*buf) + init_len);
676 if (!buf->__AST_STR_LEN) {
677 buf->__AST_STR_LEN = init_len;
678 buf->__AST_STR_USED = 0;
679 buf->__AST_STR_TS = ts;
685 #else /* defined(DEBUG_THREADLOCALS) */
687 struct ast_str *__ast_str_thread_get(struct ast_threadstorage *ts,
688 size_t init_len, const char *file, const char *function, unsigned int line),
692 buf = (struct ast_str *)__ast_threadstorage_get(ts, sizeof(*buf) + init_len, file, function, line);
696 if (!buf->__AST_STR_LEN) {
697 buf->__AST_STR_LEN = init_len;
698 buf->__AST_STR_USED = 0;
699 buf->__AST_STR_TS = ts;
706 #define ast_str_thread_get(ts, init_len) __ast_str_thread_get(ts, init_len, __FILE__, __PRETTY_FUNCTION__, __LINE__)
707 #endif /* defined(DEBUG_THREADLOCALS) */
710 * \brief Error codes from __ast_str_helper()
711 * The undelying processing to manipulate dynamic string is done
712 * by __ast_str_helper(), which can return a success or a
713 * permanent failure (e.g. no memory).
716 /*! An error has occurred and the contents of the dynamic string
718 AST_DYNSTR_BUILD_FAILED = -1,
719 /*! The buffer size for the dynamic string had to be increased, and
720 * __ast_str_helper() needs to be called again after
721 * a va_end() and va_start(). This return value is legacy and will
724 AST_DYNSTR_BUILD_RETRY = -2
728 * \brief Core functionality of ast_str_(set|append)_va
730 * The arguments to this function are the same as those described for
731 * ast_str_set_va except for an addition argument, append.
732 * If append is non-zero, this will append to the current string instead of
735 * AST_DYNSTR_BUILD_RETRY is a legacy define. It should probably never
738 * A return of AST_DYNSTR_BUILD_FAILED indicates a memory allocation error.
740 * A return value greater than or equal to zero indicates the number of
741 * characters that have been written, not including the terminating '\0'.
742 * In the append case, this only includes the number of characters appended.
744 * \note This function should never need to be called directly. It should
745 * through calling one of the other functions or macros defined in this
748 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
749 int __attribute__((format(printf, 4, 0))) __ast_debug_str_helper(struct ast_str **buf, ssize_t max_len,
750 int append, const char *fmt, va_list ap, const char *file, int lineno, const char *func);
751 #define __ast_str_helper(a,b,c,d,e) __ast_debug_str_helper(a,b,c,d,e,__FILE__,__LINE__,__PRETTY_FUNCTION__)
753 int __attribute__((format(printf, 4, 0))) __ast_str_helper(struct ast_str **buf, ssize_t max_len,
754 int append, const char *fmt, va_list ap);
756 char *__ast_str_helper2(struct ast_str **buf, ssize_t max_len,
757 const char *src, size_t maxsrc, int append, int escapecommas);
760 * \brief Set a dynamic string from a va_list
762 * \param buf This is the address of a pointer to a struct ast_str.
763 * If it is retrieved using ast_str_thread_get, the
764 struct ast_threadstorage pointer will need to
765 * be updated in the case that the buffer has to be reallocated to
766 * accommodate a longer string than what it currently has space for.
767 * \param max_len This is the maximum length to allow the string buffer to grow
768 * to. If this is set to 0, then there is no maximum length.
769 * \param fmt This is the format string (printf style)
770 * \param ap This is the va_list
772 * \return The return value of this function is the same as that of the printf
773 * family of functions.
775 * Example usage (the first part is only for thread-local storage)
777 * AST_THREADSTORAGE(my_str, my_str_init);
778 * #define MY_STR_INIT_SIZE 128
780 * void my_func(const char *fmt, ...)
782 * struct ast_str *buf;
785 * if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
789 * ast_str_set_va(&buf, 0, fmt, ap);
792 * printf("This is the string we just built: %s\n", buf->str);
797 AST_INLINE_API(int __attribute__((format(printf, 3, 0))) ast_str_set_va(struct ast_str **buf, ssize_t max_len, const char *fmt, va_list ap),
799 return __ast_str_helper(buf, max_len, 0, fmt, ap);
804 * \brief Append to a dynamic string using a va_list
806 * Same as ast_str_set_va(), but append to the current content.
808 AST_INLINE_API(int __attribute__((format(printf, 3, 0))) ast_str_append_va(struct ast_str **buf, ssize_t max_len, const char *fmt, va_list ap),
810 return __ast_str_helper(buf, max_len, 1, fmt, ap);
814 /*!\brief Set a dynamic string to a non-NULL terminated substring. */
815 AST_INLINE_API(char *ast_str_set_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
817 return __ast_str_helper2(buf, maxlen, src, maxsrc, 0, 0);
821 /*!\brief Append a non-NULL terminated substring to the end of a dynamic string. */
822 AST_INLINE_API(char *ast_str_append_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
824 return __ast_str_helper2(buf, maxlen, src, maxsrc, 1, 0);
828 /*!\brief Set a dynamic string to a non-NULL terminated substring, with escaping of commas. */
829 AST_INLINE_API(char *ast_str_set_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
831 return __ast_str_helper2(buf, maxlen, src, maxsrc, 0, 1);
835 /*!\brief Append a non-NULL terminated substring to the end of a dynamic string, with escaping of commas. */
836 AST_INLINE_API(char *ast_str_append_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
838 return __ast_str_helper2(buf, maxlen, src, maxsrc, 1, 1);
843 * \brief Set a dynamic string using variable arguments
845 * \param buf This is the address of a pointer to a struct ast_str which should
846 * have been retrieved using ast_str_thread_get. It will need to
847 * be updated in the case that the buffer has to be reallocated to
848 * accomodate a longer string than what it currently has space for.
849 * \param max_len This is the maximum length to allow the string buffer to grow
850 * to. If this is set to 0, then there is no maximum length.
851 * If set to -1, we are bound to the current maximum length.
852 * \param fmt This is the format string (printf style)
854 * \return The return value of this function is the same as that of the printf
855 * family of functions.
857 * All the rest is the same as ast_str_set_va()
860 int __attribute__((format(printf, 3, 4))) ast_str_set(
861 struct ast_str **buf, ssize_t max_len, const char *fmt, ...),
867 res = ast_str_set_va(buf, max_len, fmt, ap);
875 * \brief Append to a thread local dynamic string
877 * The arguments, return values, and usage of this function are the same as
878 * ast_str_set(), but the new data is appended to the current value.
881 int __attribute__((format(printf, 3, 4))) ast_str_append(
882 struct ast_str **buf, ssize_t max_len, const char *fmt, ...),
888 res = ast_str_append_va(buf, max_len, fmt, ap);
896 * \brief Check if a string is only digits
898 * \retval 1 The string contains only digits
899 * \retval 0 The string contains non-digit characters
902 int ast_check_digits(const char *arg),
905 if (*arg < '0' || *arg > '9') {
915 * \brief Convert the tech portion of a device string to upper case
917 * \retval dev_str Returns the char* passed in for convenience
920 char *ast_tech_to_upper(char *dev_str),
923 if (!dev_str || !strchr(dev_str, '/')) {
927 for (pos = dev_str; *pos && *pos != '/'; pos++) {
928 *pos = toupper(*pos);
935 * \brief Compute a hash value on a string
937 * This famous hash algorithm was written by Dan Bernstein and is
940 * http://www.cse.yorku.ca/~oz/hash.html
942 static force_inline int attribute_pure ast_str_hash(const char *str)
947 hash = hash * 33 ^ *str++;
953 * \brief Compute a hash value on a string
955 * \param[in] str The string to add to the hash
956 * \param[in] hash The hash value to add to
959 * This version of the function is for when you need to compute a
960 * string hash of more than one string.
962 * This famous hash algorithm was written by Dan Bernstein and is
965 * \sa http://www.cse.yorku.ca/~oz/hash.html
967 static force_inline int ast_str_hash_add(const char *str, int hash)
970 hash = hash * 33 ^ *str++;
976 * \brief Compute a hash value on a case-insensitive string
978 * Uses the same hash algorithm as ast_str_hash, but converts
979 * all characters to lowercase prior to computing a hash. This
980 * allows for easy case-insensitive lookups in a hash table.
982 static force_inline int attribute_pure ast_str_case_hash(const char *str)
987 hash = hash * 33 ^ tolower(*str++);
993 #endif /* _ASTERISK_STRINGS_H */