aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPer Larsson <[email protected]>2021-09-17 13:10:12 +0200
committerPer Larsson <[email protected]>2021-09-17 13:10:12 +0200
commitca33b89e2ec26355b17e589fb254fa59438c87d8 (patch)
treea874c2abfc65fe62013cb5b4bfd149e501f499b0
parentMerge branch 'main' of https://github.com/EpicGames/zen (diff)
downloadzen-ca33b89e2ec26355b17e589fb254fa59438c87d8.tar.xz
zen-ca33b89e2ec26355b17e589fb254fa59438c87d8.zip
Added helper function for iterating string tokens.
-rw-r--r--zencore/include/zencore/string.h39
-rw-r--r--zencore/string.cpp40
2 files changed, 79 insertions, 0 deletions
diff --git a/zencore/include/zencore/string.h b/zencore/include/zencore/string.h
index 2b5f20f86..bb9b1c896 100644
--- a/zencore/include/zencore/string.h
+++ b/zencore/include/zencore/string.h
@@ -622,6 +622,45 @@ ToLower(const std::string_view& InString)
//////////////////////////////////////////////////////////////////////////
+template<typename Fn>
+uint32_t
+ForEachStrTok(const std::string_view& Str, char Delim, Fn&& Func)
+{
+ auto It = Str.begin();
+ auto End = Str.end();
+ uint32_t Count = 0;
+
+ while (It != End)
+ {
+ if (*It == Delim)
+ {
+ It++;
+ continue;
+ }
+
+ std::string_view Remaining{It, End};
+ size_t Idx = Remaining.find(Delim, 0);
+
+ if (Idx == std::string_view::npos)
+ {
+ Idx = Remaining.size();
+ }
+
+ Count++;
+ std::string_view Token{It, It + Idx};
+ if (!Func(Token))
+ {
+ break;
+ }
+
+ It = It + Idx;
+ }
+
+ return Count;
+}
+
+//////////////////////////////////////////////////////////////////////////
+
void string_forcelink(); // internal
} // namespace zen
diff --git a/zencore/string.cpp b/zencore/string.cpp
index 8ea10d2a3..ce1b6d675 100644
--- a/zencore/string.cpp
+++ b/zencore/string.cpp
@@ -912,4 +912,44 @@ TEST_CASE("filepath")
CHECK(FilepathFindExtension(".txt") == std::string_view(".txt"));
}
+TEST_CASE("string")
+{
+ using namespace std::literals;
+
+ SUBCASE("ForEachStrTok")
+ {
+ const auto Tokens = "here,is,my,different,tokens"sv;
+ int32_t ExpectedTokenCount = 5;
+ int32_t TokenCount = 0;
+ StringBuilder<512> Sb;
+
+ TokenCount = ForEachStrTok(Tokens, ',', [&Sb](const std::string_view& Token) {
+ if (Sb.Size())
+ {
+ Sb << ",";
+ }
+ Sb << Token;
+ return true;
+ });
+
+ CHECK(TokenCount == ExpectedTokenCount);
+ CHECK(Sb.ToString() == Tokens);
+
+ ExpectedTokenCount = 1;
+ const auto Str = "mosdef"sv;
+
+ Sb.Reset();
+ TokenCount = ForEachStrTok(Str, ' ', [&Sb](const std::string_view& Token) {
+ Sb << Token;
+ return true;
+ });
+ CHECK(Sb.ToString() == Str);
+ CHECK(TokenCount == ExpectedTokenCount);
+
+ ExpectedTokenCount = 0;
+ TokenCount = ForEachStrTok(""sv, ',', [](const std::string_view&) { return true; });
+ CHECK(TokenCount == ExpectedTokenCount);
+ }
+}
+
} // namespace zen