From 91455f1c480e97f3b01e0e5bbb688764128a8b42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 1 Oct 2017 21:57:31 -0700 Subject: [PATCH] Switching code to use StringView. --- include/bx/string.h | 10 ++-------- src/string.cpp | 40 ++++++++++++++++++++-------------------- tests/string_test.cpp | 30 +++++++++++++++--------------- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/include/bx/string.h b/include/bx/string.h index 5e78b44..dfb6d09 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -203,16 +203,10 @@ namespace bx const char* strRFind(const StringView& _str, char _ch); /// Find substring in string. Limit search to _max characters. - const char* strFind(const char* _str, int32_t _max, const char* _find, int32_t _findMax = INT32_MAX); - - /// - const char* strFind(const StringView& _str, const StringView& _find); + const char* strFind(const StringView& _str, const StringView& _find, int32_t _num = INT32_MAX); /// Find substring in string. Case insensitive. Limit search to _max characters. - const char* strFindI(const char* _str, int32_t _max, const char* _find, int32_t _findMax = INT32_MAX); - - /// - const char* strFindI(const StringView& _str, const StringView& _find); + const char* strFindI(const StringView& _str, const StringView& _find, int32_t _num = INT32_MAX); /// Find new line. Returns pointer after new line terminator. const char* strnl(const char* _str); diff --git a/src/string.cpp b/src/string.cpp index 01ed8ee..dd055cb 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -428,37 +428,37 @@ namespace bx return NULL; } - const char* strFind(const char* _str, int32_t _max, const char* _find, int32_t _findMax) + const char* strFind(const StringView& _str, const StringView& _find, int32_t _num) { - return strFind(_str, _max, _find, _findMax); + return strFind( + _str.getPtr() + , _str.getLength() + , _find.getPtr() + , min(_find.getLength(), _num) + ); } - const char* strFind(const StringView& _str, const StringView& _find) + const char* strFindI(const StringView& _str, const StringView& _find, int32_t _num) { - return strFind(_str.getPtr(), _str.getLength(), _find.getPtr(), _find.getLength() ); - } - - const char* strFindI(const char* _str, int32_t _max, const char* _find, int32_t _findMax) - { - return strFind(_str, _max, _find, _findMax); - } - - const char* strFindI(const StringView& _str, const StringView& _find) - { - return strFindI(_str.getPtr(), _str.getLength(), _find.getPtr(), _find.getLength() ); + return strFind( + _str.getPtr() + , _str.getLength() + , _find.getPtr() + , min(_find.getLength(), _num) + ); } const char* strnl(const char* _str) { for (; '\0' != *_str; _str += strLen(_str, 1024) ) { - const char* eol = strFind(_str, 1024, "\r\n"); + const char* eol = strFind(StringView(_str, 1024), "\r\n"); if (NULL != eol) { return eol + 2; } - eol = strFind(_str, 1024, "\n"); + eol = strFind(StringView(_str, 1024), "\n"); if (NULL != eol) { return eol + 1; @@ -472,13 +472,13 @@ namespace bx { for (; '\0' != *_str; _str += strLen(_str, 1024) ) { - const char* eol = strFind(_str, 1024, "\r\n"); + const char* eol = strFind(StringView(_str, 1024), "\r\n"); if (NULL != eol) { return eol; } - eol = strFind(_str, 1024, "\n"); + eol = strFind(StringView(_str, 1024), "\n"); if (NULL != eol) { return eol; @@ -548,8 +548,8 @@ namespace bx const char* findIdentifierMatch(const char* _str, const char* _word) { int32_t len = strLen(_word); - const char* ptr = strFind(_str, INT32_MAX, _word); - for (; NULL != ptr; ptr = strFind(ptr + len, INT32_MAX, _word) ) + const char* ptr = strFind(_str, _word); + for (; NULL != ptr; ptr = strFind(ptr + len, _word) ) { if (ptr != _str) { diff --git a/tests/string_test.cpp b/tests/string_test.cpp index c0ba4ab..6c2d1b0 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -164,10 +164,10 @@ TEST_CASE("strFindI", "") { const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog."; - REQUIRE(NULL == bx::strFindI(test, 8, "quick") ); - REQUIRE(NULL == bx::strFindI(test, INT32_MAX, "quick1") ); - REQUIRE(&test[4] == bx::strFindI(test, 9, "quick") ); - REQUIRE(&test[4] == bx::strFindI(test, INT32_MAX, "quick") ); + REQUIRE(NULL == bx::strFindI(bx::StringView(test, 8), "quick") ); + REQUIRE(NULL == bx::strFindI(test, "quick1") ); + REQUIRE(&test[4] == bx::strFindI(bx::StringView(test, 9), "quick") ); + REQUIRE(&test[4] == bx::strFindI(test, "quick") ); } TEST_CASE("strFind", "") @@ -175,23 +175,23 @@ TEST_CASE("strFind", "") { const char* test = "test"; - REQUIRE(NULL == bx::strFind(test, 0, 's') ); - REQUIRE(NULL == bx::strFind(test, 2, 's') ); - REQUIRE(&test[2] == bx::strFind(test, INT32_MAX, 's') ); + REQUIRE(NULL == bx::strFind(bx::StringView(test, 0), 's') ); + REQUIRE(NULL == bx::strFind(bx::StringView(test, 2), 's') ); + REQUIRE(&test[2] == bx::strFind(test, 's') ); } { const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog."; - REQUIRE(NULL == bx::strFind(test, 8, "quick") ); - REQUIRE(NULL == bx::strFind(test, INT32_MAX, "quick1") ); - REQUIRE(NULL == bx::strFind(test, 9, "quick") ); - REQUIRE(NULL == bx::strFind(test, INT32_MAX, "quick") ); + REQUIRE(NULL == bx::strFind(bx::StringView(test, 8), "quick") ); + REQUIRE(NULL == bx::strFind(test, "quick1") ); + REQUIRE(NULL == bx::strFind(bx::StringView(test, 9), "quick") ); + REQUIRE(NULL == bx::strFind(test, "quick") ); - REQUIRE(NULL == bx::strFind(test, 8, "Quick") ); - REQUIRE(NULL == bx::strFind(test, INT32_MAX, "Quick1") ); - REQUIRE(&test[4] == bx::strFind(test, 9, "Quick") ); - REQUIRE(&test[4] == bx::strFind(test, INT32_MAX, "Quick") ); + REQUIRE(NULL == bx::strFind(bx::StringView(test, 8), "Quick") ); + REQUIRE(NULL == bx::strFind(test, "Quick1") ); + REQUIRE(&test[4] == bx::strFind(bx::StringView(test, 9), "Quick") ); + REQUIRE(&test[4] == bx::strFind(test, "Quick") ); } }