From 3e7f96d9429aa66c05fcbb19100f66f83aa4f6b3 Mon Sep 17 00:00:00 2001 From: Cedric BAIL Date: Wed, 29 May 2019 13:49:26 -0400 Subject: [PATCH] eina: add eina_slstr_copy_new_length. Reviewers: SanghyeonLee, bu5hm4n, zmike, segfaultxavi, lauromoura, felipealmeida, raster Reviewed By: bu5hm4n, zmike Subscribers: #reviewers, #committers Tags: #efl Maniphest Tasks: T7832 Differential Revision: https://phab.enlightenment.org/D8800 --- src/lib/eina/eina_slstr.c | 17 +++++++++++++++++ src/lib/eina/eina_slstr.h | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/lib/eina/eina_slstr.c b/src/lib/eina/eina_slstr.c index 143965a..017b692 100644 --- a/src/lib/eina/eina_slstr.c +++ b/src/lib/eina/eina_slstr.c @@ -109,6 +109,23 @@ eina_slstr_copy_new(const char *string) } EAPI Eina_Slstr * +eina_slstr_copy_new_length(const char *string, size_t len) +{ + Eina_FreeQ *fq; + char *copy; + + if (!string) return NULL; + + fq = _slstr_freeq_get(EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(fq, NULL); + + copy = eina_strndup(string, len); + if (!copy) return NULL; + eina_freeq_ptr_add(fq, copy, free, len); + return copy; +} + +EAPI Eina_Slstr * eina_slstr_steal_new(char *string) { Eina_FreeQ *fq; diff --git a/src/lib/eina/eina_slstr.h b/src/lib/eina/eina_slstr.h index ce93e23..7f6144f 100644 --- a/src/lib/eina/eina_slstr.h +++ b/src/lib/eina/eina_slstr.h @@ -52,6 +52,25 @@ EAPI Eina_Slstr * eina_slstr_copy_new(const char *string); /** + * @brief Create a new short lived string by duplicating another string. + * + * @param[in] string An existing string, it will be copied. + * @param[in] len How many charactere max to copy. + * @return A new Eina_Slstr or NULL if out of memory. + * + * Usage example: + * @code + * char local[200]; + * sprintf(local, "Hello %d", value); + * return eina_slstr_copy_new_length(local, 5); + * @endcode + * + * @since 1.19 + */ +EAPI Eina_Slstr * +eina_slstr_copy_new_length(const char *string, size_t len); + +/** * @brief Create a new short lived string by taking ownership of a string. * * @param[in] string An existing string. It will not be duplicated. -- 2.7.4