Migrate language_2/reg_exp to NNBD.

Change-Id: I662add43926c09735f7ae3899567f5df085ff6a9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150465
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Srujan Gaddam <srujzs@google.com>
This commit is contained in:
Robert Nystrom 2020-06-09 18:45:33 +00:00 committed by commit-bot@chromium.org
parent 1427fadf03
commit 222248b29e
4 changed files with 167 additions and 0 deletions

View file

@ -0,0 +1,34 @@
// Copyright (c) 2011, 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.
// Dart test for testing regular expressions in Dart.
import "package:expect/expect.dart";
class RegExp2Test {
static String? findImageTag_(String text, String extensions) {
final re = new RegExp('src="(http://\\S+\\.(${extensions}))"');
print('REGEXP findImageTag_ $extensions text: \n$text');
final match = re.firstMatch(text);
print('REGEXP findImageTag_ $extensions SUCCESS');
if (match != null) {
return match[1];
} else {
return null;
}
}
static testMain() {
String text =
'''<img src="http://cdn.archinect.net/images/514x/c0/c0p3qo202oxp0e6z.jpg" width="514" height="616" border="0" title="" alt=""><em><p>My last entry was in December of 2009. I suppose I never was particularly good about updating this thing, but it seems a bit ridiculous that I couldn't be bothered to post once about the many, many things that have gone on since then. My apologies. I guess I could start by saying that the world looks like a very different place than it did back in second year.</p></em>
''';
String extensions = 'jpg|jpeg|png';
String? tag = findImageTag_(text, extensions);
Expect.isNotNull(tag);
}
}
main() {
RegExp2Test.testMain();
}

View file

@ -0,0 +1,23 @@
// Copyright (c) 2011, 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.
// Dart test for testing regular expressions in Dart.
import "package:expect/expect.dart";
class RegExp3Test {
static testMain() {
var i = 2000;
try {
RegExp exp = new RegExp("[");
i = 100; // Should not reach here.
} on FormatException catch (e) {
i = 0;
}
Expect.equals(0, i);
}
}
main() {
RegExp3Test.testMain();
}

View file

@ -0,0 +1,26 @@
// Copyright (c) 2011, 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.
// Dart test for testing regular expressions in Dart.
class RegEx2Test {
static void testMain() {
final helloPattern = new RegExp("with (hello)");
String s = "this is a string with hello somewhere";
Match? match = helloPattern.firstMatch(s);
if (match != null) {
print("got match");
int groupCount = match.groupCount;
print("groupCount is $groupCount");
print("group 0 is ${match.group(0)}");
print("group 1 is ${match.group(1)}");
} else {
print("match not round");
}
print("done");
}
}
main() {
RegEx2Test.testMain();
}

View file

@ -0,0 +1,84 @@
// Copyright (c) 2011, 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.
// Dart test for testing regular expressions in Dart.
import "package:expect/expect.dart";
void main() {
RegExp exp = new RegExp(r"(\w+)");
String str = "Parse my string";
List<Match> matches = exp.allMatches(str).toList();
Expect.equals(3, matches.length);
Expect.equals("Parse", matches[0].group(0));
Expect.equals("my", matches[1].group(0));
Expect.equals("string", matches[2].group(0));
// Check that allMatches progresses correctly for empty matches, and that
// it includes the empty match at the end position.
exp = new RegExp("a?");
str = "babba";
Expect.listEquals(["", "a", "", "", "a", ""],
exp.allMatches(str).map((x) => x[0]).toList());
// Check that allMatches works with optional start index.
exp = new RegExp("as{2}");
str = "assassin";
Expect.equals(2, exp.allMatches(str).length);
Expect.equals(2, exp.allMatches(str, 0).length);
Expect.equals(1, exp.allMatches(str, 1).length);
Expect.equals(0, exp.allMatches(str, 4).length);
Expect.equals(0, exp.allMatches(str, str.length).length);
Expect.throws(() => exp.allMatches(str, -1));
Expect.throws(() => exp.allMatches(str, str.length + 1));
exp = new RegExp(".*");
Expect.equals("", exp.allMatches(str, str.length).single[0]);
// The "^" must only match at the beginning of the string.
// Using a start-index doesn't change where the string starts.
exp = new RegExp("^ass");
Expect.equals(1, exp.allMatches(str, 0).length);
Expect.equals(0, exp.allMatches(str, 3).length);
// Regression test for http://dartbug.com/2980
exp = new RegExp("^", multiLine: true); // Any zero-length match will work.
str = "foo\nbar\nbaz";
Expect.equals(" foo\n bar\n baz", str.replaceAll(exp, " "));
exp = new RegExp(r"(\w+)");
Expect.isNull(exp.matchAsPrefix(" xyz ab"));
Expect.isNull(exp.matchAsPrefix(" xyz ab", 0));
var m = exp.matchAsPrefix(" xyz ab", 1)!;
Expect.equals("xyz", m[0]);
Expect.equals("xyz", m[1]);
Expect.equals(1, m.groupCount);
m = exp.matchAsPrefix(" xyz ab", 2)!;
Expect.equals("yz", m[0]);
Expect.equals("yz", m[1]);
Expect.equals(1, m.groupCount);
m = exp.matchAsPrefix(" xyz ab", 3)!;
Expect.equals("z", m[0]);
Expect.equals("z", m[1]);
Expect.equals(1, m.groupCount);
Expect.isNull(exp.matchAsPrefix(" xyz ab", 4));
m = exp.matchAsPrefix(" xyz ab", 5)!;
Expect.equals("ab", m[0]);
Expect.equals("ab", m[1]);
Expect.equals(1, m.groupCount);
m = exp.matchAsPrefix(" xyz ab", 6)!;
Expect.equals("b", m[0]);
Expect.equals("b", m[1]);
Expect.equals(1, m.groupCount);
Expect.isNull(exp.matchAsPrefix(" xyz ab", 7));
Expect.throws(() => exp.matchAsPrefix(" xyz ab", -1));
Expect.throws(() => exp.matchAsPrefix(" xyz ab", 8));
}