Browse Source

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
Daniel-Constantin Mierla 14 years ago
parent
commit
8c2a2826f5
2 changed files with 27 additions and 0 deletions
  1. 23 0
      ut.c
  2. 4 0
      ut.h

+ 23 - 0
ut.c

@@ -274,3 +274,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;
+}

+ 4 - 0
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