From e144480d3f236831d0acd9948cf7baf5019d785b Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sun, 16 Dec 2012 15:46:24 -0800 Subject: [PATCH] Added strnstr function. Included stdint.h from bx.h --- include/bx/bx.h | 1 + include/bx/float4_sse.h | 2 -- include/bx/float4_t.h | 1 - include/bx/string.h | 77 ++++++++++++++++++++++++++++++----------- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/include/bx/bx.h b/include/bx/bx.h index a5808df..3429a42 100644 --- a/include/bx/bx.h +++ b/include/bx/bx.h @@ -6,6 +6,7 @@ #ifndef __BX_H__ #define __BX_H__ +#include #include "platform.h" #include "macros.h" diff --git a/include/bx/float4_sse.h b/include/bx/float4_sse.h index 9c173c8..7936298 100644 --- a/include/bx/float4_sse.h +++ b/include/bx/float4_sse.h @@ -6,8 +6,6 @@ #ifndef __BX_FLOAT4_SSE_H__ #define __BX_FLOAT4_SSE_H__ -#include - #include // __m128i #if defined(__SSE4_1__) # include diff --git a/include/bx/float4_t.h b/include/bx/float4_t.h index 0a92748..83a0775 100644 --- a/include/bx/float4_t.h +++ b/include/bx/float4_t.h @@ -6,7 +6,6 @@ #ifndef __BX_FLOAT4_T_H__ #define __BX_FLOAT4_T_H__ -#include #include "bx.h" #define BX_FLOAT4_INLINE BX_FORCE_INLINE diff --git a/include/bx/string.h b/include/bx/string.h index 5a7fb17..60313d8 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -23,40 +23,77 @@ namespace bx #endif // BX_COMPILER_ } + /// Find substring in string. Limit search to _size. + inline const char* strnstr(const char* _str, const char* _find, size_t _size) + { + char first = *_find; + if ('\0' == first) + { + return _str; + } + + const char* cmp = _find + 1; + size_t len = strlen(cmp); + do + { + 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) { - const char* eol = strstr(_str, "\n\r"); - if (NULL != eol) + for (; '\0' != *_str; _str += strnlen(_str, 1024) ) { - return eol + 2; + 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; + } } - eol = strstr(_str, "\n"); - if (NULL != eol) - { - return eol + 1; - } - - return eol + strlen(_str); + return _str; } /// Find end of line. Retuns pointer to new line terminator. 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 = strnstr(_str, "\n", 1024); + if (NULL != eol) + { + return eol; + } } - eol = strstr(_str, "\n"); - if (NULL != eol) - { - return eol; - } - - return eol + strlen(_str); + return _str; } /// Skip whitespace.