]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-11382: [core] add switch_pool_strip_whitespace function to strip whitespace using...
authorMike Jerris <mike@jerris.com>
Thu, 6 Sep 2018 17:49:46 +0000 (17:49 +0000)
committerMike Jerris <mike@jerris.com>
Thu, 6 Sep 2018 17:49:46 +0000 (17:49 +0000)
src/include/switch_utils.h
src/switch_utils.c

index 06ade13180ae788c43cd694fb0260acf92dcf540..10242525f18d36ac1beea6fd79f014e6cc868743 100644 (file)
@@ -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
index bd21e7ee75577f22d4b342b02e02e84c7501cd35..52ec8f6327029f285fe61995d0243d10b9da5c7d 100644 (file)
@@ -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;