1
0
Selaa lähdekoodia

faux: str: faux_str_casestr()

Serj Kalichev 4 vuotta sitten
vanhempi
commit
d11a1b573b
3 muutettua tiedostoa jossa 24 lisäystä ja 21 poistoa
  1. 1 0
      faux/ini.h
  2. 1 1
      faux/str.h
  3. 22 20
      faux/str/str.c

+ 1 - 0
faux/ini.h

@@ -32,6 +32,7 @@ const faux_pair_t *faux_ini_each(faux_ini_node_t **iter);
 
 int faux_ini_parse_str(faux_ini_t *ini, const char *str);
 int faux_ini_parse_file(faux_ini_t *ini, const char *fn);
+int faux_ini_write_file(const faux_ini_t *ini, const char *fn);
 
 C_DECL_END
 

+ 1 - 1
faux/str.h

@@ -29,10 +29,10 @@ char *faux_str_toupper(const char *str);
 
 int faux_str_ncasecmp(const char *str1, const char *str2, size_t n);
 int faux_str_casecmp(const char *str1, const char *str2);
+char *faux_str_casestr(const char *haystack, const char *needle);
 
 
 //const char *faux_str_suffix(const char *string);
-//const char *faux_str_nocasestr(const char *cs, const char *ct);
 /*
  * These are the escape characters which are used by default when 
  * expanding variables. These characters will be backslash escaped

+ 22 - 20
faux/str/str.c

@@ -8,6 +8,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <assert.h>
 
 #include "faux/ctype.h"
 #include "faux/str.h"
@@ -245,7 +246,7 @@ int faux_str_ncasecmp(const char *str1, const char *str2, size_t n) {
 		return 0;
 
 	return faux_str_cmp_chars(faux_ctype_tolower(*p1),
-			faux_ctype_tolower(*p2));
+		faux_ctype_tolower(*p2));
 }
 
 
@@ -274,32 +275,33 @@ int faux_str_casecmp(const char *str1, const char *str2) {
 	}
 
 	return faux_str_cmp_chars(faux_ctype_tolower(*p1),
-			faux_ctype_tolower(*p2));
+		faux_ctype_tolower(*p2));
 }
 
 
-const char *lub_string_nocasestr(const char *cs, const char *ct)
+char *faux_str_casestr(const char *haystack, const char *needle)
 {
-	const char *p = NULL;
-	const char *result = NULL;
+	const char *ptr = haystack;
+	size_t ptr_len = 0;
+	size_t needle_len = 0;
 
-	while (*cs) {
-		const char *q = cs;
+	assert(haystack);
+	assert(needle);
+	if (!haystack || !needle)
+		return NULL;
 
-		p = ct;
-		while (*p && *q
-		       && (faux_ctype_tolower(*p) == faux_ctype_tolower(*q))) {
-			p++, q++;
-		}
-		if (0 == *p) {
-			break;
-		}
-		cs++;
-	}
-	if (p && !*p) {
-		result = cs;
+	ptr_len = strlen(haystack);
+	needle_len = strlen(needle);
+
+	while ((*ptr != '\0') && (ptr_len >= needle_len)) {
+		int res = faux_str_ncasecmp(ptr, needle, needle_len);
+		if (0 == res)
+			return (char *)ptr;
+		ptr++;
+		ptr_len--;
 	}
-	return result;
+
+	return NULL; // Not found
 }