Skip to content

Commit

Permalink
feat: String::split function (#120)
Browse files Browse the repository at this point in the history
* feat: add string split function

* test: add a few more test cases

* chore: change comment a little

* style: formatting

---------

Co-authored-by: vishwa2710 <[email protected]>
  • Loading branch information
kyle-cochran and vishwa2710 authored Jun 23, 2023
1 parent 58f780a commit c370cf4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/OpenSpaceToolkit/Core/Types/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ namespace ostk
{
namespace core
{

// Forward declare Array to avoid circular dependency
namespace ctnr
{
template <typename T>
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
Expand Down Expand Up @@ -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<String> split(const String& aDelimiter) const;

/// @brief Removes whitespace from both ends
///
/// @return Reference to string
Expand Down
8 changes: 8 additions & 0 deletions src/OpenSpaceToolkit/Core/Types/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <boost/algorithm/string.hpp>

#include <OpenSpaceToolkit/Core/Containers/Array.hpp>
#include <OpenSpaceToolkit/Core/Error.hpp>
#include <OpenSpaceToolkit/Core/Types/Integer.hpp>
#include <OpenSpaceToolkit/Core/Types/String.hpp>
Expand Down Expand Up @@ -143,6 +144,13 @@ String String::getSubstring(const Index& aStartPosition, const Size& aLength) co
return this->substr(aStartPosition, aLength);
}

Array<String> String::split(const String& aDelimiter) const
{
Array<String> result;
boost::split(result, *this, boost::is_any_of(aDelimiter));
return result;
}

String& String::trim()
{
if (!this->empty())
Expand Down
19 changes: 19 additions & 0 deletions test/OpenSpaceToolkit/Core/Types/String.test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// Apache License 2.0

#include <OpenSpaceToolkit/Core/Containers/Array.hpp>
#include <OpenSpaceToolkit/Core/Types/Integer.hpp>
#include <OpenSpaceToolkit/Core/Types/Real.hpp>
#include <OpenSpaceToolkit/Core/Types/String.hpp>
Expand Down Expand Up @@ -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<String>({"a", "b", "c", ""}), String("a,b,c,").split(","));
EXPECT_EQ(Array<String>({"", "a", "b", "c"}), String(",a,b,c").split(","));
EXPECT_EQ(Array<String>({"abc"}), String("abc").split(","));
EXPECT_EQ(Array<String>({"a", "c"}), String("abc").split("b"));
EXPECT_EQ(Array<String>({"a,b", "", "c", "", ""}), String("a,b c ").split(" "));
EXPECT_EQ(Array<String>({"ab", "c"}), String("ab\nc").split("\n"));
EXPECT_EQ(Array<String>({"", "", "", ""}), String("abc").split("abcd"));
EXPECT_EQ(Array<String>({""}), String("").split("\n"));
EXPECT_EQ(Array<String>({"abc"}), String("abc").split(""));
}
}

TEST(OpenSpaceToolkit_Core_Types_String, Trim)
{
using ostk::core::types::String;
Expand Down

0 comments on commit c370cf4

Please sign in to comment.