LibJS: Add some missing |this| checks in StringPrototype
Also some style tweaks
This commit is contained in:
parent
9eb9b46640
commit
1108dd1659
Notes:
sideshowbarker
2024-07-19 07:33:44 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/1108dd16591
2 changed files with 19 additions and 17 deletions
Libraries/LibJS/Runtime
|
@ -245,9 +245,9 @@ enum class TrimMode {
|
|||
Both
|
||||
};
|
||||
|
||||
static Value trim_string(Interpreter& interpreter, Object* object, TrimMode mode)
|
||||
static Value trim_string(Interpreter& interpreter, const Object& object, TrimMode mode)
|
||||
{
|
||||
auto& string = object->to_string().as_string()->string();
|
||||
auto& string = object.to_string().as_string()->string();
|
||||
|
||||
size_t substring_start = 0;
|
||||
size_t substring_length = string.length();
|
||||
|
@ -258,7 +258,7 @@ static Value trim_string(Interpreter& interpreter, Object* object, TrimMode mode
|
|||
|
||||
if (mode == TrimMode::Left || mode == TrimMode::Both) {
|
||||
for (size_t i = 0; i < string.length(); ++i) {
|
||||
if (!is_white_space_character(string.characters()[i])) {
|
||||
if (!is_white_space_character(string[i])) {
|
||||
substring_start = i;
|
||||
substring_length -= substring_start;
|
||||
break;
|
||||
|
@ -267,12 +267,12 @@ static Value trim_string(Interpreter& interpreter, Object* object, TrimMode mode
|
|||
}
|
||||
|
||||
if (substring_length == 0)
|
||||
return js_string(interpreter, String());
|
||||
return js_string(interpreter, String(""));
|
||||
|
||||
if (mode == TrimMode::Right || mode == TrimMode::Both) {
|
||||
size_t count = 0;
|
||||
for (size_t i = string.length() - 1; i > 0; --i) {
|
||||
if (!is_white_space_character(string.characters()[i])) {
|
||||
if (!is_white_space_character(string[i])) {
|
||||
substring_length -= count;
|
||||
break;
|
||||
}
|
||||
|
@ -280,29 +280,31 @@ static Value trim_string(Interpreter& interpreter, Object* object, TrimMode mode
|
|||
}
|
||||
}
|
||||
|
||||
auto substring = string.substring(substring_start, substring_length);
|
||||
return js_string(interpreter, substring);
|
||||
return js_string(interpreter, string.substring(substring_start, substring_length));
|
||||
}
|
||||
|
||||
Value StringPrototype::trim(Interpreter& interpreter)
|
||||
{
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
||||
|
||||
return trim_string(interpreter, this_object, TrimMode::Both);
|
||||
if (!this_object)
|
||||
return {};
|
||||
return trim_string(interpreter, *this_object, TrimMode::Both);
|
||||
}
|
||||
|
||||
Value StringPrototype::trimStart(Interpreter& interpreter)
|
||||
Value StringPrototype::trim_start(Interpreter& interpreter)
|
||||
{
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
||||
|
||||
return trim_string(interpreter, this_object, TrimMode::Left);
|
||||
if (!this_object)
|
||||
return {};
|
||||
return trim_string(interpreter, *this_object, TrimMode::Left);
|
||||
}
|
||||
|
||||
Value StringPrototype::trimEnd(Interpreter& interpreter)
|
||||
Value StringPrototype::trim_end(Interpreter& interpreter)
|
||||
{
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
||||
|
||||
return trim_string(interpreter, this_object, TrimMode::Right);
|
||||
if (!this_object)
|
||||
return {};
|
||||
return trim_string(interpreter, *this_object, TrimMode::Right);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -51,8 +51,8 @@ private:
|
|||
static Value length_getter(Interpreter&);
|
||||
|
||||
static Value trim(Interpreter&);
|
||||
static Value trimStart(Interpreter&);
|
||||
static Value trimEnd(Interpreter&);
|
||||
static Value trim_start(Interpreter&);
|
||||
static Value trim_end(Interpreter&);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue