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

LibJS: Trim initial whitespace in parseFloat

This commit is contained in:
Idan Horowitz 2021-06-06 02:32:14 +03:00 committed by Linus Groh
parent bda32e9440
commit bbf75d0bea

View file

@ -202,12 +202,13 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_float)
{ {
if (vm.argument(0).is_number()) if (vm.argument(0).is_number())
return vm.argument(0); return vm.argument(0);
auto string = vm.argument(0).to_string(global_object); auto input_string = vm.argument(0).to_string(global_object);
if (vm.exception()) if (vm.exception())
return {}; return {};
for (size_t length = string.length(); length > 0; --length) { auto trimmed_string = input_string.trim_whitespace(TrimMode::Left);
for (size_t length = trimmed_string.length(); length > 0; --length) {
// This can't throw, so no exception check is fine. // This can't throw, so no exception check is fine.
auto number = Value(js_string(vm, string.substring(0, length))).to_number(global_object); auto number = Value(js_string(vm, trimmed_string.substring(0, length))).to_number(global_object);
if (!number.is_nan()) if (!number.is_nan())
return number; return number;
} }