add eina_strbuf_append_length()
authorbarbieri <barbieri>
Fri, 26 Feb 2010 20:09:36 +0000 (20:09 +0000)
committerbarbieri <barbieri@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 26 Feb 2010 20:09:36 +0000 (20:09 +0000)
git-svn-id: http://svn.enlightenment.org/svn/e/trunk/eina@46516 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

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

index bf2664d..6a1f2ba 100644 (file)
@@ -13,6 +13,7 @@ EAPI void eina_strbuf_reset(Eina_Strbuf *buf) EINA_ARG_NONNULL(1);
 EAPI Eina_Bool eina_strbuf_append(Eina_Strbuf *buf, const char *str) EINA_ARG_NONNULL(1, 2);
 EAPI Eina_Bool eina_strbuf_append_escaped(Eina_Strbuf *buf, const char *str) EINA_ARG_NONNULL(1, 2);
 EAPI Eina_Bool eina_strbuf_append_n(Eina_Strbuf *buf, const char *str, unsigned int maxlen) EINA_ARG_NONNULL(1, 2);
+EAPI Eina_Bool eina_strbuf_append_length(Eina_Strbuf *buf, const char *str, unsigned int length) EINA_ARG_NONNULL(1, 2);
 EAPI Eina_Bool eina_strbuf_append_char(Eina_Strbuf *buf, char c) EINA_ARG_NONNULL(1);
 EAPI Eina_Bool eina_strbuf_insert(Eina_Strbuf *buf, const char *str,
                                 size_t pos) EINA_ARG_NONNULL(1, 2);
index b547c3a..f74936b 100644 (file)
@@ -117,8 +117,16 @@ eina_strbuf_reset(Eina_Strbuf *buf)
 
 /**
  * Append a string to a buffer, reallocating as necessary.
+ *
+ * This is slightly slower than eina_strbuf_append_length(), as it
+ * needs to strlen() the given pointer. If you know the size
+ * beforehand, consider using that variant.
+ *
  * @param buf the Eina_Strbuf to append to
  * @param str the string to append
+ *
+ * @see eina_strbuf_append()
+ * @see eina_strbuf_append_length()
  */
 EAPI Eina_Bool
 eina_strbuf_append(Eina_Strbuf *buf, const char *str)
@@ -167,6 +175,9 @@ eina_strbuf_append_escaped(Eina_Strbuf *buf, const char *str)
  * @param buf the Eina_Strbuf to append to
  * @param str the string to append
  * @param maxlen maximum number of chars to append
+ *
+ * @see eina_strbuf_append()
+ * @see eina_strbuf_append_length()
  */
 EAPI Eina_Bool
 eina_strbuf_append_n(Eina_Strbuf *buf, const char *str, unsigned int maxlen)
@@ -188,6 +199,36 @@ eina_strbuf_append_n(Eina_Strbuf *buf, const char *str, unsigned int maxlen)
 }
 
 /**
+ * Append a string of exact length to a buffer, reallocating as necessary.
+ *
+ * This is a slightly faster version that does not need to strlen()
+ * the whole string to know its size. Useful when dealing with strings
+ * of known size, such as eina_stringshare, see
+ * eina_stringshare_length().
+ *
+ * @param buf the Eina_Strbuf to append to
+ * @param str the string to append
+ * @param length the exact length to use.
+ *
+ * @see eina_strbuf_append()
+ * @see eina_strbuf_append_n()
+ */
+EAPI Eina_Bool
+eina_strbuf_append_length(Eina_Strbuf *buf, const char *str, unsigned int length)
+{
+   EINA_SAFETY_ON_NULL_RETURN_VAL(str, EINA_FALSE);
+   EINA_MAGIC_CHECK_STRBUF(buf, EINA_FALSE);
+
+   if (EINA_UNLIKELY(!_eina_strbuf_grow(buf, buf->len + length)))
+     return EINA_FALSE;
+
+   memcpy(buf->buf + buf->len, str, length);
+   buf->len += length;
+   buf->buf[buf->len] = '\0';
+   return EINA_TRUE;
+}
+
+/**
  * Insert a string to a buffer, reallocating as necessary.
  * @param buf the Eina_Strbuf to insert
  * @param str the string to insert
index 1b145e7..06d2cf3 100644 (file)
@@ -139,6 +139,16 @@ START_TEST(strbuf_append)
    fail_if(strcmp(eina_strbuf_string_get(buf), "a"));
    eina_strbuf_reset(buf);
 
+   eina_strbuf_append_length(buf, "something", strlen("something"));
+   fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
+   fail_if(strcmp(eina_strbuf_string_get(buf), "something"));
+   eina_strbuf_reset(buf);
+
+   eina_strbuf_append_length(buf, "somethingELSE", strlen("something"));
+   fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
+   fail_if(strcmp(eina_strbuf_string_get(buf), "something"));
+   eina_strbuf_reset(buf);
+
    eina_strbuf_free(buf);
 
    eina_shutdown();