This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push:
new f2af242 Internal changes: use a char matcher when matching a length 1
string.
f2af242 is described below
commit f2af242ba04917c08385b4587072983166cbcf5e
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Jul 14 10:38:06 2020 -0400
Internal changes: use a char matcher when matching a length 1 string.
---
.../commons/text/matcher/AbstractStringMatcher.java | 15 ++-------------
.../apache/commons/text/matcher/StringMatcherFactory.java | 9 ++++++---
.../commons/text/matcher/StringSubstitutorGetSetTest.java | 2 +-
3 files changed, 9 insertions(+), 17 deletions(-)
diff --git
a/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java
b/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java
index 35b4132..6463df4 100644
--- a/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java
+++ b/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java
@@ -131,17 +131,6 @@ abstract class AbstractStringMatcher implements
StringMatcher {
}
/**
- * Constructs a matcher from a String.
- *
- * @param str the string to match, must not be null
- */
- CharArrayMatcher(final String string) {
- super();
- this.string = string;
- this.chars = string.toCharArray();
- }
-
- /**
* Returns the number of matching characters, {@code 0} if there is no
match.
*
* @param buffer the text content to match against, do not change
@@ -344,12 +333,12 @@ abstract class AbstractStringMatcher implements
StringMatcher {
* Thread=safe.
* </p>
*/
- static final class NoMatcher extends AbstractStringMatcher {
+ static final class NoneMatcher extends AbstractStringMatcher {
/**
* Constructs a new instance of {@code NoMatcher}.
*/
- NoMatcher() {
+ NoneMatcher() {
super();
}
diff --git
a/src/main/java/org/apache/commons/text/matcher/StringMatcherFactory.java
b/src/main/java/org/apache/commons/text/matcher/StringMatcherFactory.java
index cd290ac..b539a0b 100644
--- a/src/main/java/org/apache/commons/text/matcher/StringMatcherFactory.java
+++ b/src/main/java/org/apache/commons/text/matcher/StringMatcherFactory.java
@@ -46,7 +46,7 @@ public final class StringMatcherFactory {
/**
* Matches no characters.
*/
- private static final AbstractStringMatcher.NoMatcher NONE_MATCHER = new
AbstractStringMatcher.NoMatcher();
+ private static final AbstractStringMatcher.NoneMatcher NONE_MATCHER = new
AbstractStringMatcher.NoneMatcher();
/**
* Matches the single or double quote character.
@@ -212,7 +212,10 @@ public final class StringMatcherFactory {
* @since 1.9
*/
public StringMatcher stringMatcher(final char... chars) {
- return ArrayUtils.isEmpty(chars) ? NONE_MATCHER : new
AbstractStringMatcher.CharArrayMatcher(chars);
+ final int length = ArrayUtils.getLength(chars);
+ return length == 0 ? NONE_MATCHER
+ : length == 1 ? new AbstractStringMatcher.CharMatcher(chars[0])
+ : new AbstractStringMatcher.CharArrayMatcher(chars);
}
/**
@@ -222,7 +225,7 @@ public final class StringMatcherFactory {
* @return a new Matcher for the given String
*/
public StringMatcher stringMatcher(final String str) {
- return StringUtils.isEmpty(str) ? NONE_MATCHER : new
AbstractStringMatcher.CharArrayMatcher(str);
+ return str == null ? NONE_MATCHER : stringMatcher(str.toCharArray());
}
/**
diff --git
a/src/test/java/org/apache/commons/text/matcher/StringSubstitutorGetSetTest.java
b/src/test/java/org/apache/commons/text/matcher/StringSubstitutorGetSetTest.java
index 7be17e9..b4faae4 100644
---
a/src/test/java/org/apache/commons/text/matcher/StringSubstitutorGetSetTest.java
+++
b/src/test/java/org/apache/commons/text/matcher/StringSubstitutorGetSetTest.java
@@ -58,7 +58,7 @@ public class StringSubstitutorGetSetTest {
@Test
public void testGetSetSuffix() {
final StringSubstitutor sub = new StringSubstitutor();
- assertTrue(sub.getVariableSuffixMatcher() instanceof
AbstractStringMatcher.CharArrayMatcher);
+ assertTrue(sub.getVariableSuffixMatcher() instanceof
AbstractStringMatcher.CharMatcher);
sub.setVariableSuffix('<');
assertTrue(sub.getVariableSuffixMatcher() instanceof
AbstractStringMatcher.CharMatcher);