diff --git a/src/include/switch_utils.h b/src/include/switch_utils.h index 06ade13180..10242525f1 100644 --- a/src/include/switch_utils.h +++ b/src/include/switch_utils.h @@ -992,6 +992,7 @@ SWITCH_DECLARE(const char *) switch_stristr(const char *instr, const char *str); SWITCH_DECLARE(switch_bool_t) switch_is_lan_addr(const char *ip); SWITCH_DECLARE(char *) switch_replace_char(char *str, char from, char to, switch_bool_t dup); SWITCH_DECLARE(switch_bool_t) switch_ast2regex(const char *pat, char *rbuf, size_t len); +SWITCH_DECLARE(char *) switch_pool_strip_whitespace(switch_memory_pool_t *pool, const char *str); /*! \brief Escape a string by prefixing a list of characters with an escape character diff --git a/src/switch_utils.c b/src/switch_utils.c index bd21e7ee75..52ec8f6327 100644 --- a/src/switch_utils.c +++ b/src/switch_utils.c @@ -1328,6 +1328,38 @@ SWITCH_DECLARE(char *) switch_replace_char(char *str, char from, char to, switch return p; } +SWITCH_DECLARE(char *) switch_pool_strip_whitespace(switch_memory_pool_t *pool, const char *str) +{ + const char *sp = str; + char *p, *s = NULL; + size_t len; + + if (zstr(sp)) { + return switch_core_strdup(pool, SWITCH_BLANK_STRING); + } + + while ((*sp == 13 ) || (*sp == 10 ) || (*sp == 9 ) || (*sp == 32) || (*sp == 11) ) { + sp++; + } + + if (zstr(sp)) { + return switch_core_strdup(pool, SWITCH_BLANK_STRING); + } + + s = switch_core_strdup(pool, sp); + switch_assert(s); + + if ((len = strlen(s)) > 0) { + p = s + (len - 1); + + while ((p >= s) && ((*p == 13 ) || (*p == 10 ) || (*p == 9 ) || (*p == 32) || (*p == 11))) { + *p-- = '\0'; + } + } + + return s; +} + SWITCH_DECLARE(char *) switch_strip_whitespace(const char *str) { const char *sp = str;