From c370cf41622e3f244c88649bbac0f69588ba65c1 Mon Sep 17 00:00:00 2001 From: kyle-cochran Date: Thu, 22 Jun 2023 23:56:07 -0700 Subject: [PATCH] feat: String::split function (#120) * feat: add string split function * test: add a few more test cases * chore: change comment a little * style: formatting --------- Co-authored-by: vishwa2710 --- .../OpenSpaceToolkit/Core/Types/String.hpp | 16 ++++++++++++++++ src/OpenSpaceToolkit/Core/Types/String.cpp | 8 ++++++++ .../Core/Types/String.test.cpp | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/include/OpenSpaceToolkit/Core/Types/String.hpp b/include/OpenSpaceToolkit/Core/Types/String.hpp index 5adc72b6..7c048f79 100644 --- a/include/OpenSpaceToolkit/Core/Types/String.hpp +++ b/include/OpenSpaceToolkit/Core/Types/String.hpp @@ -18,11 +18,20 @@ namespace ostk { namespace core { + +// Forward declare Array to avoid circular dependency +namespace ctnr +{ +template +class Array; +} + namespace types { using ostk::core::types::Index; using ostk::core::types::Size; +using ostk::core::ctnr::Array; /// @brief A sequence of characters /// @note The current implementation (derived for std::string) is temporary, as this type of @@ -68,6 +77,13 @@ class String : public std::string String getSubstring(const Index& aStartPosition, const Size& aLength) const; + /// @brief Split the String into tokens separated by the given delimeter. + /// E.X. String("1sat2satredsatbluesat").split("sat") -> ["1", "2", "red", "blue", ""] + /// + /// @return Array of String tokens + + Array split(const String& aDelimiter) const; + /// @brief Removes whitespace from both ends /// /// @return Reference to string diff --git a/src/OpenSpaceToolkit/Core/Types/String.cpp b/src/OpenSpaceToolkit/Core/Types/String.cpp index a8f143a6..59e4e825 100644 --- a/src/OpenSpaceToolkit/Core/Types/String.cpp +++ b/src/OpenSpaceToolkit/Core/Types/String.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -143,6 +144,13 @@ String String::getSubstring(const Index& aStartPosition, const Size& aLength) co return this->substr(aStartPosition, aLength); } +Array String::split(const String& aDelimiter) const +{ + Array result; + boost::split(result, *this, boost::is_any_of(aDelimiter)); + return result; +} + String& String::trim() { if (!this->empty()) diff --git a/test/OpenSpaceToolkit/Core/Types/String.test.cpp b/test/OpenSpaceToolkit/Core/Types/String.test.cpp index a528be26..5877beee 100644 --- a/test/OpenSpaceToolkit/Core/Types/String.test.cpp +++ b/test/OpenSpaceToolkit/Core/Types/String.test.cpp @@ -1,5 +1,6 @@ /// Apache License 2.0 +#include #include #include #include @@ -167,6 +168,24 @@ TEST(OpenSpaceToolkit_Core_Types_String, GetSubstring) } } +TEST(OpenSpaceToolkit_Core_Types_String, Split) +{ + using ostk::core::types::String; + using ostk::core::ctnr::Array; + + { + EXPECT_EQ(Array({"a", "b", "c", ""}), String("a,b,c,").split(",")); + EXPECT_EQ(Array({"", "a", "b", "c"}), String(",a,b,c").split(",")); + EXPECT_EQ(Array({"abc"}), String("abc").split(",")); + EXPECT_EQ(Array({"a", "c"}), String("abc").split("b")); + EXPECT_EQ(Array({"a,b", "", "c", "", ""}), String("a,b c ").split(" ")); + EXPECT_EQ(Array({"ab", "c"}), String("ab\nc").split("\n")); + EXPECT_EQ(Array({"", "", "", ""}), String("abc").split("abcd")); + EXPECT_EQ(Array({""}), String("").split("\n")); + EXPECT_EQ(Array({"abc"}), String("abc").split("")); + } +} + TEST(OpenSpaceToolkit_Core_Types_String, Trim) { using ostk::core::types::String;