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.
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.
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.
/**
* @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
/**
* @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.
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
* 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)
{
}
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;
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);
}