AK: Add a UTF-16 replacement wrapper to replace a single code unit

Just for convenience for interop with existing code.
This commit is contained in:
Timothy Flynn 2025-08-06 07:17:20 -04:00 committed by Jelle Raaijmakers
parent 9e0b1bdfca
commit 7082cafdbc
3 changed files with 15 additions and 0 deletions

View File

@ -190,6 +190,15 @@ public:
return utf16_view().to_ascii_titlecase();
}
ALWAYS_INLINE Utf16String replace(char16_t needle, Utf16View const& replacement, ReplaceMode replace_mode) const
{
auto view = utf16_view();
if (view.is_empty() || !view.contains(needle))
return *this;
return view.replace(needle, replacement, replace_mode);
}
ALWAYS_INLINE Utf16String replace(Utf16View const& needle, Utf16View const& replacement, ReplaceMode replace_mode) const
{
auto view = utf16_view();

View File

@ -99,6 +99,11 @@ Utf16String Utf16View::to_ascii_titlecase() const
return builder.to_utf16_string();
}
Utf16String Utf16View::replace(char16_t needle, Utf16View const& replacement, ReplaceMode replace_mode) const
{
return replace({ &needle, 1 }, replacement, replace_mode);
}
Utf16String Utf16View::replace(Utf16View const& needle, Utf16View const& replacement, ReplaceMode replace_mode) const
{
if (is_empty())

View File

@ -434,6 +434,7 @@ public:
return { m_string.utf16 + code_unit_offset, length_in_code_units() - code_unit_offset };
}
Utf16String replace(char16_t needle, Utf16View const& replacement, ReplaceMode) const;
Utf16String replace(Utf16View const& needle, Utf16View const& replacement, ReplaceMode) const;
Utf16String escape_html_entities() const;