You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/06/30 20:32:32 UTC

[commons-text] branch master updated: Add org.apache.commons.text.matcher.StringMatcherFactory.stringMatcher(char...) .

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 258ebc4  Add org.apache.commons.text.matcher.StringMatcherFactory.stringMatcher(char...) .
258ebc4 is described below

commit 258ebc414c9fb83b8f2b673a1dff3eecfc92b44c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jun 30 16:32:25 2020 -0400

    Add org.apache.commons.text.matcher.StringMatcherFactory.stringMatcher(char...)
    .
---
 src/changes/changes.xml                                   |  3 ++-
 .../commons/text/matcher/AbstractStringMatcher.java       | 11 +++++++++++
 .../apache/commons/text/matcher/StringMatcherFactory.java | 15 +++++++++++++++
 .../commons/text/matcher/StringMatcherFactoryTest.java    |  5 +++++
 .../text/matcher/StringMatcherOnCharArrayTest.java        | 13 +++++++++++--
 5 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b6b991d..44c862d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -60,7 +60,8 @@ The <action> type attribute can be add,update,fix,remove.
     <action                  type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.toString(int, int).</action>
     <action                  type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.readFrom(Reader, int).</action>
     <action                  type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.charAtDelete(int).</action>
-    <action                  type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.TextStringBuilder(CharSequence).</action>    
+    <action                  type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.TextStringBuilder(CharSequence).</action>
+    <action                  type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.matcher.StringMatcherFactory.stringMatcher(char...).</action>
     <action                  type="update" dev="ggregory" due-to="Gary Gregory">[test] junit-jupiter 5.5.1 -> 5.5.2.</action>
     <action                  type="update" dev="ggregory" due-to="Gary Gregory">[test] org.assertj:assertj-core 3.13.2 -> 3.16.1.</action>
     <action                  type="update" dev="ggregory" due-to="Gary Gregory">[build] com.puppycrawl.tools:checkstyle 8.23 -> 8.34.</action>
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 26befb0..e7cf3c4 100644
--- a/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java
+++ b/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java
@@ -219,6 +219,17 @@ abstract class AbstractStringMatcher implements StringMatcher {
         }
 
         /**
+         * Constructs a matcher from a String.
+         *
+         * @param str the string to match, must not be null
+         */
+        StringMatcher(final char... chars) {
+            super();
+            this.string = String.valueOf(chars);
+            this.chars = chars.clone();
+        }
+
+        /**
          * Returns the number of matching characters, {@code 0} if there is no match.
          *
          * @param buffer the text content to match against, do not change
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 b112a92..d1011ae 100644
--- a/src/main/java/org/apache/commons/text/matcher/StringMatcherFactory.java
+++ b/src/main/java/org/apache/commons/text/matcher/StringMatcherFactory.java
@@ -196,6 +196,21 @@ public final class StringMatcherFactory {
     /**
      * Creates a matcher from a string.
      *
+     * @param chars
+     *            the string to match, null or empty matches nothing
+     * @return a new Matcher for the given String
+     * @since 1.9
+     */
+    public StringMatcher stringMatcher(final char... chars) {
+        if (chars == null || chars.length == 0) {
+            return NONE_MATCHER;
+        }
+        return new AbstractStringMatcher.StringMatcher(chars);
+    }
+
+    /**
+     * Creates a matcher from a string.
+     *
      * @param str
      *            the string to match, null or empty matches nothing
      * @return a new Matcher for the given String
diff --git a/src/test/java/org/apache/commons/text/matcher/StringMatcherFactoryTest.java b/src/test/java/org/apache/commons/text/matcher/StringMatcherFactoryTest.java
index 6b230d5..2da69f2 100644
--- a/src/test/java/org/apache/commons/text/matcher/StringMatcherFactoryTest.java
+++ b/src/test/java/org/apache/commons/text/matcher/StringMatcherFactoryTest.java
@@ -82,6 +82,11 @@ public class StringMatcherFactoryTest {
     }
 
     @Test
+    public void test_stringMatcherChars() {
+        assertNotNull(StringMatcherFactory.INSTANCE.stringMatcher('1', '2'));
+    }
+
+    @Test
     public void test_tabMatcher() {
         assertNotNull(StringMatcherFactory.INSTANCE.charMatcher('1'));
     }
diff --git a/src/test/java/org/apache/commons/text/matcher/StringMatcherOnCharArrayTest.java b/src/test/java/org/apache/commons/text/matcher/StringMatcherOnCharArrayTest.java
index a4c4e77..68208d7 100644
--- a/src/test/java/org/apache/commons/text/matcher/StringMatcherOnCharArrayTest.java
+++ b/src/test/java/org/apache/commons/text/matcher/StringMatcherOnCharArrayTest.java
@@ -254,8 +254,17 @@ public class StringMatcherOnCharArrayTest {
     }
 
     @Test
-    public void testStringMatcher_String() {
-        final StringMatcher matcher = StringMatcherFactory.INSTANCE.stringMatcher("bc");
+    public void testStringMatcher_String_fromChars() {
+        testStringMatcher_String(StringMatcherFactory.INSTANCE.stringMatcher('b', 'c'));
+        testStringMatcher_String(StringMatcherFactory.INSTANCE.stringMatcher(new char[] { 'b', 'c' }));
+    }
+
+    @Test
+    public void testStringMatcher_String_fromString() {
+        testStringMatcher_String(StringMatcherFactory.INSTANCE.stringMatcher("bc"));
+    }
+
+    private void testStringMatcher_String(final StringMatcher matcher) {
         assertEquals(2, matcher.size());
         //
         assertThat(matcher.isMatch(BUFFER2, 0, 0, BUFFER2_LENGTH)).isEqualTo(0);