util: Add suffix check for a string
PATCH 1/2 util: Add suffix check for a string
--- ell/util.c | 27 +++++++++++++++++++++++++++ ell/util.h | 1 + 2 files changed, 28 insertions(+)
ell/util.c +27 -0
@@ -444,6 +444,33 @@ LIB_EXPORT bool l_str_has_prefix(const char *str, const char *prefix) }/** +* l_str_has_suffix: +* @str: A string to be examined +* @suffix: Suffix string +* +* Determines if the string given by @str ends with the specified @suffix. +* +* Returns: True if @str ends with the specified @suffix. False otherwise. +*/ +LIB_EXPORT bool l_str_has_suffix(const char *str, const char *suffix) +{ +int str_len_diff; + +if (unlikely (!str)) +return false; + +if (unlikely (!suffix)) +return false; + + str_len_diff =strlen (str) -strlen (suffix); + +if (str_len_diff <0 ) +return false; + +return !strcmp (&str[str_len_diff], suffix); +} + +/** * l_util_hexstring: * @buf: buffer pointer * @len: length of buffer
ell/util.h +1 -0
@@ -208,6 +208,7 @@ char **l_strsplit_set(const char *str, const char *separators);char *l_strjoinv (char **str_array,const char delim);bool l_str_has_prefix (const char *str,const char *prefix); +bool l_str_has_suffix (const char *str,const char *suffix);char *l_util_hexstring (const unsigned char *buf,size_t len);unsigned char *l_util_from_hexstring (const char *str,size_t *out_len);
Signature · 1 line
2.5.0
PATCH 2/2 unit: Add tests for the suffix check for a string
--- unit/test-util.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
unit/test-util.c +14 -0
@@ -62,6 +62,18 @@ static void test_from_hexstring(const void *test_data)assert (!bytes); } +static void test_has_suffix (const void *test_data) +{ +const char *str ="string" ; +const char *suffix ="ing" ; + +assert (l_str_has_suffix (str, suffix)); +assert (l_str_has_suffix (str, str)); +assert (!l_str_has_suffix (NULL , suffix)); +assert (!l_str_has_suffix (str,NULL )); +assert (!l_str_has_suffix (suffix, str)); +} +int main (int argc,char *argv[]) {l_test_init (&argc, &argv);
@@ -69,5 +81,7 @@ int main(int argc, char *argv[])l_test_add ("l_util_hexstring" , test_hexstring,NULL );l_test_add ("l_util_from_hexstring" , test_from_hexstring,NULL ); +l_test_add ("l_util_has_suffix" , test_has_suffix,NULL ); +return l_test_run (); }
Signature · 1 line
2.5.0
Tim,
Quoted 22 lines · Tim Kourt
---
ell/util.c| 27 +++++++++++++++++++++++++++ell/util.h| 1 +2 files changed, 28 insertions(+)ell/util.c +9
-0@@ -444,6 +444,33 @@ LIB_EXPORT bool l_str_has_prefix(const char *str, const char *prefix) }/** +* l_str_has_suffix: +* @str: A string to be examined +* @suffix: Suffix string +* +* Determines if the string given by @str ends with the specified @suffix. +* +* Returns: True if @str ends with the specified @suffix. False otherwise. +*/ +LIB_EXPORT bool l_str_has_suffix(const char *str, const char *suffix)
ell/util.c LIB_EXPORT bool l_str_has_prefix(const char *str, const char *prefix) · line 465
Quoted 10 lines
ell/util.c +10
-0+{ +int str_len_diff; + +if (unlikely (!str)) +return false; + +if (unlikely (!suffix)) +return false; + + str_len_diff =strlen (str) -strlen (suffix);
strlen returns size_t (unsigned), which is a 64-bit type on 64-bit platforms. Hopefully no one is throwing around 4+GB null-terminated strings, but you never know. You can make sure the suffix is shorter before calculating the difference, avoiding signed integers altogether.
ell/util.c LIB_EXPORT bool l_str_has_prefix(const char *str, const char *prefix) · line 471
Quoted 6 lines
ell/util.c +6
-0+ +if (str_len_diff <0 ) +return false; + +return !strcmp (&str[str_len_diff], suffix); +}
Regards,
Mat
Hi Tim,
Quoted 7 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + str_len_diff = strlen(str) - strlen(suffix);strlen returns
size_t(unsigned), which is a 64-bit type on 64-bit platforms. Hopefully no one is throwing around 4+GB null-terminated strings, but you never know. You can make sure the suffix is shorter before calculating the difference, avoiding signed integers altogether.
+1. Please fix/resubmit this one.
Regards,
-Denis