All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tim Kourt <tim.a.kourt@linux.intel.com>
To: ell@lists.01.org
Subject: [PATCH v2 1/2] util: Add suffix check for a string
Date: Mon, 25 Apr 2016 13:51:02 -0700	[thread overview]
Message-ID: <1461617463-31883-1-git-send-email-tim.a.kourt@linux.intel.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1623 bytes --]

---
 ell/util.c | 32 ++++++++++++++++++++++++++++++++
 ell/util.h |  1 +
 2 files changed, 33 insertions(+)

diff --git a/ell/util.c b/ell/util.c
index f920895..98916e5 100644
--- a/ell/util.c
+++ b/ell/util.c
@@ -444,6 +444,38 @@ 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)
+{
+	size_t str_len;
+	size_t suffix_len;
+	size_t len_diff;
+
+	if (unlikely(!str))
+		return false;
+
+	if (unlikely(!suffix))
+		return false;
+
+	str_len = strlen(str);
+	suffix_len = strlen(suffix);
+
+	if (str_len < suffix_len)
+		return false;
+
+	len_diff = str_len - suffix_len;
+
+	return !strcmp(&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


             reply	other threads:[~2016-04-25 20:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-25 20:51 Tim Kourt [this message]
2016-04-25 20:51 ` [PATCH v2 2/2] unit: Add tests for the suffix check for a string Tim Kourt
2016-04-26  3:48 ` [PATCH v2 1/2] util: Add " Denis Kenzior

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1461617463-31883-1-git-send-email-tim.a.kourt@linux.intel.com \
    --to=tim.a.kourt@linux.intel.com \
    --cc=ell@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.