* [PATCH 1/2] util: Add suffix check for a string
@ 2016-04-22 22:06 Tim Kourt
2016-04-22 22:06 ` [PATCH 2/2] unit: Add tests for the " Tim Kourt
2016-04-25 16:16 ` [PATCH 1/2] util: Add " Mat Martineau
0 siblings, 2 replies; 4+ messages in thread
From: Tim Kourt @ 2016-04-22 22:06 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 1529 bytes --]
---
ell/util.c | 27 +++++++++++++++++++++++++++
ell/util.h | 1 +
2 files changed, 28 insertions(+)
diff --git a/ell/util.c b/ell/util.c
index f920895..895f95f 100644
--- a/ell/util.c
+++ b/ell/util.c
@@ -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
diff --git a/ell/util.h b/ell/util.h
index f6a88a0..92e7c8a 100644
--- a/ell/util.h
+++ b/ell/util.h
@@ -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);
--
2.5.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] unit: Add tests for the suffix check for a string
2016-04-22 22:06 [PATCH 1/2] util: Add suffix check for a string Tim Kourt
@ 2016-04-22 22:06 ` Tim Kourt
2016-04-25 16:16 ` [PATCH 1/2] util: Add " Mat Martineau
1 sibling, 0 replies; 4+ messages in thread
From: Tim Kourt @ 2016-04-22 22:06 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 1001 bytes --]
---
unit/test-util.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/unit/test-util.c b/unit/test-util.c
index d72646f..427c74a 100644
--- a/unit/test-util.c
+++ b/unit/test-util.c
@@ -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();
}
--
2.5.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] util: Add suffix check for a string
2016-04-22 22:06 [PATCH 1/2] util: Add suffix check for a string Tim Kourt
2016-04-22 22:06 ` [PATCH 2/2] unit: Add tests for the " Tim Kourt
@ 2016-04-25 16:16 ` Mat Martineau
2016-04-25 17:36 ` Denis Kenzior
1 sibling, 1 reply; 4+ messages in thread
From: Mat Martineau @ 2016-04-25 16:16 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 1342 bytes --]
Tim,
On Fri, 22 Apr 2016, Tim Kourt wrote:
> ---
> ell/util.c | 27 +++++++++++++++++++++++++++
> ell/util.h | 1 +
> 2 files changed, 28 insertions(+)
>
> diff --git a/ell/util.c b/ell/util.c
> index f920895..895f95f 100644
> --- a/ell/util.c
> +++ b/ell/util.c
> @@ -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);
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.
> +
> + if (str_len_diff < 0)
> + return false;
> +
> + return !strcmp(&str[str_len_diff], suffix);
> +}
Regards,
Mat
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] util: Add suffix check for a string
2016-04-25 16:16 ` [PATCH 1/2] util: Add " Mat Martineau
@ 2016-04-25 17:36 ` Denis Kenzior
0 siblings, 0 replies; 4+ messages in thread
From: Denis Kenzior @ 2016-04-25 17:36 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 421 bytes --]
Hi Tim,
>> +
>> + 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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-04-25 17:36 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-22 22:06 [PATCH 1/2] util: Add suffix check for a string Tim Kourt
2016-04-22 22:06 ` [PATCH 2/2] unit: Add tests for the " Tim Kourt
2016-04-25 16:16 ` [PATCH 1/2] util: Add " Mat Martineau
2016-04-25 17:36 ` Denis Kenzior
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.