From 5ebcf40c8c8a37e3995e1195f80bcf89bcae1a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sat, 30 Sep 2017 16:24:46 -0700 Subject: [PATCH] Cleanup. --- include/bx/string.h | 20 ++++++++++++++---- src/filepath.cpp | 4 ++-- src/string.cpp | 48 ++++++++++++++++++++++++++++++------------- tests/string_test.cpp | 36 ++++++++++++++++---------------- 4 files changed, 70 insertions(+), 38 deletions(-) diff --git a/include/bx/string.h b/include/bx/string.h index 5392478..f06c4b6 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -179,16 +179,28 @@ namespace bx int32_t strCat(char* _dst, int32_t _dstSize, const StringView& _str); /// Find character in string. Limit search to _max characters. - const char* strFind(const char* _str, char _ch, int32_t _max = INT32_MAX); + const char* strFind(const char* _str, int32_t _max, char _ch); + + /// + const char* strFind(const StringView& _str, char _ch); /// Find character in string in reverse. Limit search to _max characters. - const char* strRFind(const char* _str, char _ch, int32_t _max = INT32_MAX); + const char* strRFind(const char* _str, int32_t _max, char _ch); + + /// + const char* strRFind(const StringView& _str, char _ch); /// Find substring in string. Limit search to _max characters. - const char* strFind(const char* _str, const char* _find, int32_t _max = INT32_MAX); + 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); /// Find substring in string. Case insensitive. Limit search to _max characters. - const char* strFindI(const char* _str, const char* _find, int32_t _max = INT32_MAX); + 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); /// Find new line. Returns pointer after new line terminator. const char* strnl(const char* _str); diff --git a/src/filepath.cpp b/src/filepath.cpp index 09343b2..eada573 100644 --- a/src/filepath.cpp +++ b/src/filepath.cpp @@ -239,7 +239,7 @@ namespace bx const StringView FilePath::getPath() const { - const char* end = strRFind(m_filePath, '/'); + const char* end = strRFind(m_filePath, INT32_MAX, '/'); if (NULL != end) { return StringView(m_filePath, end+1); @@ -250,7 +250,7 @@ namespace bx const StringView FilePath::getFileName() const { - const char* fileName = strRFind(m_filePath, '/'); + const char* fileName = strRFind(m_filePath, INT32_MAX, '/'); if (NULL != fileName) { return StringView(fileName+1); diff --git a/src/string.cpp b/src/string.cpp index a1d5445..c0f0328 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -266,7 +266,7 @@ namespace bx return strCat(_dst, _dstSize, _str.getPtr(), _str.getLength() ); } - const char* strFind(const char* _str, char _ch, int32_t _max) + const char* strFind(const char* _str, int32_t _max, char _ch) { for (int32_t ii = 0, len = strLen(_str, _max); ii < len; ++ii) { @@ -279,7 +279,12 @@ namespace bx return NULL; } - const char* strRFind(const char* _str, char _ch, int32_t _max) + const char* strFind(const StringView& _str, char _ch) + { + return strFind(_str.getPtr(), _str.getLength(), _ch); + } + + const char* strRFind(const char* _str, int32_t _max, char _ch) { for (int32_t ii = strLen(_str, _max); 0 <= ii; --ii) { @@ -292,8 +297,13 @@ namespace bx return NULL; } + const char* strRFind(const StringView& _str, char _ch) + { + return strRFind(_str.getPtr(), _str.getLength(), _ch); + } + template - static const char* strStr(const char* _str, int32_t _strMax, const char* _find, int32_t _findMax) + static const char* strFind(const char* _str, int32_t _strMax, const char* _find, int32_t _findMax) { const char* ptr = _str; @@ -333,27 +343,37 @@ namespace bx return NULL; } - const char* strFind(const char* _str, const char* _find, int32_t _max) + const char* strFind(const char* _str, int32_t _max, const char* _find, int32_t _findMax) { - return strStr(_str, _max, _find, INT32_MAX); + return strFind(_str, _max, _find, _findMax); } - const char* strFindI(const char* _str, const char* _find, int32_t _max) + const char* strFind(const StringView& _str, const StringView& _find) { - return strStr(_str, _max, _find, INT32_MAX); + 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() ); } const char* strnl(const char* _str) { for (; '\0' != *_str; _str += strLen(_str, 1024) ) { - const char* eol = strFind(_str, "\r\n", 1024); + const char* eol = strFind(_str, 1024, "\r\n"); if (NULL != eol) { return eol + 2; } - eol = strFind(_str, "\n", 1024); + eol = strFind(_str, 1024, "\n"); if (NULL != eol) { return eol + 1; @@ -367,13 +387,13 @@ namespace bx { for (; '\0' != *_str; _str += strLen(_str, 1024) ) { - const char* eol = strFind(_str, "\r\n", 1024); + const char* eol = strFind(_str, 1024, "\r\n"); if (NULL != eol) { return eol; } - eol = strFind(_str, "\n", 1024); + eol = strFind(_str, 1024, "\n"); if (NULL != eol) { return eol; @@ -443,8 +463,8 @@ namespace bx const char* findIdentifierMatch(const char* _str, const char* _word) { int32_t len = strLen(_word); - const char* ptr = strFind(_str, _word); - for (; NULL != ptr; ptr = strFind(ptr + len, _word) ) + const char* ptr = strFind(_str, INT32_MAX, _word); + for (; NULL != ptr; ptr = strFind(ptr + len, INT32_MAX, _word) ) { if (ptr != _str) { @@ -628,7 +648,7 @@ namespace bx toUpperUnsafe(str, len); } - const char* dot = strFind(str, '.'); + const char* dot = strFind(str, INT32_MAX, '.'); if (NULL != dot) { const int32_t precLen = int32_t( diff --git a/tests/string_test.cpp b/tests/string_test.cpp index a2e30db..9f055d1 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -153,19 +153,19 @@ TEST_CASE("strCmpV sort", "") TEST_CASE("strRFind", "") { const char* test = "test"; - REQUIRE(NULL == bx::strRFind(test, 's', 0) ); - REQUIRE(NULL == bx::strRFind(test, 's', 1) ); - REQUIRE(&test[2] == bx::strRFind(test, 's') ); + REQUIRE(NULL == bx::strRFind(test, 0, 's') ); + REQUIRE(NULL == bx::strRFind(test, 1, 's') ); + REQUIRE(&test[2] == bx::strRFind(test, INT32_MAX, 's') ); } TEST_CASE("strFindI", "") { const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog."; - REQUIRE(NULL == bx::strFindI(test, "quick", 8) ); - REQUIRE(NULL == bx::strFindI(test, "quick1") ); - REQUIRE(&test[4] == bx::strFindI(test, "quick", 9) ); - REQUIRE(&test[4] == bx::strFindI(test, "quick") ); + 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") ); } TEST_CASE("strFind", "") @@ -173,23 +173,23 @@ TEST_CASE("strFind", "") { const char* test = "test"; - REQUIRE(NULL == bx::strFind(test, 's', 0) ); - REQUIRE(NULL == bx::strFind(test, 's', 2) ); - REQUIRE(&test[2] == bx::strFind(test, 's') ); + REQUIRE(NULL == bx::strFind(test, 0, 's') ); + REQUIRE(NULL == bx::strFind(test, 2, 's') ); + REQUIRE(&test[2] == bx::strFind(test, INT32_MAX, 's') ); } { const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog."; - REQUIRE(NULL == bx::strFind(test, "quick", 8) ); - REQUIRE(NULL == bx::strFind(test, "quick1") ); - REQUIRE(NULL == bx::strFind(test, "quick", 9) ); - REQUIRE(NULL == bx::strFind(test, "quick") ); + 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(test, "Quick", 8) ); - REQUIRE(NULL == bx::strFind(test, "Quick1") ); - REQUIRE(&test[4] == bx::strFind(test, "Quick", 9) ); - REQUIRE(&test[4] == 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") ); } }