소스 검색

core: _strnstr() and _strnistr() defined in core

- alternative to strnstr() which is not in all OSes, pluse the
  insensitive option
Daniel-Constantin Mierla 9 년 전
부모
커밋
7772e14fc2
2개의 변경된 파일56개의 추가작업 그리고 0개의 파일을 삭제
  1. 51 0
      str.c
  2. 5 0
      str.h

+ 51 - 0
str.c

@@ -1,5 +1,6 @@
 /*
 /*
  *
  *
+ * Copyright (C) 2015 kamailio.org
  * Copyright (C) 2014 Victor Seva <[email protected]>
  * Copyright (C) 2014 Victor Seva <[email protected]>
  *
  *
  * This file is part of kamailio, a free SIP server.
  * This file is part of kamailio, a free SIP server.
@@ -48,3 +49,53 @@ int str_append(str *orig, str *suffix, str *dest)
 	memcpy(dest->s+orig->len, suffix->s, suffix->len);
 	memcpy(dest->s+orig->len, suffix->s, suffix->len);
 	return 0;
 	return 0;
 }
 }
+
+/*
+* Find the first occurrence of find in s, where the search is limited to the
+* first slen characters of s.
+*/
+char * _strnstr(const char* s, const char* find, size_t slen)
+{
+	char c, sc;
+	size_t len;
+
+	if ((c = *find++) != '\0') {
+		len = strlen(find);
+		do {
+			do {
+				if (slen-- < 1 || (sc = *s++) == '\0')
+					return (NULL);
+			} while (sc != c);
+			if (len > slen)
+				return (NULL);
+		} while (strncmp(s, find, len) != 0);
+		s--;
+	}
+	return ((char *)s);
+}
+
+/*
+ * Find the first case insensitive occurrence of find in s, where the
+ * search is limited to the first slen characters of s.
+ * Based on FreeBSD strnstr.
+ */
+char* _strnistr(const char *s, const char *find, size_t slen)
+{
+	char c, sc;
+	size_t len;
+
+	if ((c = *find++) != '\0') {
+		len = strlen(find);
+		do {
+			do {
+				if ((sc = *s++) == '\0' || slen-- < 1)
+					return (NULL);
+			} while (sc != c);
+			if (len > slen)
+				return (NULL);
+		} while (strncasecmp(s, find, len) != 0);
+		s--;
+	}
+	return ((char *)s);
+}
+

+ 5 - 0
str.h

@@ -21,6 +21,8 @@
 #ifndef str_h
 #ifndef str_h
 #define str_h
 #define str_h
 
 
+#include <string.h>
+
 /** @defgroup str_string Counted-Length Strings 
 /** @defgroup str_string Counted-Length Strings 
  * @{
  * @{
  * 
  * 
@@ -125,4 +127,7 @@ typedef struct _str str;
  */
  */
 int str_append(str *orig, str *suffix, str *dest);
 int str_append(str *orig, str *suffix, str *dest);
 
 
+char* _strnstr(const char *s, const char *find, size_t slen);
+char* _strnistr(const char *s, const char *find, size_t slen);
+
 #endif
 #endif