forked from enlightenment/efl
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.
This commit is contained in:
parent
f9548fa7da
commit
f7a6880d5b
|
@ -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;
|
||||
|
@ -119,11 +117,17 @@ 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);
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue