Forráskód Böngészése

Added strnstr function. Included stdint.h from bx.h

bkaradzic 13 éve
szülő
commit
e144480d3f
4 módosított fájl, 56 hozzáadás és 21 törlés
  1. 1 0
      include/bx/bx.h
  2. 0 2
      include/bx/float4_sse.h
  3. 0 1
      include/bx/float4_t.h
  4. 55 18
      include/bx/string.h

+ 1 - 0
include/bx/bx.h

@@ -6,6 +6,7 @@
 #ifndef __BX_H__
 #ifndef __BX_H__
 #define __BX_H__
 #define __BX_H__
 
 
+#include <stdint.h>
 #include "platform.h"
 #include "platform.h"
 #include "macros.h"
 #include "macros.h"
 
 

+ 0 - 2
include/bx/float4_sse.h

@@ -6,8 +6,6 @@
 #ifndef __BX_FLOAT4_SSE_H__
 #ifndef __BX_FLOAT4_SSE_H__
 #define __BX_FLOAT4_SSE_H__
 #define __BX_FLOAT4_SSE_H__
 
 
-#include <stdint.h>
-
 #include <emmintrin.h> // __m128i
 #include <emmintrin.h> // __m128i
 #if defined(__SSE4_1__)
 #if defined(__SSE4_1__)
 #	include <smmintrin.h>
 #	include <smmintrin.h>

+ 0 - 1
include/bx/float4_t.h

@@ -6,7 +6,6 @@
 #ifndef __BX_FLOAT4_T_H__
 #ifndef __BX_FLOAT4_T_H__
 #define __BX_FLOAT4_T_H__
 #define __BX_FLOAT4_T_H__
 
 
-#include <stdint.h>
 #include "bx.h"
 #include "bx.h"
 
 
 #define BX_FLOAT4_INLINE BX_FORCE_INLINE
 #define BX_FLOAT4_INLINE BX_FORCE_INLINE

+ 55 - 18
include/bx/string.h

@@ -23,40 +23,77 @@ namespace bx
 #endif // BX_COMPILER_
 #endif // BX_COMPILER_
 	}
 	}
 
 
-	/// Find new line. Returns pointer after new line terminator.
-	inline const char* strnl(const char* _str)
+	/// Find substring in string. Limit search to _size.
+	inline const char* strnstr(const char* _str, const char* _find, size_t _size)
 	{
 	{
-		const char* eol = strstr(_str, "\n\r");
-		if (NULL != eol)
+		char first = *_find;
+		if ('\0' == first)
 		{
 		{
-			return eol + 2;
+			return _str;
 		}
 		}
 
 
-		eol = strstr(_str, "\n");
-		if (NULL != eol)
+		const char* cmp = _find + 1;
+		size_t len = strlen(cmp);
+		do 
 		{
 		{
-			return eol + 1;
+			for (char match = *_str++; match != first && 0 < _size; match = *_str++, --_size)
+			{
+				if ('\0' == match)
+				{
+					return NULL;
+				}
+			}
+
+			if (0 == _size)
+			{
+				return NULL;
+			}
+			
+		} while (0 != strncmp(_str, cmp, len) );
+
+		return --_str;
+	}
+
+	/// Find new line. Returns pointer after new line terminator.
+	inline const char* strnl(const char* _str)
+	{
+		for (; '\0' != *_str; _str += strnlen(_str, 1024) )
+		{
+			const char* eol = strnstr(_str, "\n\r", 1024);
+			if (NULL != eol)
+			{
+				return eol + 2;
+			}
+
+			eol = strnstr(_str, "\n", 1024);
+			if (NULL != eol)
+			{
+				return eol + 1;
+			}
 		}
 		}
 
 
-		return eol + strlen(_str);
+		return _str;
 	}
 	}
 
 
 	/// Find end of line. Retuns pointer to new line terminator.
 	/// Find end of line. Retuns pointer to new line terminator.
 	inline const char* streol(const char* _str)
 	inline const char* streol(const char* _str)
 	{
 	{
-		const char* eol = strstr(_str, "\n\r");
-		if (NULL != eol)
+		for (; '\0' != *_str; _str += strnlen(_str, 1024) )
 		{
 		{
-			return eol;
-		}
+			const char* eol = strnstr(_str, "\n\r", 1024);
+			if (NULL != eol)
+			{
+				return eol;
+			}
 
 
-		eol = strstr(_str, "\n");
-		if (NULL != eol)
-		{
-			return eol;
+			eol = strnstr(_str, "\n", 1024);
+			if (NULL != eol)
+			{
+				return eol;
+			}
 		}
 		}
 
 
-		return eol + strlen(_str);
+		return _str;
 	}
 	}
 
 
 	/// Skip whitespace.
 	/// Skip whitespace.