Skip to content

Commit

Permalink
LibJS: Add String.prototype.lastIndexOf
Browse files Browse the repository at this point in the history
  • Loading branch information
kessejones authored and awesomekling committed May 1, 2020
1 parent 46b79ea commit 6dbb5df
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Libraries/LibJS/Runtime/StringPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}

}
1 change: 1 addition & 0 deletions Libraries/LibJS/Runtime/StringPrototype.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class StringPrototype final : public StringObject {
static Value concat(Interpreter&);
static Value includes(Interpreter&);
static Value slice(Interpreter&);
static Value last_index_of(Interpreter&);
};

}
27 changes: 27 additions & 0 deletions Libraries/LibJS/Tests/String.prototype.lastIndexOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("test-common.js");

try {
assert(String.prototype.lastIndexOf.length === 1);
assert("hello friends".lastIndexOf() === -1);
assert("hello friends".lastIndexOf("e") === 9);
assert("hello friends".lastIndexOf("e", -7) === -1);
assert("hello friends".lastIndexOf("e", 100) === 9);
assert("hello friends".lastIndexOf("") === 13);
assert("hello friends".lastIndexOf("Z") === -1);
assert("hello friends".lastIndexOf("serenity") === -1);
assert("hello friends".lastIndexOf("", 4) === 4);
assert("hello serenity friends".lastIndexOf("serenity") === 6);
assert("hello serenity friends serenity".lastIndexOf("serenity") === 23);
assert("hello serenity friends serenity".lastIndexOf("serenity", 14) === 6);
assert("".lastIndexOf("") === 0);
assert("".lastIndexOf("", 1) === 0);
assert("".lastIndexOf("", -1) === 0);
assert("hello friends serenity".lastIndexOf("h", 10) === 0);
assert("hello friends serenity".lastIndexOf("l", 4) === 3);
assert("hello friends serenity".lastIndexOf("s", 13) === 12);
assert("hello".lastIndexOf("serenity") === -1);

console.log("PASS");
} catch (err) {
console.log("FAIL: " + err);
}

0 comments on commit 6dbb5df

Please sign in to comment.