eina_strbuf: add eina_strbuf_free_return

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
This commit is contained in:
Marcel Hollerbach 2017-01-03 10:52:27 +01:00
parent e998529f2e
commit 2f1d0fb189
3 changed files with 39 additions and 0 deletions

View File

@ -220,6 +220,17 @@ eina_strbuf_substr_get(Eina_Strbuf *buf, size_t pos, size_t len)
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"

View File

@ -718,6 +718,19 @@ EAPI Eina_Slice eina_strbuf_slice_get(const Eina_Strbuf *buf) EINA_WARN_UNUSED_R
*/
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);
/**
* @}
*/

View File

@ -646,6 +646,20 @@ START_TEST(strbuf_prepend_print)
}
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)
{
@ -662,4 +676,5 @@ 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);
}