[sr-dev] git:master:747ed6d1: core: ut - functions for reverse search of str value

Daniel-Constantin Mierla miconda at gmail.com
Thu Sep 23 09:31:57 CEST 2021


Module: kamailio
Branch: master
Commit: 747ed6d1c9434a90b77fd86c4dae35e447da9185
URL: https://github.com/kamailio/kamailio/commit/747ed6d1c9434a90b77fd86c4dae35e447da9185

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2021-09-23T09:22:21+02:00

core: ut - functions for reverse search of str value

---

Modified: src/core/ut.c
Modified: src/core/ut.h

---

Diff:  https://github.com/kamailio/kamailio/commit/747ed6d1c9434a90b77fd86c4dae35e447da9185.diff
Patch: https://github.com/kamailio/kamailio/commit/747ed6d1c9434a90b77fd86c4dae35e447da9185.patch

---

diff --git a/src/core/ut.c b/src/core/ut.c
index 00254eba23..8dde51aaff 100644
--- a/src/core/ut.c
+++ b/src/core/ut.c
@@ -392,6 +392,49 @@ char *str_casesearch_strz(str *text, char *needlez)
 	return str_casesearch(text, &needle);
 }
 
+/**
+ * @brief search for last occurence of needle in text (reverse search)
+ * @return pointer to start of needle in text or NULL if the needle
+ *	is not found
+ */
+char *str_rsearch(str *text, str *needle)
+{
+    char *p;
+
+    if(text==NULL || text->s==NULL || needle==NULL || needle->s==NULL
+			|| text->len<needle->len)
+        return NULL;
+
+    for (p = text->s + text->len - needle->len; p >= text->s; p--) {
+        if (*p == *needle->s && memcmp(p, needle->s, needle->len)==0) {
+            return p;
+        }
+    }
+
+    return NULL;
+}
+
+/**
+ * @brief case insensitive search for last occurence of needle in text (reverse search)
+ * @return pointer to start of needle in text or NULL if the needle
+ *	is not found
+ */
+char *str_rcasesearch(str *text, str *needle)
+{
+	int i,j;
+	for(i=text->len-needle->len;i>=0;i--) {
+		for(j=0;j<needle->len;j++) {
+			if ( !((text->s[i+j]==needle->s[j]) ||
+					( isalpha((int)text->s[i+j])
+						&& ((text->s[i+j])^(needle->s[j]))==0x20 )) )
+				break;
+		}
+		if (j==needle->len)
+			return text->s+i;
+	}
+	return NULL;
+}
+
 /*
  * ser_memmem() returns the location of the first occurrence of data
  * pattern b2 of size len2 in memory block b1 of size len1 or
diff --git a/src/core/ut.h b/src/core/ut.h
index f2173d3dcf..385db99ca7 100644
--- a/src/core/ut.h
+++ b/src/core/ut.h
@@ -1110,6 +1110,10 @@ char *strz_casesearch_strz(char *textz, char *needlez);
 
 char *str_casesearch_strz(str *text, char *needlez);
 
+char *str_rsearch(str *text, str *needle);
+
+char *str_rcasesearch(str *text, str *needle);
+
 /*
  * ser_memmem() returns the location of the first occurrence of data
  * pattern b2 of size len2 in memory block b1 of size len1 or




More information about the sr-dev mailing list