From 2f1d0fb18924b5c100dd2a5b149b4af505c87566 Mon Sep 17 00:00:00 2001 From: Marcel Hollerbach Date: Tue, 3 Jan 2017 10:52:27 +0100 Subject: [PATCH] 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 --- src/lib/eina/eina_strbuf.c | 11 +++++++++++ src/lib/eina/eina_strbuf.h | 13 +++++++++++++ src/tests/eina/eina_test_strbuf.c | 15 +++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/lib/eina/eina_strbuf.c b/src/lib/eina/eina_strbuf.c index 81ab30a45f..b784a701ab 100644 --- a/src/lib/eina/eina_strbuf.c +++ b/src/lib/eina/eina_strbuf.c @@ -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" diff --git a/src/lib/eina/eina_strbuf.h b/src/lib/eina/eina_strbuf.h index 94d16f8c3a..9c2506c816 100644 --- a/src/lib/eina/eina_strbuf.h +++ b/src/lib/eina/eina_strbuf.h @@ -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); + /** * @} */ diff --git a/src/tests/eina/eina_test_strbuf.c b/src/tests/eina/eina_test_strbuf.c index 297177d7cc..8206720ed3 100644 --- a/src/tests/eina/eina_test_strbuf.c +++ b/src/tests/eina/eina_test_strbuf.c @@ -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); }