mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:57:43 +00:00
LibJS: Implement String.prototype.charCodeAt with UTF-16 code units
This commit is contained in:
parent
5d11614bc7
commit
48a28a9a73
2 changed files with 15 additions and 4 deletions
|
@ -145,15 +145,18 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::char_at)
|
|||
// 22.1.3.2 String.prototype.charCodeAt ( pos ), https://tc39.es/ecma262/#sec-string.prototype.charcodeat
|
||||
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::char_code_at)
|
||||
{
|
||||
auto string = ak_string_from(vm, global_object);
|
||||
if (!string.has_value())
|
||||
auto string = utf16_string_from(vm, global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto position = vm.argument(0).to_integer_or_infinity(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (position < 0 || position >= string->length())
|
||||
|
||||
Utf16View utf16_string_view { string };
|
||||
if (position < 0 || position >= utf16_string_view.length_in_code_units())
|
||||
return js_nan();
|
||||
return Value((*string)[position]);
|
||||
|
||||
return Value(utf16_string_view.code_unit_at(position));
|
||||
}
|
||||
|
||||
// 22.1.3.3 String.prototype.codePointAt ( pos ), https://tc39.es/ecma262/#sec-string.prototype.codepointat
|
||||
|
|
|
@ -19,3 +19,11 @@ test("basic functionality", () => {
|
|||
expect(s.charCodeAt("foo")).toBe(70);
|
||||
expect(s.charCodeAt(undefined)).toBe(70);
|
||||
});
|
||||
|
||||
test("UTF-16", () => {
|
||||
var s = "😀";
|
||||
expect(s).toHaveLength(2);
|
||||
expect(s.charCodeAt(0)).toBe(0xd83d);
|
||||
expect(s.charCodeAt(1)).toBe(0xde00);
|
||||
expect(s.charCodeAt(2)).toBe(NaN);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue