Skip to content

Commit

Permalink
LibJS: Add String.prototype.split using the @@split methods on object
Browse files Browse the repository at this point in the history
  • Loading branch information
davidot authored and linusg committed Jun 30, 2021
1 parent 3666889 commit 7a3b057
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,58 +575,69 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::slice)
// 22.1.3.21 String.prototype.split ( separator, limit ), https://tc39.es/ecma262/#sec-string.prototype.split
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
{
// FIXME Implement the @@split part
auto separator_argument = vm.argument(0);
auto limit_argument = vm.argument(1);

auto string = ak_string_from(vm, global_object);
if (!string.has_value())
auto this_value = require_object_coercible(global_object, vm.this_value(global_object));
if (vm.exception())
return {};

if (!separator_argument.is_nullish()) {
auto splitter = separator_argument.get_method(global_object, *vm.well_known_symbol_split());
if (vm.exception())
return {};
if (splitter)
return vm.call(*splitter, separator_argument, this_value, limit_argument);
}

auto string = this_value.to_string(global_object);

auto* result = Array::create(global_object);
size_t result_len = 0;

auto limit = NumericLimits<u32>::max();
if (!vm.argument(1).is_undefined()) {
limit = vm.argument(1).to_u32(global_object);
limit = limit_argument.to_u32(global_object);
if (vm.exception())
return {};
}

auto separator = vm.argument(0).to_string(global_object);
auto separator = separator_argument.to_string(global_object);
if (vm.exception())
return {};

if (limit == 0)
return result;

if (vm.argument(0).is_undefined()) {
result->define_property(0, js_string(vm, *string));
result->define_property(0, js_string(vm, string));
return result;
}

auto len = string->length();
auto len = string.length();
auto separator_len = separator.length();
if (len == 0) {
if (separator_len > 0)
result->define_property(0, js_string(vm, *string));
result->define_property(0, js_string(vm, string));
return result;
}

size_t start = 0;
auto pos = start;
if (separator_len == 0) {
for (pos = 0; pos < len; pos++)
result->define_property(pos, js_string(vm, string->substring(pos, 1)));
result->define_property(pos, js_string(vm, string.substring(pos, 1)));
return result;
}

while (pos != len) {
auto e = split_match(*string, pos, separator);
auto e = split_match(string, pos, separator);
if (!e.has_value()) {
pos += 1;
continue;
}

auto segment = string->substring_view(start, pos - start);
auto segment = string.substring_view(start, pos - start);
result->define_property(result_len, js_string(vm, segment));
result_len++;
if (result_len == limit)
Expand All @@ -635,7 +646,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
pos = start;
}

auto rest = string->substring(start, len - start);
auto rest = string.substring(start, len - start);
result->define_property(result_len, js_string(vm, rest));

return result;
Expand Down

0 comments on commit 7a3b057

Please sign in to comment.