diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-07-16 13:04:46 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-17 16:59:59 +0100 |
commit | a2e734d202e10ec9ef61095bf2e05f634873d441 (patch) | |
tree | 17e9b7f970674f7a0d3671078ce92b9f07f7c6d7 | |
parent | 87848cdf7d66e76da1331477ffa73a0141d480d7 (diff) | |
download | serenity-a2e734d202e10ec9ef61095bf2e05f634873d441.zip |
LibJS: Report string length as the code point length, not byte length
For example, U+180E is 3 bytes, but should have a string length of 1.
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/StringObject.cpp | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/String/String.js | 7 |
2 files changed, 9 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/StringObject.cpp b/Userland/Libraries/LibJS/Runtime/StringObject.cpp index a1f4186c4c..a941744060 100644 --- a/Userland/Libraries/LibJS/Runtime/StringObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringObject.cpp @@ -5,6 +5,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <AK/Utf8View.h> #include <LibJS/Runtime/AbstractOperations.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/PrimitiveString.h> @@ -33,7 +34,7 @@ void StringObject::initialize(GlobalObject& global_object) { auto& vm = this->vm(); Object::initialize(global_object); - define_direct_property(vm.names.length, Value(m_string.string().length()), 0); + define_direct_property(vm.names.length, Value(Utf8View(m_string.string()).length()), 0); } void StringObject::visit_edges(Cell::Visitor& visitor) diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.js index 687e0929d4..ba076bd5b4 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/String/String.js +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.js @@ -7,3 +7,10 @@ test("typeof", () => { expect(typeof String()).toBe("string"); expect(typeof new String()).toBe("object"); }); + +test("length", () => { + expect(new String().length).toBe(0); + expect(new String("a").length).toBe(1); + expect(new String("\u180E").length).toBe(1); + expect(new String("\uDBFF\uDFFF").length).toBe(2); +}); |