From ca33b89e2ec26355b17e589fb254fa59438c87d8 Mon Sep 17 00:00:00 2001 From: Per Larsson Date: Fri, 17 Sep 2021 13:10:12 +0200 Subject: Added helper function for iterating string tokens. --- zencore/string.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'zencore/string.cpp') 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 -- cgit v1.2.3 From 8d3024a7e09246e44bf6d9ea14a36e6b03032e85 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 20 Sep 2021 10:15:12 +0200 Subject: It's not possible to compile out tests Tests are now compiled in if `ZEN_WITH_TESTS=1`, and compiled out if not. Compiling tests out reduces the footprint of the resulting executables quite significantly. --- zencore/string.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'zencore/string.cpp') diff --git a/zencore/string.cpp b/zencore/string.cpp index ce1b6d675..6dcdc9542 100644 --- a/zencore/string.cpp +++ b/zencore/string.cpp @@ -1,11 +1,12 @@ // Copyright Epic Games, Inc. All Rights Reserved. -#include +#include +#include +#include + #include #include #include -#include -#include #include #include #include @@ -452,6 +453,8 @@ template class StringBuilderImpl; // Unit tests // +#if ZEN_WITH_TESTS + TEST_CASE("niceNum") { char Buffer[16]; @@ -952,4 +955,6 @@ TEST_CASE("string") } } +#endif + } // namespace zen -- cgit v1.2.3