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

LibJS: Add String.prototype.substring

This commit is contained in:
Kesse Jones 2020-04-16 13:15:03 -03:00 committed by Andreas Kling
parent 60d1ef6af4
commit b0b204822f
3 changed files with 68 additions and 0 deletions

View file

@ -56,6 +56,7 @@ StringPrototype::StringPrototype()
put_native_function("trimStart", trim_start, 0);
put_native_function("trimEnd", trim_end, 0);
put_native_function("concat", concat, 1);
put_native_function("substring", substring, 2);
}
StringPrototype::~StringPrototype()
@ -326,4 +327,51 @@ Value StringPrototype::concat(Interpreter& interpreter)
return js_string(interpreter, builder.to_string());
}
Value StringPrototype::substring(Interpreter& interpreter)
{
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
if (!this_object)
return {};
auto& string = this_object->to_string().as_string()->string();
if (interpreter.argument_count() == 0)
return js_string(interpreter, string);
i32 string_length = static_cast<i32>(string.length());
i32 index_start = interpreter.argument(0).to_number().to_i32();
i32 index_end = string_length;
if (index_start > string_length)
index_start = string_length;
else if (index_start < 0)
index_start = 0;
if (interpreter.argument_count() >= 2) {
index_end = interpreter.argument(1).to_number().to_i32();
if (index_end > string_length)
index_end = string_length;
else if (index_end < 0)
index_end = 0;
}
if (index_start == index_end)
return js_string(interpreter, String(""));
if (index_start > index_end) {
if (interpreter.argument_count() == 1) {
return js_string(interpreter, String(""));
} else {
i32 temp_index_start = index_start;
index_start = index_end;
index_end = temp_index_start;
}
}
auto part_length = index_end - index_start;
auto string_part = string.substring(index_start, part_length);
return js_string(interpreter, string_part);
}
}

View file

@ -47,6 +47,7 @@ private:
static Value to_string(Interpreter&);
static Value pad_start(Interpreter&);
static Value pad_end(Interpreter&);
static Value substring(Interpreter&);
static Value length_getter(Interpreter&);