tasn pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=f7a6880d5b0f25c8004d2bb3ba6385cc0d8869c5

commit f7a6880d5b0f25c8004d2bb3ba6385cc0d8869c5
Author: Tom Hacohen <t...@stosb.com>
Date:   Mon Feb 9 13:43:40 2015 +0000

    Eina tmpstr: deprecate tmpstr_strlen and add tmpstr_len.
    
    Confusingly enough, strlen was actually returning strlen + 1 for
    strings. The new functions behaves as expected.
---
 src/lib/eina/eina_tmpstr.c        | 18 +++++++++++-------
 src/lib/eina/eina_tmpstr.h        | 19 ++++++++++++++++---
 src/tests/eina/eina_test_tmpstr.c | 23 +++++++++++++++++++++++
 3 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/src/lib/eina/eina_tmpstr.c b/src/lib/eina/eina_tmpstr.c
index 57f3c2b..7181b75 100644
--- a/src/lib/eina/eina_tmpstr.c
+++ b/src/lib/eina/eina_tmpstr.c
@@ -72,14 +72,12 @@ eina_tmpstr_add_length(const char *str, size_t length)
    Str *s;
 
    if (!str || !length) return NULL;
-   /* eina_tmpstr_strlen is expected to return strlen + 1 */
-   length += 1;
-   s = malloc(sizeof(Str) + length);
+   s = malloc(sizeof(Str) + length + 1);
    if (!s) return NULL;
    s->length = length;
    s->str = ((char *)s) + sizeof(Str);
-   strncpy(s->str, str, length - 1);
-   s->str[length - 1] = '\0';
+   strncpy(s->str, str, length);
+   s->str[length] = '\0';
    eina_lock_take(&_mutex);
    s->next = strs;
    strs = s;
@@ -120,10 +118,16 @@ eina_tmpstr_del(Eina_Tmpstr *tmpstr)
 EAPI size_t
 eina_tmpstr_strlen(Eina_Tmpstr *tmpstr)
 {
+   return (tmpstr) ? eina_tmpstr_len(tmpstr) : 0;
+}
+
+EAPI size_t
+eina_tmpstr_len(Eina_Tmpstr *tmpstr)
+{
    Str *s;
 
    if (!tmpstr) return 0;
-   if (!strs) return strlen(tmpstr) + 1;
+   if (!strs) return strlen(tmpstr);
    eina_lock_take(&_mutex);
    for (s = strs; s; s = s->next)
      {
@@ -135,5 +139,5 @@ eina_tmpstr_strlen(Eina_Tmpstr *tmpstr)
      }
    eina_lock_release(&_mutex);
 
-   return strlen(tmpstr) + 1;
+   return strlen(tmpstr);
 }
diff --git a/src/lib/eina/eina_tmpstr.h b/src/lib/eina/eina_tmpstr.h
index 7460356..085041b 100644
--- a/src/lib/eina/eina_tmpstr.h
+++ b/src/lib/eina/eina_tmpstr.h
@@ -187,7 +187,7 @@ EAPI Eina_Tmpstr *eina_tmpstr_add(const char *str) 
EINA_WARN_UNUSED_RESULT;
  *
  * @note If the length is greater than the actual string, but still '\0'
  *       terminateed. Their won't be any crash and the string will be correct,
- *       but eina_tmpstr_strlen will return an erroneous length. So if you
+ *       but eina_tmpstr_len will return an erroneous length. So if you
  *       want to have the correct length always call eina_tmpstr_add_length
  *       with length == strlen(str).
  * @see eina_tmpstr_del()
@@ -198,15 +198,28 @@ EAPI Eina_Tmpstr *eina_tmpstr_add(const char *str) 
EINA_WARN_UNUSED_RESULT;
 EAPI Eina_Tmpstr *eina_tmpstr_add_length(const char *str, size_t length);
 
 /**
- * @brief Return the length of a temporary string including the '\0'.
+ * @brief **Deprecated** Return the length of a temporary string including the 
'\0'.
  *
  * @param tmpstr This is any C string pointer, but if it is a tmp string
  * it will return the length faster.
  * @return The length of the string including the '\0';
  *
+ * @deprecated
+ * @see eina_tmpstr_len()
  * @since 1.8.0
  */
-EAPI size_t eina_tmpstr_strlen(Eina_Tmpstr *tmpstr);
+EINA_DEPRECATED EAPI size_t eina_tmpstr_strlen(Eina_Tmpstr *tmpstr);
+
+/**
+ * @brief Return the length of a temporary string.
+ *
+ * @param tmpstr This is any C string pointer, but if it is a tmp string
+ * it will return the length faster.
+ * @return The length of the string.
+ *
+ * @since 1.14.0
+ */
+EAPI size_t eina_tmpstr_len(Eina_Tmpstr *tmpstr);
 
 /**
  * @brief Delete the temporary string if it is one, or ignore it if it is not.
diff --git a/src/tests/eina/eina_test_tmpstr.c 
b/src/tests/eina/eina_test_tmpstr.c
index ba3d3e7..72fb7c8 100644
--- a/src/tests/eina/eina_test_tmpstr.c
+++ b/src/tests/eina/eina_test_tmpstr.c
@@ -60,8 +60,31 @@ START_TEST(tmpstr_simple)
 }
 END_TEST
 
+START_TEST(tmpstr_len)
+{
+   eina_init();
+
+   const char *str1 = "12345";
+   const char *str2 = "123456789";
+   Eina_Tmpstr *tstr1 = eina_tmpstr_add(str1);
+   Eina_Tmpstr *tstr2 = eina_tmpstr_add(str2);
+   Eina_Tmpstr *tstr_empty = eina_tmpstr_add("");
+
+   ck_assert_int_eq(eina_tmpstr_len(tstr1), strlen(str1));
+   ck_assert_int_eq(eina_tmpstr_len(tstr2), strlen(str2));
+
+   ck_assert_int_eq(eina_tmpstr_len(tstr_empty), 0);
+
+   eina_tmpstr_del(tstr1);
+   eina_tmpstr_del(tstr2);
+
+   eina_shutdown();
+}
+END_TEST
+
 void
 eina_test_tmpstr(TCase *tc)
 {
    tcase_add_test(tc, tmpstr_simple);
+   tcase_add_test(tc, tmpstr_len);
 }

-- 


Reply via email to