changeset 89:30690f5c4cae

Add new function th_strrcasecmp().
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 17 May 2014 22:39:37 +0300
parents d3d3cd41a95a
children 2b1f7f1ca8e4
files th_string.c th_string.h
diffstat 2 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Sat May 17 22:39:27 2014 +0300
+++ b/th_string.c	Sat May 17 22:39:37 2014 +0300
@@ -158,6 +158,29 @@
 }
 
 
+/* Check if end of the given string str matches needle
+ * case-insensitively, return pointer to start of the match,
+ * if found, NULL otherwise.
+ */
+char *th_strrcasecmp(char *str, const char *needle)
+{
+    if (str == NULL || needle == NULL)
+        return NULL;
+
+    const size_t
+        slen = strlen(str),
+        nlen = strlen(needle);
+    
+    if (slen < nlen)
+        return NULL;
+
+    if (th_strcasecmp(str - nlen - 1, needle) == 0)
+        return str - nlen - 1;
+    else
+        return NULL;
+}
+
+
 /* Remove all occurences of control characters, in-place.
  * Resulting string is always shorter or same length than original.
  */
--- a/th_string.h	Sat May 17 22:39:27 2014 +0300
+++ b/th_string.h	Sat May 17 22:39:37 2014 +0300
@@ -47,6 +47,7 @@
 
 int     th_strcasecmp(const char *, const char *);
 int     th_strncasecmp(const char *, const char *, size_t);
+char    *th_strrcasecmp(char *str, const char *needle);
 void    th_strip_ctrlchars(char *);
 
 char    *th_strdup_vprintf(const char *, va_list);