Skip to contentSkip to conversation

util: Add suffix check for a string

PATCH Superseded Tim Kourt 3 participants 4 messages Apr 22, 2016 – Apr 25, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tim Kourt

author
#0 lore raw

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
index f920895..895f95f 100644
@@ -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
index f6a88a0..92e7c8a 100644
@@ -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
author
#1 lore raw

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
index d72646f..427c74a 100644
@@ -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
#2 lore raw
In reply to Tim Kourt

Tim,

Quoted 22 lines · Tim Kourt

---
ell/util.c | 27 +++++++++++++++++++++++++++
ell/util.h | 1 +

2 files changed, 28 insertions(+)
ell/util.c +9 -0
index f920895..895f95f 100644
@@ -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

#3 lore raw

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