Skip to content

Commit

Permalink
Strings: Add StringView::splitBefore
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagghiu committed Apr 27, 2024
1 parent 20dddb7 commit 62c7854
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
15 changes: 15 additions & 0 deletions Libraries/Strings/StringView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ bool SC::StringView::splitAfter(const StringView stringToMatch, StringView& rema
});
}

bool SC::StringView::splitBefore(const StringView stringToMatch, StringView& stringBeforeSplit) const
{
SC_ASSERT_RELEASE(hasCompatibleEncoding(stringToMatch));
return withIterator(
[&](auto it)
{
if (it.advanceBeforeFinding(stringToMatch.getIterator<decltype(it)>()))
{
stringBeforeSplit = StringView::fromIteratorFromStart(it);
return true;
}
return false;
});
}

bool SC::StringView::containsCodePoint(StringCodePoint c) const
{
return withIterator([c](auto it) { return it.advanceUntilMatches(c); });
Expand Down
20 changes: 17 additions & 3 deletions Libraries/Strings/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ struct SC::StringView
/// @endcode
[[nodiscard]] bool containsString(const StringView str) const;

/// @brief Splits the remaining part of the strin after matching str
/// @brief Returns the remaining part of the string after matching stringToMatch
/// @param stringToMatch String to match inside the source string
/// @param remainingAfterSplit Portion of this StringView AFTER first match of stringtoMatch
/// @param remainingAfterSplit Portion of this StringView AFTER first match of stringToMatch (excluding the match)
/// @return Returns `true` if stringToMatch has been found and split has been written to remainingAfterSplit
///
/// Example:
Expand All @@ -303,6 +303,20 @@ struct SC::StringView
/// @endcode
[[nodiscard]] bool splitAfter(const StringView stringToMatch, StringView& remainingAfterSplit) const;

/// @brief Returns the part of the string before matching stringToMatch
/// @param stringToMatch String to match inside the source string
/// @param stringBeforeSplit Portion of this StringView BEFORE first match of stringToMatch (excluding the match)
/// @return Returns `true` if stringToMatch has been found and split has been written to remainingAfterSplit
///
/// Example:
/// @code{.cpp}
/// StringView str("KEY = VALUE");
/// StringView split;
/// SC_TEST_EXPECT(str.splitBefore(" = ", split));
/// SC_TEST_EXPECT(split == "KEY");
/// @endcode
[[nodiscard]] bool splitBefore(const StringView stringToMatch, StringView& stringBeforeSplit) const;

/// @brief Check if StringView contains given utf code point
/// @param c The utf code point to check against
/// @return Returns `true` if this StringView contains code point c
Expand Down Expand Up @@ -421,7 +435,7 @@ struct SC::StringView
/// @endcode
[[nodiscard]] StringView trimAnyOf(Span<const StringCodePoint> codePoints) const;

/// @brief Returns a shortened StringView without starting / ending utf code points matching {'\r', '\n', '\t', ' '}
/// @brief Returns a shortened StringView without starting/ending utf code points inside {'\\r', '\\n', '\\t', ' '}
/// @return The trimmed StringView
///
/// Example:
Expand Down
6 changes: 6 additions & 0 deletions Libraries/Strings/Tests/StringViewTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ struct SC::StringViewTest : public SC::TestCase
SC_TEST_EXPECT(str.splitAfter(" = ", split));
SC_TEST_EXPECT(split == "VALUE");
}
{
StringView str("KEY = VALUE");
StringView split;
SC_TEST_EXPECT(str.splitBefore(" = ", split));
SC_TEST_EXPECT(split == "KEY");
}
}
if (test_section("isInteger"))
{
Expand Down

0 comments on commit 62c7854

Please sign in to comment.