Skip to content

Commit ed40ab1

Browse files
schuayCommit Bot
authored andcommitted
[regexp] Fix the order of named captures on the groups object
Named capture properties on the groups object should be ordered by the capture index (and not alpha-sorted). This was accidentally broken in https://crrev.com/c/1687413. Bug: v8:9822,v8:9423 Change-Id: Iac6f866f077a1b7ce557ba47e8ba5d7e7014b3ce Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1864829 Auto-Submit: Jakob Gruber <[email protected]> Reviewed-by: Peter Marshall <[email protected]> Commit-Queue: Peter Marshall <[email protected]> Cr-Commit-Position: refs/heads/master@{#64306}
1 parent 69efc4c commit ed40ab1

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

src/regexp/regexp-ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class RegExpCapture final : public RegExpTree {
477477
int max_match() override { return body_->max_match(); }
478478
RegExpTree* body() { return body_; }
479479
void set_body(RegExpTree* body) { body_ = body; }
480-
int index() { return index_; }
480+
int index() const { return index_; }
481481
const ZoneVector<uc16>* name() const { return name_; }
482482
void set_name(const ZoneVector<uc16>* name) { name_ = name; }
483483
static int StartRegister(int index) { return index * 2; }

src/regexp/regexp-parser.cc

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,18 +987,39 @@ RegExpCapture* RegExpParser::GetCapture(int index) {
987987
return captures_->at(index - 1);
988988
}
989989

990+
namespace {
991+
992+
struct RegExpCaptureIndexLess {
993+
bool operator()(const RegExpCapture* lhs, const RegExpCapture* rhs) const {
994+
DCHECK_NOT_NULL(lhs);
995+
DCHECK_NOT_NULL(rhs);
996+
return lhs->index() < rhs->index();
997+
}
998+
};
999+
1000+
} // namespace
1001+
9901002
Handle<FixedArray> RegExpParser::CreateCaptureNameMap() {
9911003
if (named_captures_ == nullptr || named_captures_->empty()) {
9921004
return Handle<FixedArray>();
9931005
}
9941006

1007+
// Named captures are sorted by name (because the set is used to ensure
1008+
// name uniqueness). But the capture name map must to be sorted by index.
1009+
1010+
ZoneVector<RegExpCapture*> sorted_named_captures(
1011+
named_captures_->begin(), named_captures_->end(), zone());
1012+
std::sort(sorted_named_captures.begin(), sorted_named_captures.end(),
1013+
RegExpCaptureIndexLess{});
1014+
DCHECK_EQ(sorted_named_captures.size(), named_captures_->size());
1015+
9951016
Factory* factory = isolate()->factory();
9961017

997-
int len = static_cast<int>(named_captures_->size()) * 2;
1018+
int len = static_cast<int>(sorted_named_captures.size()) * 2;
9981019
Handle<FixedArray> array = factory->NewFixedArray(len);
9991020

10001021
int i = 0;
1001-
for (const auto& capture : *named_captures_) {
1022+
for (const auto& capture : sorted_named_captures) {
10021023
Vector<const uc16> capture_name(capture->name()->data(),
10031024
capture->name()->size());
10041025
// CSA code in ConstructNewResultFromMatchInfo requires these strings to be

test/mjsunit/harmony/regexp-named-captures.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,15 @@ function toSlowMode(re) {
419419
assertEquals("cd", "abcd".replace(re, "$<$1>"));
420420
}
421421

422+
// Named captures are ordered by capture index on the groups object.
423+
// https://crbug.com/v8/9822
424+
425+
{
426+
const r = /(?<BKey>.+)\s(?<AKey>.+)/;
427+
const s = 'example string';
428+
assertArrayEquals(["BKey", "AKey"], Object.keys(r.exec(s).groups));
429+
}
430+
422431
// Tests for 'groups' semantics on the regexp result object.
423432
// https://crbug.com/v8/7192
424433

0 commit comments

Comments
 (0)