From f4c68eb0a4451004ecc4153266254b1aab28476e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 17 Jul 2022 21:08:53 +0200 Subject: [PATCH] LibJS: Add PrimitiveString::is_empty() and use it If we're only interested in whether the string is empty, we can skip the conversion from UTF-16 to UTF-8. --- Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp | 9 +++++++++ Userland/Libraries/LibJS/Runtime/PrimitiveString.h | 2 ++ Userland/Libraries/LibJS/Runtime/Value.cpp | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp b/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp index 4eae78e950..5aa4f0e17f 100644 --- a/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp +++ b/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp @@ -32,6 +32,15 @@ PrimitiveString::~PrimitiveString() vm().string_cache().remove(m_utf8_string); } +bool PrimitiveString::is_empty() const +{ + if (m_has_utf16_string) + return m_utf16_string.is_empty(); + if (m_has_utf8_string) + return m_utf8_string.is_empty(); + VERIFY_NOT_REACHED(); +} + String const& PrimitiveString::string() const { if (!m_has_utf8_string) { diff --git a/Userland/Libraries/LibJS/Runtime/PrimitiveString.h b/Userland/Libraries/LibJS/Runtime/PrimitiveString.h index 49b136bcd6..1a4f4af0dc 100644 --- a/Userland/Libraries/LibJS/Runtime/PrimitiveString.h +++ b/Userland/Libraries/LibJS/Runtime/PrimitiveString.h @@ -24,6 +24,8 @@ public: PrimitiveString(PrimitiveString const&) = delete; PrimitiveString& operator=(PrimitiveString const&) = delete; + bool is_empty() const; + String const& string() const; bool has_utf8_string() const { return m_has_utf8_string; } diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index c560763156..34fb6c1395 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -382,7 +382,7 @@ bool Value::to_boolean() const return false; return m_value.as_double != 0; case Type::String: - return !m_value.as_string->string().is_empty(); + return !m_value.as_string->is_empty(); case Type::Symbol: return true; case Type::BigInt: