mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
AK: Add an overload of String::find_byte_offset for StringView
This commit is contained in:
parent
38b4e938b7
commit
c35b1371a3
Notes:
sideshowbarker
2024-07-17 23:02:37 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/c35b1371a3 Pull-request: https://github.com/SerenityOS/serenity/pull/17202
3 changed files with 55 additions and 2 deletions
|
@ -8,6 +8,7 @@
|
|||
#include <AK/Checked.h>
|
||||
#include <AK/FlyString.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/MemMem.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Utf8View.h>
|
||||
|
@ -328,6 +329,21 @@ Optional<size_t> String::find_byte_offset(u32 code_point, size_t from_byte_offse
|
|||
return {};
|
||||
}
|
||||
|
||||
Optional<size_t> String::find_byte_offset(StringView substring, size_t from_byte_offset) const
|
||||
{
|
||||
auto view = bytes_as_string_view();
|
||||
if (from_byte_offset >= view.length())
|
||||
return {};
|
||||
|
||||
auto index = memmem_optional(
|
||||
view.characters_without_null_termination() + from_byte_offset, view.length() - from_byte_offset,
|
||||
substring.characters_without_null_termination(), substring.length());
|
||||
|
||||
if (index.has_value())
|
||||
return *index + from_byte_offset;
|
||||
return {};
|
||||
}
|
||||
|
||||
bool String::operator==(String const& other) const
|
||||
{
|
||||
if (is_short_string())
|
||||
|
|
|
@ -133,6 +133,7 @@ public:
|
|||
ErrorOr<Vector<String>> split(u32 separator, SplitBehavior = SplitBehavior::Nothing) const;
|
||||
|
||||
Optional<size_t> find_byte_offset(u32 code_point, size_t from_byte_offset = 0) const;
|
||||
Optional<size_t> find_byte_offset(StringView substring, size_t from_byte_offset = 0) const;
|
||||
|
||||
[[nodiscard]] bool operator==(String const&) const;
|
||||
[[nodiscard]] bool operator!=(String const& other) const { return !(*this == other); }
|
||||
|
|
|
@ -320,8 +320,11 @@ TEST_CASE(find_byte_offset)
|
|||
{
|
||||
{
|
||||
String string {};
|
||||
auto index = string.find_byte_offset(0);
|
||||
EXPECT(!index.has_value());
|
||||
auto index1 = string.find_byte_offset(0);
|
||||
EXPECT(!index1.has_value());
|
||||
|
||||
auto index2 = string.find_byte_offset(""sv);
|
||||
EXPECT(!index2.has_value());
|
||||
}
|
||||
{
|
||||
auto string = MUST(String::from_utf8("foo"sv));
|
||||
|
@ -338,6 +341,21 @@ TEST_CASE(find_byte_offset)
|
|||
auto index4 = string.find_byte_offset('b');
|
||||
EXPECT(!index4.has_value());
|
||||
}
|
||||
{
|
||||
auto string = MUST(String::from_utf8("foo"sv));
|
||||
|
||||
auto index1 = string.find_byte_offset("fo"sv);
|
||||
EXPECT_EQ(index1, 0u);
|
||||
|
||||
auto index2 = string.find_byte_offset("oo"sv);
|
||||
EXPECT_EQ(index2, 1u);
|
||||
|
||||
auto index3 = string.find_byte_offset("o"sv, *index2 + 1);
|
||||
EXPECT_EQ(index3, 2u);
|
||||
|
||||
auto index4 = string.find_byte_offset("fooo"sv);
|
||||
EXPECT(!index4.has_value());
|
||||
}
|
||||
{
|
||||
auto string = MUST(String::from_utf8("ωΣωΣω"sv));
|
||||
|
||||
|
@ -356,6 +374,24 @@ TEST_CASE(find_byte_offset)
|
|||
auto index5 = string.find_byte_offset(0x03C9U, 6);
|
||||
EXPECT_EQ(index5, 8u);
|
||||
}
|
||||
{
|
||||
auto string = MUST(String::from_utf8("ωΣωΣω"sv));
|
||||
|
||||
auto index1 = string.find_byte_offset("ω"sv);
|
||||
EXPECT_EQ(index1, 0u);
|
||||
|
||||
auto index2 = string.find_byte_offset("Σ"sv);
|
||||
EXPECT_EQ(index2, 2u);
|
||||
|
||||
auto index3 = string.find_byte_offset("ω"sv, 2);
|
||||
EXPECT_EQ(index3, 4u);
|
||||
|
||||
auto index4 = string.find_byte_offset("Σ"sv, 4);
|
||||
EXPECT_EQ(index4, 6u);
|
||||
|
||||
auto index5 = string.find_byte_offset("ω"sv, 6);
|
||||
EXPECT_EQ(index5, 8u);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(repeated)
|
||||
|
|
Loading…
Reference in a new issue