summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Hacohen <tom@stosb.com>2015-02-09 13:43:40 +0000
committerTom Hacohen <tom@stosb.com>2015-02-09 13:55:13 +0000
commitf7a6880d5b0f25c8004d2bb3ba6385cc0d8869c5 (patch)
treeb5ce7715d61385f40283174fc3e62ce7198a9ecd
parentf9548fa7da37be05f316d2d68fdcfb4826d4ab42 (diff)
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.
-rw-r--r--src/lib/eina/eina_tmpstr.c18
-rw-r--r--src/lib/eina/eina_tmpstr.h19
-rw-r--r--src/tests/eina/eina_test_tmpstr.c23
3 files changed, 50 insertions, 10 deletions
diff --git a/src/lib/eina/eina_tmpstr.c b/src/lib/eina/eina_tmpstr.c
index 57f3c2bd09..7181b75c65 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)
72 Str *s; 72 Str *s;
73 73
74 if (!str || !length) return NULL; 74 if (!str || !length) return NULL;
75 /* eina_tmpstr_strlen is expected to return strlen + 1 */ 75 s = malloc(sizeof(Str) + length + 1);
76 length += 1;
77 s = malloc(sizeof(Str) + length);
78 if (!s) return NULL; 76 if (!s) return NULL;
79 s->length = length; 77 s->length = length;
80 s->str = ((char *)s) + sizeof(Str); 78 s->str = ((char *)s) + sizeof(Str);
81 strncpy(s->str, str, length - 1); 79 strncpy(s->str, str, length);
82 s->str[length - 1] = '\0'; 80 s->str[length] = '\0';
83 eina_lock_take(&_mutex); 81 eina_lock_take(&_mutex);
84 s->next = strs; 82 s->next = strs;
85 strs = s; 83 strs = s;
@@ -120,10 +118,16 @@ eina_tmpstr_del(Eina_Tmpstr *tmpstr)
120EAPI size_t 118EAPI size_t
121eina_tmpstr_strlen(Eina_Tmpstr *tmpstr) 119eina_tmpstr_strlen(Eina_Tmpstr *tmpstr)
122{ 120{
121 return (tmpstr) ? eina_tmpstr_len(tmpstr) : 0;
122}
123
124EAPI size_t
125eina_tmpstr_len(Eina_Tmpstr *tmpstr)
126{
123 Str *s; 127 Str *s;
124 128
125 if (!tmpstr) return 0; 129 if (!tmpstr) return 0;
126 if (!strs) return strlen(tmpstr) + 1; 130 if (!strs) return strlen(tmpstr);
127 eina_lock_take(&_mutex); 131 eina_lock_take(&_mutex);
128 for (s = strs; s; s = s->next) 132 for (s = strs; s; s = s->next)
129 { 133 {
@@ -135,5 +139,5 @@ eina_tmpstr_strlen(Eina_Tmpstr *tmpstr)
135 } 139 }
136 eina_lock_release(&_mutex); 140 eina_lock_release(&_mutex);
137 141
138 return strlen(tmpstr) + 1; 142 return strlen(tmpstr);
139} 143}
diff --git a/src/lib/eina/eina_tmpstr.h b/src/lib/eina/eina_tmpstr.h
index 7460356930..085041b810 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;
187 * 187 *
188 * @note If the length is greater than the actual string, but still '\0' 188 * @note If the length is greater than the actual string, but still '\0'
189 * terminateed. Their won't be any crash and the string will be correct, 189 * terminateed. Their won't be any crash and the string will be correct,
190 * but eina_tmpstr_strlen will return an erroneous length. So if you 190 * but eina_tmpstr_len will return an erroneous length. So if you
191 * want to have the correct length always call eina_tmpstr_add_length 191 * want to have the correct length always call eina_tmpstr_add_length
192 * with length == strlen(str). 192 * with length == strlen(str).
193 * @see eina_tmpstr_del() 193 * @see eina_tmpstr_del()
@@ -198,15 +198,28 @@ EAPI Eina_Tmpstr *eina_tmpstr_add(const char *str) EINA_WARN_UNUSED_RESULT;
198EAPI Eina_Tmpstr *eina_tmpstr_add_length(const char *str, size_t length); 198EAPI Eina_Tmpstr *eina_tmpstr_add_length(const char *str, size_t length);
199 199
200/** 200/**
201 * @brief Return the length of a temporary string including the '\0'. 201 * @brief **Deprecated** Return the length of a temporary string including the '\0'.
202 * 202 *
203 * @param tmpstr This is any C string pointer, but if it is a tmp string 203 * @param tmpstr This is any C string pointer, but if it is a tmp string
204 * it will return the length faster. 204 * it will return the length faster.
205 * @return The length of the string including the '\0'; 205 * @return The length of the string including the '\0';
206 * 206 *
207 * @deprecated
208 * @see eina_tmpstr_len()
207 * @since 1.8.0 209 * @since 1.8.0
208 */ 210 */
209EAPI size_t eina_tmpstr_strlen(Eina_Tmpstr *tmpstr); 211EINA_DEPRECATED EAPI size_t eina_tmpstr_strlen(Eina_Tmpstr *tmpstr);
212
213/**
214 * @brief Return the length of a temporary string.
215 *
216 * @param tmpstr This is any C string pointer, but if it is a tmp string
217 * it will return the length faster.
218 * @return The length of the string.
219 *
220 * @since 1.14.0
221 */
222EAPI size_t eina_tmpstr_len(Eina_Tmpstr *tmpstr);
210 223
211/** 224/**
212 * @brief Delete the temporary string if it is one, or ignore it if it is not. 225 * @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 ba3d3e703d..72fb7c840b 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)
60} 60}
61END_TEST 61END_TEST
62 62
63START_TEST(tmpstr_len)
64{
65 eina_init();
66
67 const char *str1 = "12345";
68 const char *str2 = "123456789";
69 Eina_Tmpstr *tstr1 = eina_tmpstr_add(str1);
70 Eina_Tmpstr *tstr2 = eina_tmpstr_add(str2);
71 Eina_Tmpstr *tstr_empty = eina_tmpstr_add("");
72
73 ck_assert_int_eq(eina_tmpstr_len(tstr1), strlen(str1));
74 ck_assert_int_eq(eina_tmpstr_len(tstr2), strlen(str2));
75
76 ck_assert_int_eq(eina_tmpstr_len(tstr_empty), 0);
77
78 eina_tmpstr_del(tstr1);
79 eina_tmpstr_del(tstr2);
80
81 eina_shutdown();
82}
83END_TEST
84
63void 85void
64eina_test_tmpstr(TCase *tc) 86eina_test_tmpstr(TCase *tc)
65{ 87{
66 tcase_add_test(tc, tmpstr_simple); 88 tcase_add_test(tc, tmpstr_simple);
89 tcase_add_test(tc, tmpstr_len);
67} 90}