8236034: Use optimized Ques node for curly {0,1} quantifier

Reviewed-by: rriggs
This commit is contained in:
Ivan Gerasimov 2020-01-21 17:52:55 -08:00
parent eef726a99a
commit 6f7f81f794
3 changed files with 100 additions and 31 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -3243,21 +3243,28 @@ loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
GREEDY, LAZY, POSSESSIVE, INDEPENDENT GREEDY, LAZY, POSSESSIVE, INDEPENDENT
} }
private Node curly(Node prev, int cmin) { private Qtype qtype() {
int ch = next(); int ch = next();
if (ch == '?') { if (ch == '?') {
next(); next();
return new Curly(prev, cmin, MAX_REPS, Qtype.LAZY); return Qtype.LAZY;
} else if (ch == '+') { } else if (ch == '+') {
next(); next();
return new Curly(prev, cmin, MAX_REPS, Qtype.POSSESSIVE); return Qtype.POSSESSIVE;
} }
return Qtype.GREEDY;
}
private Node curly(Node prev, int cmin) {
Qtype qtype = qtype();
if (qtype == Qtype.GREEDY) {
if (prev instanceof BmpCharProperty) { if (prev instanceof BmpCharProperty) {
return new BmpCharPropertyGreedy((BmpCharProperty)prev, cmin); return new BmpCharPropertyGreedy((BmpCharProperty)prev, cmin);
} else if (prev instanceof CharProperty) { } else if (prev instanceof CharProperty) {
return new CharPropertyGreedy((CharProperty)prev, cmin); return new CharPropertyGreedy((CharProperty)prev, cmin);
} }
return new Curly(prev, cmin, MAX_REPS, Qtype.GREEDY); }
return new Curly(prev, cmin, MAX_REPS, qtype);
} }
/** /**
@ -3269,15 +3276,7 @@ loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
int ch = peek(); int ch = peek();
switch (ch) { switch (ch) {
case '?': case '?':
ch = next(); return new Ques(prev, qtype());
if (ch == '?') {
next();
return new Ques(prev, Qtype.LAZY);
} else if (ch == '+') {
next();
return new Ques(prev, Qtype.POSSESSIVE);
}
return new Ques(prev, Qtype.GREEDY);
case '*': case '*':
return curly(prev, 0); return curly(prev, 0);
case '+': case '+':
@ -3314,16 +3313,10 @@ loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
throw error("Unclosed counted closure"); throw error("Unclosed counted closure");
if (cmax < cmin) if (cmax < cmin)
throw error("Illegal repetition range"); throw error("Illegal repetition range");
ch = peek(); unread();
if (ch == '?') { return (cmin == 0 && cmax == 1)
next(); ? new Ques(prev, qtype())
return new Curly(prev, cmin, cmax, Qtype.LAZY); : new Curly(prev, cmin, cmax, qtype());
} else if (ch == '+') {
next();
return new Curly(prev, cmin, cmax, Qtype.POSSESSIVE);
} else {
return new Curly(prev, cmin, cmax, Qtype.GREEDY);
}
} else { } else {
throw error("Illegal repetition"); throw error("Illegal repetition");
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -35,7 +35,7 @@
* 8027645 8035076 8039124 8035975 8074678 6854417 8143854 8147531 7071819 * 8027645 8035076 8039124 8035975 8074678 6854417 8143854 8147531 7071819
* 8151481 4867170 7080302 6728861 6995635 6736245 4916384 6328855 6192895 * 8151481 4867170 7080302 6728861 6995635 6736245 4916384 6328855 6192895
* 6345469 6988218 6693451 7006761 8140212 8143282 8158482 8176029 8184706 * 6345469 6988218 6693451 7006761 8140212 8143282 8158482 8176029 8184706
* 8194667 8197462 8184692 8221431 8224789 8228352 8230829 * 8194667 8197462 8184692 8221431 8224789 8228352 8230829 8236034
* *
* @library /test/lib * @library /test/lib
* @library /lib/testlibrary/java/lang * @library /lib/testlibrary/java/lang

View File

@ -1,5 +1,5 @@
// //
// Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved. // Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. // DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
// //
// This code is free software; you can redistribute it and/or modify it // This code is free software; you can redistribute it and/or modify it
@ -34,26 +34,50 @@
a a
true a 1 true a 1
^(a){0,1}a
a
true a 1
^(aa(bb)?)+$ ^(aa(bb)?)+$
aabbaa aabbaa
true aabbaa 2 aa bb true aabbaa 2 aa bb
^(aa(bb){0,1})+$
aabbaa
true aabbaa 2 aa bb
((a|b)?b)+ ((a|b)?b)+
b b
true b 2 b true b 2 b
((a|b){0,1}b)+
b
true b 2 b
(aaa)?aaa (aaa)?aaa
aaa aaa
true aaa 1 true aaa 1
(aaa){0,1}aaa
aaa
true aaa 1
^(a(b)?)+$ ^(a(b)?)+$
aba aba
true aba 2 a b true aba 2 a b
^(a(b){0,1})+$
aba
true aba 2 a b
^(a(b(c)?)?)?abc ^(a(b(c)?)?)?abc
abc abc
true abc 3 true abc 3
^(a(b(c){0,1}){0,1}){0,1}abc
abc
true abc 3
^(a(b(c))).* ^(a(b(c))).*
abc abc
true abc 3 abc bc c true abc 3 abc bc c
@ -750,52 +774,100 @@ a?b
aaaab aaaab
true ab 0 true ab 0
a{0,1}b
aaaab
true ab 0
a?b a?b
b b
true b 0 true b 0
a{0,1}b
b
true b 0
a?b a?b
aaaccc aaaccc
false 0 false 0
a{0,1}b
aaaccc
false 0
.?b .?b
aaaab aaaab
true ab 0 true ab 0
.{0,1}b
aaaab
true ab 0
// Reluctant ? metacharacter // Reluctant ? metacharacter
a??b a??b
aaaab aaaab
true ab 0 true ab 0
a{0,1}?b
aaaab
true ab 0
a??b a??b
b b
true b 0 true b 0
a{0,1}?b
b
true b 0
a??b a??b
aaaccc aaaccc
false 0 false 0
a{0,1}?b
aaaccc
false 0
.??b .??b
aaaab aaaab
true ab 0 true ab 0
.{0,1}?b
aaaab
true ab 0
// Possessive ? metacharacter // Possessive ? metacharacter
a?+b a?+b
aaaab aaaab
true ab 0 true ab 0
a{0,1}+b
aaaab
true ab 0
a?+b a?+b
b b
true b 0 true b 0
a{0,1}+b
b
true b 0
a?+b a?+b
aaaccc aaaccc
false 0 false 0
a{0,1}+b
aaaccc
false 0
.?+b .?+b
aaaab aaaab
true ab 0 true ab 0
.{0,1}+b
aaaab
true ab 0
// Greedy + metacharacter // Greedy + metacharacter
a+b a+b
aaaab aaaab
@ -1155,3 +1227,7 @@ true blahblah 0
(|f)?+ (|f)?+
foo foo
true 1 true 1
(|f){0,1}+
foo
true 1