mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 04:09:13 +00:00
LibJS: Don't convert to UTF-8 in order to compare two UTF-16 strings
If we have two PrimitiveString objects that are both backed by UTF-16 data, we don't have to convert them to UTF-8 for equality checking. Just compare the underlying UTF-16 data. :^)
This commit is contained in:
parent
e80d1c1a86
commit
d78e3590d5
Notes:
github-actions[bot]
2025-04-12 09:08:53 +00:00
Author: https://github.com/awesomekling
Commit: d78e3590d5
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4328
3 changed files with 14 additions and 1 deletions
|
@ -106,6 +106,17 @@ Utf16View PrimitiveString::utf16_string_view() const
|
||||||
return m_utf16_string->view();
|
return m_utf16_string->view();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool PrimitiveString::operator==(PrimitiveString const& other) const
|
||||||
|
{
|
||||||
|
if (this == &other)
|
||||||
|
return true;
|
||||||
|
if (m_utf8_string.has_value() && other.m_utf8_string.has_value())
|
||||||
|
return m_utf8_string->bytes_as_string_view() == other.m_utf8_string->bytes_as_string_view();
|
||||||
|
if (m_utf16_string.has_value() && other.m_utf16_string.has_value())
|
||||||
|
return m_utf16_string->string() == other.m_utf16_string->string();
|
||||||
|
return utf8_string_view() == other.utf8_string_view();
|
||||||
|
}
|
||||||
|
|
||||||
ThrowCompletionOr<Optional<Value>> PrimitiveString::get(VM& vm, PropertyKey const& property_key) const
|
ThrowCompletionOr<Optional<Value>> PrimitiveString::get(VM& vm, PropertyKey const& property_key) const
|
||||||
{
|
{
|
||||||
if (property_key.is_symbol())
|
if (property_key.is_symbol())
|
||||||
|
|
|
@ -47,6 +47,8 @@ public:
|
||||||
|
|
||||||
ThrowCompletionOr<Optional<Value>> get(VM&, PropertyKey const&) const;
|
ThrowCompletionOr<Optional<Value>> get(VM&, PropertyKey const&) const;
|
||||||
|
|
||||||
|
[[nodiscard]] bool operator==(PrimitiveString const&) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
enum class RopeTag { Rope };
|
enum class RopeTag { Rope };
|
||||||
explicit PrimitiveString(RopeTag)
|
explicit PrimitiveString(RopeTag)
|
||||||
|
|
|
@ -2225,7 +2225,7 @@ bool same_value_non_number(Value lhs, Value rhs)
|
||||||
// 5. If x is a String, then
|
// 5. If x is a String, then
|
||||||
if (lhs.is_string()) {
|
if (lhs.is_string()) {
|
||||||
// a. If x and y are exactly the same sequence of code units (same length and same code units at corresponding indices), return true; otherwise, return false.
|
// a. If x and y are exactly the same sequence of code units (same length and same code units at corresponding indices), return true; otherwise, return false.
|
||||||
return lhs.as_string().utf8_string_view() == rhs.as_string().utf8_string_view();
|
return lhs.as_string() == rhs.as_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. If x is undefined, return true.
|
// 3. If x is undefined, return true.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue