From efa97e61da8eb7e30ffdaa6c984e3417ca48d864 Mon Sep 17 00:00:00 2001 From: Dario Manesku Date: Mon, 15 Dec 2014 02:54:03 +0100 Subject: [PATCH 1/2] Added stristr(). --- include/bx/string.h | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/include/bx/string.h b/include/bx/string.h index c1b87bb..2c805e8 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -73,6 +73,87 @@ namespace bx return --_str; } + /// Find substring in string. Case insensitive. + inline const char* stristr(const char* _str, const char* _find) + { + const char* ptr = _str; + + for (size_t len = strlen(_str), searchLen = strlen(_find) + ; len >= searchLen + ; ++ptr, --len) + { + // Find start of the string. + while (tolower(*ptr) != tolower(*_find) ) + { + ++ptr; + --len; + + // Search pattern lenght can't be longer than the string. + if (searchLen > len) + { + return NULL; + } + } + + // Set pointers. + const char* string = ptr; + const char* search = _find; + + // Start comparing. + while (tolower(*string++) == tolower(*search++)) + { + // If end of the 'search' string is reached, all characters match. + if ('\0' == *search) + { + return ptr; + } + } + } + + return NULL; + } + + /// Find substring in string. Case insensitive. Limit search to _size. + inline const char* stristr(const char* _str, const char* _find, size_t _max) + { + const char* ptr = _str; + + const size_t total = strlen(_str); + size_t len = _max < total ? _max : total; + + for (const size_t searchLen = strlen(_find); len >= searchLen; ++ptr, --len) + { + // Find start of the string. + while (tolower(*ptr) != tolower(*_find) ) + { + ++ptr; + --len; + + // Search pattern lenght can't be longer than the string. + if (searchLen > len) + { + return NULL; + } + } + + // Set pointers. + const char* string = ptr; + const char* search = _find; + + // Start comparing. + while (tolower(*string++) == tolower(*search++)) + { + // If end of the 'search' string is reached, all characters match. + if ('\0' == *search) + { + return ptr; + } + } + } + + return NULL; + } + /// Find new line. Returns pointer after new line terminator. inline const char* strnl(const char* _str) { From f3b8fb3e671146d93c48ce1196221e056f4f5205 Mon Sep 17 00:00:00 2001 From: Dario Manesku Date: Mon, 15 Dec 2014 02:57:13 +0100 Subject: [PATCH 2/2] Cleanup. --- include/bx/string.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bx/string.h b/include/bx/string.h index 2c805e8..aed38e7 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -100,7 +100,7 @@ namespace bx const char* search = _find; // Start comparing. - while (tolower(*string++) == tolower(*search++)) + while (tolower(*string++) == tolower(*search++) ) { // If end of the 'search' string is reached, all characters match. if ('\0' == *search) @@ -141,7 +141,7 @@ namespace bx const char* search = _find; // Start comparing. - while (tolower(*string++) == tolower(*search++)) + while (tolower(*string++) == tolower(*search++) ) { // If end of the 'search' string is reached, all characters match. if ('\0' == *search)