[sr-dev] git:3.1: core: added function to search a str inside a str

Daniel-Constantin Mierla miconda at gmail.com
Tue Mar 29 12:43:32 CEST 2011


Module: sip-router
Branch: 3.1
Commit: c61a9bfab9bab92f9c7f59cba880a8431b5be146
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=c61a9bfab9bab92f9c7f59cba880a8431b5be146

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Sat Mar 26 23:44:01 2011 +0100

core: added function to search a str inside a str

- str_search(text, needle) added to return the position of str needle
  when it is found inside str text
(cherry picked from commit 8c2a2826f5f6954b4a38405fd04f128078e98e0f)

---

 ut.c |   23 +++++++++++++++++++++++
 ut.h |    4 ++++
 2 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/ut.c b/ut.c
index 275381c..9bb4b9f 100644
--- a/ut.c
+++ b/ut.c
@@ -273,3 +273,26 @@ char* get_abs_pathname(str* base, str* file)
 	}
 	return res;
 }
+
+
+/**
+ * @brief search for occurence of needle in text
+ * @return pointer to start of needle in text or NULL if the needle
+ *	is not found
+ */
+char *str_search(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; p <= text->s + text->len - needle->len; p++) {
+        if (*p == *needle->s && memcmp(p, needle->s, needle->len)==0) {
+            return p;
+        }
+    }
+
+    return NULL;
+}
diff --git a/ut.h b/ut.h
index 1e4b2b4..da40ebc 100644
--- a/ut.h
+++ b/ut.h
@@ -824,4 +824,8 @@ unsigned int get_sys_version(int* major, int* minor, int* minor2);
  */
 char* get_abs_pathname(str* base, str* file);
 
+/**
+ * search for needle in text
+ */
+char *str_search(str *text, str *needle);
 #endif




More information about the sr-dev mailing list