Browse code

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 authored on 26/03/2011 22:44:01
Showing 2 changed files
... ...
@@ -274,3 +274,26 @@ char* get_abs_pathname(str* base, str* file)
274 274
 	}
275 275
 	return res;
276 276
 }
277
+
278
+
279
+/**
280
+ * @brief search for occurence of needle in text
281
+ * @return pointer to start of needle in text or NULL if the needle
282
+ *	is not found
283
+ */
284
+char *str_search(str *text, str *needle)
285
+{
286
+    char *p;
287
+
288
+    if(text==NULL || text->s==NULL || needle==NULL || needle->s==NULL
289
+			|| text->len<needle->len)
290
+        return NULL;
291
+
292
+    for (p = text->s; p <= text->s + text->len - needle->len; p++) {
293
+        if (*p == *needle->s && memcmp(p, needle->s, needle->len)==0) {
294
+            return p;
295
+        }
296
+    }
297
+
298
+    return NULL;
299
+}
... ...
@@ -824,4 +824,8 @@ unsigned int get_sys_version(int* major, int* minor, int* minor2);
824 824
  */
825 825
 char* get_abs_pathname(str* base, str* file);
826 826
 
827
+/**
828
+ * search for needle in text
829
+ */
830
+char *str_search(str *text, str *needle);
827 831
 #endif