mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:07:45 +00:00
LibJS: Add String.prototype.lastIndexOf
This commit is contained in:
parent
46b79eaad9
commit
6dbb5df81f
3 changed files with 62 additions and 0 deletions
|
@ -81,6 +81,7 @@ StringPrototype::StringPrototype()
|
|||
put_native_function("substring", substring, 2, attr);
|
||||
put_native_function("includes", includes, 1, attr);
|
||||
put_native_function("slice", slice, 2, attr);
|
||||
put_native_function("lastIndexOf", last_index_of, 1, attr);
|
||||
}
|
||||
|
||||
StringPrototype::~StringPrototype()
|
||||
|
@ -425,4 +426,37 @@ Value StringPrototype::slice(Interpreter& interpreter)
|
|||
return js_string(interpreter, string_part);
|
||||
}
|
||||
|
||||
Value StringPrototype::last_index_of(Interpreter& interpreter)
|
||||
{
|
||||
auto string = string_from(interpreter);
|
||||
if (string.is_null())
|
||||
return {};
|
||||
|
||||
if (interpreter.argument_count() == 0)
|
||||
return Value(-1);
|
||||
|
||||
auto search_string = interpreter.argument(0).to_string();
|
||||
if (search_string.length() > string.length())
|
||||
return Value(-1);
|
||||
|
||||
ssize_t max_index = string.length() - search_string.length();
|
||||
ssize_t from_index = max_index;
|
||||
if (interpreter.argument_count() >= 2) {
|
||||
from_index = static_cast<ssize_t>(interpreter.argument(1).to_i32());
|
||||
|
||||
if (from_index < 0)
|
||||
from_index = 0;
|
||||
if (from_index > max_index)
|
||||
from_index = max_index;
|
||||
}
|
||||
|
||||
for (ssize_t i = from_index; i >= 0; --i) {
|
||||
auto part_view = string.substring_view(i, search_string.length());
|
||||
if (part_view == search_string)
|
||||
return Value((i32)i);
|
||||
}
|
||||
|
||||
return Value(-1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -57,6 +57,7 @@ private:
|
|||
static Value concat(Interpreter&);
|
||||
static Value includes(Interpreter&);
|
||||
static Value slice(Interpreter&);
|
||||
static Value last_index_of(Interpreter&);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue