4028fec3b5
This work pulls in v8 support for these features with appropriate changes for Dart and closes https://github.com/dart-lang/sdk/issues/34935. This adds support for the following features: * Interpreting patterns as Unicode patterns instead of BMP patterns * the dotAll flag (`/s`) for changing the behavior of '.' to also match line terminators * Escapes for character classes described by Unicode property groups (e.g., \p{Greek} to match all Greek characters, or \P{Greek} for all non-Greek characters). The following TC39 proposals describe some of the added features: * https://github.com/tc39/proposal-regexp-dotall-flag * https://github.com/tc39/proposal-regexp-unicode-property-escapes These additional changes are included: * Extends named capture group names to include the full range of identifier characters supported by ECMAScript, not just ASCII. * Changing the RegExp interface to return RegExpMatch objects, not Match objects, so that downcasting is not necessary to use named capture groups from Dart **Note**: The changes to the RegExp interface are a breaking change for implementers of the RegExp interface. Current users of the RegExp interface (i.e., code using Dart RegExp objects) will not be affected. Change-Id: Ie62e6082a0e2fedc1680ef2576ce0c6db80fc19a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/100641 Reviewed-by: Martin Kustermann <kustermann@google.com> Commit-Queue: Stevie Strickland <sstrickl@google.com>
114 lines
3.6 KiB
C++
114 lines
3.6 KiB
C++
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include "platform/globals.h"
|
|
|
|
#include "vm/isolate.h"
|
|
#include "vm/object.h"
|
|
#include "vm/regexp.h"
|
|
#include "vm/regexp_assembler_ir.h"
|
|
#include "vm/unit_test.h"
|
|
|
|
namespace dart {
|
|
|
|
static RawArray* Match(const String& pat, const String& str) {
|
|
Thread* thread = Thread::Current();
|
|
Zone* zone = thread->zone();
|
|
const RegExp& regexp =
|
|
RegExp::Handle(RegExpEngine::CreateRegExp(thread, pat, RegExpFlags()));
|
|
const Smi& idx = Smi::Handle(Smi::New(0));
|
|
return IRRegExpMacroAssembler::Execute(regexp, str, idx, /*sticky=*/false,
|
|
zone);
|
|
}
|
|
|
|
ISOLATE_UNIT_TEST_CASE(RegExp_OneByteString) {
|
|
uint8_t chars[] = {'a', 'b', 'c', 'b', 'a'};
|
|
intptr_t len = ARRAY_SIZE(chars);
|
|
const String& str =
|
|
String::Handle(OneByteString::New(chars, len, Heap::kNew));
|
|
|
|
const String& pat = String::Handle(String::New("bc"));
|
|
const Array& res = Array::Handle(Match(pat, str));
|
|
EXPECT_EQ(2, res.Length());
|
|
|
|
const Object& res_1 = Object::Handle(res.At(0));
|
|
const Object& res_2 = Object::Handle(res.At(1));
|
|
EXPECT(res_1.IsSmi());
|
|
EXPECT(res_2.IsSmi());
|
|
|
|
const Smi& smi_1 = Smi::Cast(res_1);
|
|
const Smi& smi_2 = Smi::Cast(res_2);
|
|
EXPECT_EQ(1, smi_1.Value());
|
|
EXPECT_EQ(3, smi_2.Value());
|
|
}
|
|
|
|
ISOLATE_UNIT_TEST_CASE(RegExp_TwoByteString) {
|
|
uint16_t chars[] = {'a', 'b', 'c', 'b', 'a'};
|
|
intptr_t len = ARRAY_SIZE(chars);
|
|
const String& str =
|
|
String::Handle(TwoByteString::New(chars, len, Heap::kNew));
|
|
|
|
const String& pat = String::Handle(String::New("bc"));
|
|
const Array& res = Array::Handle(Match(pat, str));
|
|
EXPECT_EQ(2, res.Length());
|
|
|
|
const Object& res_1 = Object::Handle(res.At(0));
|
|
const Object& res_2 = Object::Handle(res.At(1));
|
|
EXPECT(res_1.IsSmi());
|
|
EXPECT(res_2.IsSmi());
|
|
|
|
const Smi& smi_1 = Smi::Cast(res_1);
|
|
const Smi& smi_2 = Smi::Cast(res_2);
|
|
EXPECT_EQ(1, smi_1.Value());
|
|
EXPECT_EQ(3, smi_2.Value());
|
|
}
|
|
|
|
static void NoopFinalizer(void* isolate_callback_data,
|
|
Dart_WeakPersistentHandle handle,
|
|
void* peer) {}
|
|
|
|
ISOLATE_UNIT_TEST_CASE(RegExp_ExternalOneByteString) {
|
|
uint8_t chars[] = {'a', 'b', 'c', 'b', 'a'};
|
|
intptr_t len = ARRAY_SIZE(chars);
|
|
const String& str = String::Handle(ExternalOneByteString::New(
|
|
chars, len, NULL, 0, NoopFinalizer, Heap::kNew));
|
|
|
|
const String& pat = String::Handle(String::New("bc"));
|
|
const Array& res = Array::Handle(Match(pat, str));
|
|
EXPECT_EQ(2, res.Length());
|
|
|
|
const Object& res_1 = Object::Handle(res.At(0));
|
|
const Object& res_2 = Object::Handle(res.At(1));
|
|
EXPECT(res_1.IsSmi());
|
|
EXPECT(res_2.IsSmi());
|
|
|
|
const Smi& smi_1 = Smi::Cast(res_1);
|
|
const Smi& smi_2 = Smi::Cast(res_2);
|
|
EXPECT_EQ(1, smi_1.Value());
|
|
EXPECT_EQ(3, smi_2.Value());
|
|
}
|
|
|
|
ISOLATE_UNIT_TEST_CASE(RegExp_ExternalTwoByteString) {
|
|
uint16_t chars[] = {'a', 'b', 'c', 'b', 'a'};
|
|
intptr_t len = ARRAY_SIZE(chars);
|
|
const String& str = String::Handle(ExternalTwoByteString::New(
|
|
chars, len, NULL, 0, NoopFinalizer, Heap::kNew));
|
|
|
|
const String& pat = String::Handle(String::New("bc"));
|
|
const Array& res = Array::Handle(Match(pat, str));
|
|
EXPECT_EQ(2, res.Length());
|
|
|
|
const Object& res_1 = Object::Handle(res.At(0));
|
|
const Object& res_2 = Object::Handle(res.At(1));
|
|
EXPECT(res_1.IsSmi());
|
|
EXPECT(res_2.IsSmi());
|
|
|
|
const Smi& smi_1 = Smi::Cast(res_1);
|
|
const Smi& smi_2 = Smi::Cast(res_2);
|
|
EXPECT_EQ(1, smi_1.Value());
|
|
EXPECT_EQ(3, smi_2.Value());
|
|
}
|
|
|
|
} // namespace dart
|