eina: add eina_strbuf_trim/rtrim/ltrim()
authorbilliob <billiob@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Sat, 15 Oct 2011 12:56:27 +0000 (12:56 +0000)
committerbilliob <billiob@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Sat, 15 Oct 2011 12:56:27 +0000 (12:56 +0000)
git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@64098 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/include/eina_strbuf.h
src/lib/eina_strbuf.c

index f356ffe..7043575 100644 (file)
@@ -564,6 +564,33 @@ EAPI Eina_Bool eina_strbuf_replace(Eina_Strbuf *buf, const char *str, const char
 EAPI int eina_strbuf_replace_all(Eina_Strbuf *buf, const char *str, const char *with) EINA_ARG_NONNULL(1, 2, 3);
 
 /**
+ * @brief Trim the string buffer
+
+ * @param buf the string buffer to work with.
+ *
+ * This function skips whitespaces in the beginning and the end of the buffer.
+ */
+EAPI void eina_strbuf_trim(Eina_Strbuf *buf) EINA_ARG_NONNULL(1);
+
+/**
+ * @brief Left trim the string buffer
+
+ * @param buf the string buffer to work with.
+ *
+ * This function skips whitespaces in the beginning of the buffer.
+ */
+EAPI void eina_strbuf_ltrim(Eina_Strbuf *buf) EINA_ARG_NONNULL(1);
+
+/**
+ * @brief Right trim the string buffer
+
+ * @param buf the string buffer to work with.
+ *
+ * This function skips whitespaces in the end of the buffer.
+ */
+EAPI void eina_strbuf_rtrim(Eina_Strbuf *buf) EINA_ARG_NONNULL(1);
+
+/**
  * @}
  */
 
index bc968ea..74b1eb9 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <ctype.h>
 
 #ifdef HAVE_EVIL
 # include <Evil.h>
@@ -158,6 +159,44 @@ eina_strbuf_insert_vprintf(Eina_Strbuf *buf,
    return ret;
 }
 
+EAPI void
+eina_strbuf_trim(Eina_Strbuf *buf)
+{
+   char *c = buf->buf;
+
+   while (buf->len > 0 && isspace(((unsigned char*)(buf->buf))[buf->len - 1]))
+     buf->len--;
+   while (buf->len > 0 && isspace(*c))
+     {
+        c++;
+        buf->len--;
+     }
+   memmove(buf->buf, c, buf->len);
+   ((unsigned char *)buf->buf)[buf->len] = '\0';
+}
+
+EAPI void
+eina_strbuf_ltrim(Eina_Strbuf *buf)
+{
+   char *c = buf->buf;
+
+   while (buf->len > 0 && isspace(*c))
+     {
+        c++;
+        buf->len--;
+     }
+   memmove(buf->buf, c, buf->len);
+   ((unsigned char *)buf->buf)[buf->len] = '\0';
+}
+
+EAPI void
+eina_strbuf_rtrim(Eina_Strbuf *buf)
+{
+   while (buf->len > 0 && isspace(((unsigned char*)(buf->buf))[buf->len - 1]))
+     buf->len--;
+   ((unsigned char *)buf->buf)[buf->len] = '\0';
+}
+
 /* Unicode */
 
 #include "eina_strbuf_template_c.x"