From e3993fd7e99c56162d1a0e26be6463ded65f15cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sat, 9 Dec 2017 17:54:52 -0800 Subject: [PATCH] Cleanup. --- include/bx/string.h | 7 +++++-- src/string.cpp | 11 +++++++++-- tests/string_test.cpp | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/bx/string.h b/include/bx/string.h index 331bda8..3f57837 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -215,8 +215,11 @@ namespace bx /// Skip non-whitespace. const char* strnws(const char* _str); - /// Skip word. - const char* strword(const char* _str); + /// Returns pointer to first character after word. + const char* strSkipWord(const char* _str, int32_t _max = INT32_MAX); + + /// Returns StringView of word or empty. + StringView strWord(const StringView& _str); /// Find matching block. const char* strmb(const char* _str, char _open, char _close); diff --git a/src/string.cpp b/src/string.cpp index 5fe2725..cf6fc15 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -531,12 +531,19 @@ namespace bx return _str; } - const char* strword(const char* _str) + const char* strSkipWord(const char* _str, int32_t _max) { - for (char ch = *_str++; isAlphaNum(ch) || '_' == ch; ch = *_str++) {}; + for (char ch = *_str++; 0 < _max && (isAlphaNum(ch) || '_' == ch); ch = *_str++, --_max) {}; return _str-1; } + StringView strWord(const StringView& _str) + { + const char* ptr = _str.getPtr(); + const char* term = strSkipWord(ptr, _str.getLength() ); + return StringView(ptr, term); + } + const char* strmb(const char* _str, char _open, char _close) { int count = 0; diff --git a/tests/string_test.cpp b/tests/string_test.cpp index d848f3d..4f94e08 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -389,3 +389,9 @@ TEST_CASE("Trim", "") bx::FilePath uri("/555333/podmac/"); REQUIRE(0 == bx::strCmp(bx::strTrim(uri.getPath(), "/"), "555333/podmac") ); } + +TEST_CASE("strWord", "") +{ + REQUIRE(bx::strWord(" abvgd-1389.0").isEmpty() ); + REQUIRE(0 == bx::strCmp(bx::strWord("abvgd-1389.0"), "abvgd") ); +}