1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 16:07:46 +00:00

LibJS: Implement Unicode aware String.prototype.to{Upper,Lower}Case

This commit is contained in:
Timothy Flynn 2021-07-25 15:17:38 -04:00 committed by Linus Groh
parent 98d8274040
commit 2e3a5b884c
4 changed files with 30 additions and 9 deletions

View file

@ -165,4 +165,4 @@ set(SOURCES
)
serenity_lib(LibJS js)
target_link_libraries(LibJS LibM LibCore LibCrypto LibRegex LibSyntax)
target_link_libraries(LibJS LibM LibCore LibCrypto LibRegex LibSyntax LibUnicode)

View file

@ -20,6 +20,7 @@
#include <LibJS/Runtime/StringObject.h>
#include <LibJS/Runtime/StringPrototype.h>
#include <LibJS/Runtime/Value.h>
#include <LibUnicode/CharacterTypes.h>
#include <string.h>
namespace JS {
@ -380,21 +381,33 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::index_of)
// 22.1.3.26 String.prototype.toLowerCase ( ), https://tc39.es/ecma262/#sec-string.prototype.tolowercase
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_lowercase)
{
// FIXME: Implement Unicode case folding: https://www.unicode.org/Public/13.0.0/ucd/CaseFolding.txt
auto string = ak_string_from(vm, global_object);
if (!string.has_value())
auto string = utf16_string_from(vm, global_object);
if (vm.exception())
return {};
return js_string(vm, string->to_lowercase());
Utf16View utf16_string_view { string };
StringBuilder builder;
for (auto code_point : utf16_string_view)
builder.append_code_point(Unicode::to_unicode_lowercase(code_point));
return js_string(vm, builder.to_string());
}
// 22.1.3.28 String.prototype.toUpperCase ( ), https://tc39.es/ecma262/#sec-string.prototype.touppercase
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::to_uppercase)
{
// FIXME: Implement Unicode case folding: https://www.unicode.org/Public/13.0.0/ucd/CaseFolding.txt
auto string = ak_string_from(vm, global_object);
if (!string.has_value())
auto string = utf16_string_from(vm, global_object);
if (vm.exception())
return {};
return js_string(vm, string->to_uppercase());
Utf16View utf16_string_view { string };
StringBuilder builder;
for (auto code_point : utf16_string_view)
builder.append_code_point(Unicode::to_unicode_uppercase(code_point));
return js_string(vm, builder.to_string());
}
// 22.1.3.27 String.prototype.toString ( ), https://tc39.es/ecma262/#sec-string.prototype.tostring

View file

@ -1,6 +1,10 @@
test("basic functionality", () => {
expect(String.prototype.toLowerCase).toHaveLength(0);
expect("ω".toLowerCase()).toBe("ω");
expect("Ω".toLowerCase()).toBe("ω");
expect("😀".toLowerCase()).toBe("😀");
expect("foo".toLowerCase()).toBe("foo");
expect("Foo".toLowerCase()).toBe("foo");
expect("FOO".toLowerCase()).toBe("foo");

View file

@ -1,6 +1,10 @@
test("basic functionality", () => {
expect(String.prototype.toUpperCase).toHaveLength(0);
expect("ω".toUpperCase()).toBe("Ω");
expect("Ω".toUpperCase()).toBe("Ω");
expect("😀".toUpperCase()).toBe("😀");
expect("foo".toUpperCase()).toBe("FOO");
expect("Foo".toUpperCase()).toBe("FOO");
expect("FOO".toUpperCase()).toBe("FOO");