diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index 427d03ef88c..1d3ca6fdf26 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -2390,36 +2390,18 @@ ThrowCompletionOr is_less_than(VM& vm, Value lhs, Value rhs, bool left // 3. If px is a String and py is a String, then if (x_primitive.is_string() && y_primitive.is_string()) { - auto x_string = x_primitive.as_string().utf8_string_view(); - auto y_string = y_primitive.as_string().utf8_string_view(); - - Utf8View x_code_points { x_string }; - Utf8View y_code_points { y_string }; + auto x_string = x_primitive.as_string().utf16_string_view(); + auto y_string = y_primitive.as_string().utf16_string_view(); // a. Let lx be the length of px. // b. Let ly be the length of py. // c. For each integer i such that 0 ≤ i < min(lx, ly), in ascending order, do - for (auto k = x_code_points.begin(), l = y_code_points.begin(); - k != x_code_points.end() && l != y_code_points.end(); - ++k, ++l) { - // i. Let cx be the integer that is the numeric value of the code unit at index i within px. - // ii. Let cy be the integer that is the numeric value of the code unit at index i within py. - if (*k != *l) { - // iii. If cx < cy, return true. - if (*k < *l) { - return TriState::True; - } - // iv. If cx > cy, return false. - else { - return TriState::False; - } - } - } - + // i. Let cx be the integer that is the numeric value of the code unit at index i within px. + // ii. Let cy be the integer that is the numeric value of the code unit at index i within py. + // iii. If cx < cy, return true. + // iv. If cx > cy, return false. // d. If lx < ly, return true. Otherwise, return false. - return x_code_points.length() < y_code_points.length() - ? TriState::True - : TriState::False; + return x_string.is_code_unit_less_than(y_string) ? TriState::True : TriState::False; } // 4. Else, diff --git a/Libraries/LibJS/Tests/builtins/String/String.js b/Libraries/LibJS/Tests/builtins/String/String.js index 5050f17333e..4b8b9de8dc5 100644 --- a/Libraries/LibJS/Tests/builtins/String/String.js +++ b/Libraries/LibJS/Tests/builtins/String/String.js @@ -40,3 +40,18 @@ test("properties", () => { expect(Object.getOwnPropertyNames("abc")).toEqual(["0", "1", "2", "length"]); expect(Object.getOwnPropertyNames("😀")).toEqual(["0", "1", "length"]); }); + +test("less than", () => { + expect("a" < "").toBe(false); + expect("a" < "b").toBe(true); + expect("a" < "aa").toBe(true); + expect("aa" < "a").toBe(false); + expect("abc" < "abd").toBe(true); + expect("abc" < "abcd").toBe(true); + expect("😀" < "😁").toBe(true); + expect("" < "😁").toBe(true); + expect("😁" < "").toBe(false); + expect("a" < "A").toBe(false); + expect("a" < "a").toBe(false); + expect("~" < "😀").toBe(false); +});