Summary:
For a function which just composes a string with strbuf its quite
usefull to return the string while its freed.
This makes a function like:
{
Eina_Strbuf *buf;
char *path;
buf = eina_strbuf_new();
eina_strbuf_append(buf, "test");
eina_strbuf_append_printf(buf, "%s-%d.edj", "test", 0);
path = eina_strbuf_string_steal(buf);
eina_strbuf_free(buf);
return path;
}
To:
{
Eina_Strbuf *buf;
buf = eina_strbuf_new();
eina_strbuf_append(buf, "test");
eina_strbuf_append_printf(buf, "%s-%d.edj", "test", 0);
return eina_strbuf_free_return(buf);
}
Which is a bit more handy.
Test Plan: just run make check
Reviewers: raster, cedric
Subscribers: jpeg
Differential Revision: https://phab.enlightenment.org/D4545
return eina_strbuf_manage_new(str);
}
+EAPI char*
+eina_strbuf_free_return(Eina_Strbuf *buf)
+{
+ char *result;
+
+ result = eina_strbuf_string_steal(buf);
+ eina_strbuf_free(buf);
+
+ return result;
+}
+
/* Unicode */
#include "eina_strbuf_template_c.x"
*/
EAPI Eina_Rw_Slice eina_strbuf_rw_slice_get(const Eina_Strbuf *buf) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
+
+/**
+ * @brief Get the string of the buffer and free the buffer
+ *
+ * @param buf the buffer to get the string from and which will be freed
+ *
+ * @return The string contained by bug. The caller must release the memory of the returned string by calling
+ * free().
+ *
+ * @since 1.19
+ */
+EAPI char* eina_strbuf_free_return(Eina_Strbuf *buf) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
+
/**
* @}
*/
}
END_TEST
+START_TEST(strbuf_free_return_test)
+{
+ Eina_Strbuf *buf;
+ char *string;
+
+ buf = eina_strbuf_new();
+ ck_assert_ptr_ne(buf, NULL);
+ eina_strbuf_append(buf, "strbuf_free_return_test");
+
+ string = eina_strbuf_free_return(buf);
+ ck_assert_str_eq(string, "strbuf_free_return_test");
+}
+END_TEST
+
void
eina_test_strbuf(TCase *tc)
{
tcase_add_test(tc, strbuf_tolower);
tcase_add_test(tc, strbuf_substr_get);
tcase_add_test(tc, strbuf_prepend_print);
+ tcase_add_test(tc, strbuf_free_return_test);
}