Eina (u)strbuf: Added eina_(u)strbuf_manage_new.
authortasn <tasn@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Tue, 6 Sep 2011 10:58:53 +0000 (10:58 +0000)
committertasn <tasn@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Tue, 6 Sep 2011 10:58:53 +0000 (10:58 +0000)
This lets us take a string and use it as the string buffer instead of
copying and allocating.

git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@63226 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

ChangeLog
src/include/eina_strbuf.h
src/include/eina_ustrbuf.h
src/lib/eina_strbuf_common.c
src/lib/eina_strbuf_common.h
src/lib/eina_strbuf_template_c.x
src/tests/eina_test_strbuf.c

index c0063a4..1b00657 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
 2011-09-05  Cedric Bail
 
        * Add eina_inlist_sorted_state_insert and helper.
+
+2011-09-06  Tom Hacohen
+
+       * Strbuf + Ustrbuf: Added eina_(u)strbuf_manage_new. This lets us
+       take a string and use it as the string buffer instead of copying
+       and allocating.
index ac92f97..d4f6b34 100644 (file)
@@ -80,6 +80,25 @@ typedef struct _Eina_Strbuf Eina_Strbuf;
 EAPI Eina_Strbuf *eina_strbuf_new(void) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
 
 /**
+ * @brief Create a new string buffer using the passed string. The passed
+ * string is used directly as the buffer, it's somehow the opposite function of
+ * @ref eina_strbuf_string_steal . The passed string must be malloced.
+ *
+ * @param str the string to manage
+ * @return Newly allocated string buffer instance.
+ *
+ * This function creates a new string buffer. On error, @c NULL is
+ * returned and Eina error is set to #EINA_ERROR_OUT_OF_MEMORY. To
+ * free the resources, use eina_strbuf_free().
+ *
+ * @see eina_strbuf_free()
+ * @see eina_strbuf_append()
+ * @see eina_strbuf_string_get()
+ * @since 1.1.0
+ */
+EAPI Eina_Strbuf *eina_strbuf_manage_new(char *str) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
+
+/**
  * @brief Free a string buffer.
  *
  * @param buf The string buffer to free.
index 9abe845..f68cb7b 100644 (file)
@@ -49,6 +49,25 @@ typedef struct _Eina_Strbuf Eina_UStrbuf;
 EAPI Eina_UStrbuf *eina_ustrbuf_new(void) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
 
 /**
+ * @brief Create a new string buffer using the passed string. The passed
+ * string is used directly as the buffer, it's somehow the opposite function of
+ * @ref eina_ustrbuf_string_steal . The passed string must be malloced.
+ *
+ * @param str the string to manage
+ * @return Newly allocated string buffer instance.
+ *
+ * This function creates a new string buffer. On error, @c NULL is
+ * returned and Eina error is set to #EINA_ERROR_OUT_OF_MEMORY. To
+ * free the resources, use eina_strbuf_free().
+ *
+ * @see eina_ustrbuf_free()
+ * @see eina_ustrbuf_append()
+ * @see eina_ustrbuf_string_get()
+ * @since 1.1.0
+ */
+EAPI Eina_Strbuf *eina_ustrbuf_manage_new(Eina_Unicode *str) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
+
+/**
  * @brief Free a string buffer.
  *
  * @param buf The string buffer to free.
index ad61766..abdd57c 100644 (file)
@@ -102,6 +102,30 @@ _eina_strbuf_common_init(size_t csize, Eina_Strbuf *buf)
 /**
  * @internal
  *
+ * init the buffer without allocating the actual string (for managed)
+ * @param csize the character size
+ * @param buf the buffer to init
+ *
+ * @return #EINA_TRUE on success, #EINA_FALSE on failure.
+ */
+static Eina_Bool
+_eina_strbuf_common_manage_init(size_t csize __UNUSED__,
+                                 Eina_Strbuf *buf,
+                                 void *str,
+                                 size_t len)
+{
+   buf->len = len;
+   buf->size = len + 1;
+   buf->step = EINA_STRBUF_INIT_STEP;
+   buf->buf = str;
+
+   return EINA_TRUE;
+}
+
+
+/**
+ * @internal
+ *
  * resize the buffer
  * @param csize the character size
  * @param buf the buffer to resize
@@ -244,6 +268,46 @@ eina_strbuf_common_new(size_t csize)
 
 /**
  * @internal
+ * @brief Create a new string buffer managing str.
+ *
+ * @param csize the character size
+ * @param str the string to manage
+ * @param len the length of the string to manage
+ * @return Newly allocated string buffer instance.
+ *
+ * This function creates a new string buffer. On error, @c NULL is
+ * returned and Eina error is set to #EINA_ERROR_OUT_OF_MEMORY. To
+ * free the resources, use eina_strbuf_common_free().
+ *
+ * @see eina_strbuf_common_free()
+ * @see eina_strbuf_common_append()
+ * @see eina_strbuf_common_string_get()
+ * @since 1.1.0
+ */
+Eina_Strbuf *
+eina_strbuf_common_manage_new(size_t csize,
+                               void *str,
+                               size_t len)
+{
+   Eina_Strbuf *buf;
+
+   eina_error_set(0);
+   buf = malloc(sizeof(Eina_Strbuf));
+   if (EINA_UNLIKELY(!buf))
+     {
+        eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
+        return NULL;
+     }
+  if (EINA_UNLIKELY(!_eina_strbuf_common_manage_init(csize, buf, str, len)))
+    {
+        eina_strbuf_common_free(buf);
+        return NULL;
+     }
+   return buf;
+}
+
+/**
+ * @internal
  * @brief Free a string buffer.
  *
  * @param buf The string buffer to free.
index f729b89..06a1353 100644 (file)
@@ -37,6 +37,10 @@ Eina_Bool
 eina_strbuf_common_shutdown(void);
 Eina_Strbuf *
 eina_strbuf_common_new(size_t csize);
+Eina_Strbuf *
+eina_strbuf_common_manage_new(size_t csize,
+                               void *str,
+                               size_t len);
 void
 eina_strbuf_common_free(Eina_Strbuf *buf);
 void
index 123d4be..e8c3b61 100644 (file)
  *                                   API                                      *
  *============================================================================*/
 
+EAPI _STRBUF_STRUCT_NAME *
+_FUNC_EXPAND(manage_new)(_STRBUF_DATA_TYPE *str)
+{
+   _STRBUF_STRUCT_NAME *buf = eina_strbuf_common_manage_new(_STRBUF_CSIZE,
+        (void *) str, _STRBUF_STRLEN_FUNC(str));
+   EINA_MAGIC_SET(buf, _STRBUF_MAGIC);
+   return buf;
+}
+
 EAPI Eina_Bool
 _FUNC_EXPAND(append)(_STRBUF_STRUCT_NAME *buf, const _STRBUF_DATA_TYPE *str)
 {
index 87dfc56..2205e3e 100644 (file)
@@ -63,6 +63,45 @@ START_TEST(strbuf_simple)
 }
 END_TEST
 
+START_TEST(strbuf_manage_simple)
+{
+   Eina_Strbuf *buf;
+   char *txt;
+#define TEXT \
+   "This test should be so long that it is longer than the initial size of strbuf"
+
+   eina_init();
+
+   txt = strdup(TEXT);
+
+   buf = eina_strbuf_manage_new(txt);
+   fail_if(!buf);
+
+   fail_if(strcmp(eina_strbuf_string_get(buf), TEXT));
+   fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
+   eina_strbuf_append(buf, TEXT);
+   fail_if(strcmp(eina_strbuf_string_get(buf), TEXT TEXT));
+   fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
+   txt = eina_strbuf_string_steal(buf);
+   fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
+   fail_if(strcmp(txt, TEXT TEXT));
+   free(txt);
+   fail_if(eina_strbuf_length_get(buf) != 0);
+   fail_if(!strcmp(eina_strbuf_string_get(buf), TEXT TEXT));
+   eina_strbuf_append(buf, TEXT);
+   fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
+   eina_strbuf_reset(buf);
+   fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
+   fail_if(eina_strbuf_length_get(buf) != 0);
+   fail_if(!strcmp(eina_strbuf_string_get(buf), TEXT));
+
+   eina_strbuf_free(buf);
+
+   eina_shutdown();
+#undef TEXT
+}
+END_TEST
+
 START_TEST(strbuf_remove)
 {
    Eina_Strbuf *buf;
@@ -406,4 +445,5 @@ eina_test_strbuf(TCase *tc)
    tcase_add_test(tc, strbuf_realloc);
    tcase_add_test(tc, strbuf_append_realloc);
    tcase_add_test(tc, strbuf_prepend_realloc);
+   tcase_add_test(tc, strbuf_manage_simple);
 }