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) {