Commit fa282812 authored by jgruber's avatar jgruber Committed by Commit bot

[regexp] Match spec semantics in AdvanceStringIndex

Let AdvanceStringIndex return the incremented index instead of the
increment, and adjust all use sites.

BUG=v8:5339

Review-Url: https://codereview.chromium.org/2415383002
Cr-Commit-Position: refs/heads/master@{#40310}
parent b182ab83
......@@ -1372,8 +1372,8 @@ BUILTIN(RegExpPrototypeSplit) {
factory->undefined_value()));
if (result->IsNull(isolate)) {
string_index += RegExpUtils::AdvanceStringIndex(isolate, string,
string_index, unicode);
string_index = RegExpUtils::AdvanceStringIndex(isolate, string,
string_index, unicode);
continue;
}
......@@ -1388,8 +1388,8 @@ BUILTIN(RegExpPrototypeSplit) {
const int end = std::min(last_index, length);
if (end == prev_string_index) {
string_index += RegExpUtils::AdvanceStringIndex(isolate, string,
string_index, unicode);
string_index = RegExpUtils::AdvanceStringIndex(isolate, string,
string_index, unicode);
continue;
}
......
......@@ -145,23 +145,19 @@ bool RegExpUtils::IsBuiltinExec(Handle<Object> exec) {
return (code->builtin_index() == Builtins::kRegExpPrototypeExec);
}
// ES#sec-advancestringindex
// AdvanceStringIndex ( S, index, unicode )
int RegExpUtils::AdvanceStringIndex(Isolate* isolate, Handle<String> string,
int index, bool unicode) {
int increment = 1;
if (unicode && index < string->length()) {
const uint16_t first = string->Get(index);
if (first >= 0xD800 && first <= 0xDBFF && string->length() > index + 1) {
const uint16_t second = string->Get(index + 1);
if (second >= 0xDC00 && second <= 0xDFFF) {
increment = 2;
return index + 2;
}
}
}
return increment;
return index + 1;
}
MaybeHandle<Object> RegExpUtils::SetAdvancedStringIndex(
......@@ -178,7 +174,7 @@ MaybeHandle<Object> RegExpUtils::SetAdvancedStringIndex(
const int last_index = Handle<Smi>::cast(last_index_obj)->value();
const int new_last_index =
last_index + AdvanceStringIndex(isolate, string, last_index, unicode);
AdvanceStringIndex(isolate, string, last_index, unicode);
return SetLastIndex(isolate, regexp, new_last_index);
}
......
......@@ -37,7 +37,8 @@ class RegExpUtils : public AllStatic {
// Checks whether exec is identical to the initial RegExp.prototype.exec.
static bool IsBuiltinExec(Handle<Object> exec);
// Generic index increment methods respecting unicode surrogate pairs.
// ES#sec-advancestringindex
// AdvanceStringIndex ( S, index, unicode )
static int AdvanceStringIndex(Isolate* isolate, Handle<String> string,
int index, bool unicode);
static MaybeHandle<Object> SetAdvancedStringIndex(Isolate* isolate,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment