From 04fe0c6aecb22af537db2b87db2efd981f85a783 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 22 Jul 2025 15:13:43 -0400 Subject: [PATCH] LibJS: Create match indices based on code unit length In Unicode mode, we were mixing code units (start_index) with code point length (end_index). --- Libraries/LibJS/Runtime/RegExpPrototype.cpp | 2 +- .../Tests/builtins/RegExp/RegExp.prototype.hasIndices.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Libraries/LibJS/Runtime/RegExpPrototype.cpp index 0b5c93b72f5..f033e36ae6c 100644 --- a/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -76,7 +76,7 @@ static ThrowCompletionOr increment_last_index(VM& vm, Object& regexp_objec struct Match { static Match create(regex::Match const& match) { - return { match.global_offset, match.global_offset + match.view.length() }; + return { match.global_offset, match.global_offset + match.view.length_in_code_units() }; } size_t start_index { 0 }; diff --git a/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.hasIndices.js b/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.hasIndices.js index c2761b19711..a264e7be723 100644 --- a/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.hasIndices.js +++ b/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.hasIndices.js @@ -50,4 +50,8 @@ test("basic functionality", () => { var result = regex.exec("let foo").indices; expect(result.groups).toEqual({ keyword: [0, 3], id: [4, 7] }); } + + regex = /🍕/du; + expect(regex.hasIndices).toBeTrue(); + expect(regex.exec("🍕").indices[0]).toEqual([0, 2]); });