Skip to content

Commit

Permalink
BUG: StringUtilities::split-Add Additional Edge Case Checks(#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungbq authored Mar 20, 2024
1 parent 5ed1cd9 commit 3a9c87d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/simplnx/DataStructure/DataPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ std::optional<DataPath> DataPath::FromString(std::string_view inputPath, char de
{
return DataPath{};
}
if(inputPath.size() == 1 && inputPath[0] == delimiter)
{
return DataPath{};
}
auto parts = StringUtilities::split(inputPath, delimiter);
if(parts.empty())
{
Expand Down
10 changes: 10 additions & 0 deletions src/simplnx/Utilities/StringUtilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ using SplitAllowEmptyRightAnalyze = SplitTypeOptions<true, false, true>;
template <class SplitTypeOptionsV = SplitIgnoreEmpty>
inline std::vector<std::string> optimized_split(std::string_view str, nonstd::span<const char> delimiters)
{
if(str.empty())
{
return {};
}
auto endPos = str.end();
auto startPos = str.begin();

Expand Down Expand Up @@ -141,6 +145,12 @@ inline std::vector<std::string> optimized_split(std::string_view str, nonstd::sp

tokens.shrink_to_fit();

// No Delimiters found
if(tokens.empty())
{
tokens.emplace_back(str);
}

return tokens;
}
} // namespace
Expand Down
63 changes: 63 additions & 0 deletions test/StringUtilitiesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,69 @@

using namespace nx::core;

TEST_CASE("Empty Input Utility Function Test: split(str, char), split(str, char, bool), specific_split(str, char, StringUtilities::SplitType)")
{
std::string emptyInput = "";
std::string noDelimiterInput = "ThisIsABaselineTest";

std::vector<std::string> result;
std::array<char, 1> k_Delimiter = {'|'};

// Case 1
result = StringUtilities::split(emptyInput, k_Delimiter[0]);

REQUIRE(result.empty());

result = StringUtilities::split(noDelimiterInput, k_Delimiter[0]);

REQUIRE(result == std::vector<std::string>{noDelimiterInput});

// Case 2
result = StringUtilities::split(emptyInput, k_Delimiter, true);

REQUIRE(result.empty());

result = StringUtilities::split(noDelimiterInput, k_Delimiter, true);

REQUIRE(result == std::vector<std::string>{noDelimiterInput});

// Case 4
result = StringUtilities::specific_split(emptyInput, k_Delimiter, StringUtilities::SplitType::OnlyConsecutive);

REQUIRE(result.empty());

result = StringUtilities::specific_split(noDelimiterInput, k_Delimiter, StringUtilities::SplitType::OnlyConsecutive);

REQUIRE(result == std::vector<std::string>{noDelimiterInput});

// Case 5
result = StringUtilities::specific_split(emptyInput, k_Delimiter, StringUtilities::SplitType::NoStripIgnoreConsecutive);

REQUIRE(result.empty());

result = StringUtilities::specific_split(noDelimiterInput, k_Delimiter, StringUtilities::SplitType::NoStripIgnoreConsecutive);

REQUIRE(result == std::vector<std::string>{noDelimiterInput});

// Case 6
result = StringUtilities::specific_split(emptyInput, k_Delimiter, StringUtilities::SplitType::AllowEmptyLeftAnalyze);

REQUIRE(result.empty());

result = StringUtilities::specific_split(noDelimiterInput, k_Delimiter, StringUtilities::SplitType::AllowEmptyLeftAnalyze);

REQUIRE(result == std::vector<std::string>{noDelimiterInput});

// Case 7
result = StringUtilities::specific_split(emptyInput, k_Delimiter, StringUtilities::SplitType::AllowEmptyRightAnalyze);

REQUIRE(result.empty());

result = StringUtilities::specific_split(noDelimiterInput, k_Delimiter, StringUtilities::SplitType::AllowEmptyRightAnalyze);

REQUIRE(result == std::vector<std::string>{noDelimiterInput});
}

TEST_CASE("Utility Function Test: split(str, char)")
{
// Case 1
Expand Down

0 comments on commit 3a9c87d

Please sign in to comment.