From: Svjatoslav Agejenko Date: Sat, 15 Sep 2018 22:14:19 +0000 (+0300) Subject: Rename wildcard matcher to glob matcher. X-Git-Tag: svjatoslavcommons-1.8~28 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=svjatoslav_commons.git;a=commitdiff_plain;h=8197322b257af7ebf266af11ff73b5471927b750 Rename wildcard matcher to glob matcher. --- diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java index 0359440..8944931 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java @@ -9,7 +9,7 @@ package eu.svjatoslav.commons.network.navigation; -import eu.svjatoslav.commons.string.WildCardMatcher; +import eu.svjatoslav.commons.string.GlobMatcher; import java.util.ArrayList; import java.util.List; @@ -67,7 +67,7 @@ public class NavigationItem { } public boolean matchesUrl(final String url) { - return WildCardMatcher.match(url, pattern); + return GlobMatcher.match(url, pattern); } public NavigationItem getDefaultNavigationItem() { diff --git a/src/main/java/eu/svjatoslav/commons/string/GlobMatcher.java b/src/main/java/eu/svjatoslav/commons/string/GlobMatcher.java new file mode 100755 index 0000000..b27f1f0 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/string/GlobMatcher.java @@ -0,0 +1,103 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 3 of the GNU Lesser General Public License + * or later as published by the Free Software Foundation. + */ + +package eu.svjatoslav.commons.string; + +public class GlobMatcher { + + /** + * Allow only for * in wildcards + */ + private static boolean checkWildCardEnd(final String wildcardExpression, + int wildCardPosition) { + for (; wildCardPosition < wildcardExpression.length(); wildCardPosition++) { + final char wildCardChar = wildcardExpression + .charAt(wildCardPosition); + if (wildCardChar != '*') + return false; + } + + return true; + } + + /** + *
+     * Test input string against wildcard expression.
+     * * -- corresponds to any amount of characters.
+     * ? -- corresponds to any single character.
+     * 
+ * + * @param inputString input string + * @param wildcardExpression wildcard expression + * @return true if input string matches input pattern + */ + public static boolean match(final String inputString, + final String wildcardExpression) { + + if (inputString == null) + return false; + + if (wildcardExpression == null) + return false; + + int i; + + for (i = 0; i < inputString.length(); i++) { + if (i >= wildcardExpression.length()) + return false; + final char wildCardChar = wildcardExpression.charAt(i); + if (wildCardChar == '*') + return matchPiece(inputString, i, wildcardExpression, i + 1); + if (wildCardChar != '?') + if (inputString.charAt(i) != wildCardChar) + return false; + } + + return checkWildCardEnd(wildcardExpression, i); + } + + private static boolean matchPiece(final String inputString, + final int inputStringIndex, final String wildcardExpression, + final int wildCardExpressionIndex) { + + int wildCardPosition = wildCardExpressionIndex; + + for (int i = inputStringIndex; i < inputString.length(); i++) { + + wildCardPosition = wildCardExpressionIndex; + + subMatchAttempt: + { + + for (int j = i; j < inputString.length(); j++) { + if (wildCardPosition >= wildcardExpression.length()) + break subMatchAttempt; + final char wildCardChar = wildcardExpression + .charAt(wildCardPosition); + + if (wildCardChar == '*') + return matchPiece(inputString, j, wildcardExpression, + wildCardPosition + 1); + + if (wildCardChar != '?') + if (inputString.charAt(j) != wildCardChar) + break subMatchAttempt; + + wildCardPosition++; + } + + return checkWildCardEnd(wildcardExpression, wildCardPosition); + } + + } + + return checkWildCardEnd(wildcardExpression, wildCardPosition); + } + +} diff --git a/src/main/java/eu/svjatoslav/commons/string/WildCardMatcher.java b/src/main/java/eu/svjatoslav/commons/string/WildCardMatcher.java deleted file mode 100755 index 9fe2bb5..0000000 --- a/src/main/java/eu/svjatoslav/commons/string/WildCardMatcher.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 3 of the GNU Lesser General Public License - * or later as published by the Free Software Foundation. - */ - -package eu.svjatoslav.commons.string; - -public class WildCardMatcher { - - /** - * Allow only for * in wildcards - */ - private static boolean checkWildCardEnd(final String wildcardExpression, - int wildCardPosition) { - for (; wildCardPosition < wildcardExpression.length(); wildCardPosition++) { - final char wildCardChar = wildcardExpression - .charAt(wildCardPosition); - if (wildCardChar != '*') - return false; - } - - return true; - } - - /** - *
-     * Test input string against wildcard expression.
-     * * -- corresponds to any amount of characters.
-     * ? -- corresponds to any single character.
-     * 
- * - * @param inputString input string - * @param wildcardExpression wildcard expression - * @return true if input string matches input pattern - */ - public static boolean match(final String inputString, - final String wildcardExpression) { - - if (inputString == null) - return false; - - if (wildcardExpression == null) - return false; - - int i; - - for (i = 0; i < inputString.length(); i++) { - if (i >= wildcardExpression.length()) - return false; - final char wildCardChar = wildcardExpression.charAt(i); - if (wildCardChar == '*') - return matchPiece(inputString, i, wildcardExpression, i + 1); - if (wildCardChar != '?') - if (inputString.charAt(i) != wildCardChar) - return false; - } - - return checkWildCardEnd(wildcardExpression, i); - } - - private static boolean matchPiece(final String inputString, - final int inputStringIndex, final String wildcardExpression, - final int wildCardExpressionIndex) { - - int wildCardPosition = wildCardExpressionIndex; - - for (int i = inputStringIndex; i < inputString.length(); i++) { - - wildCardPosition = wildCardExpressionIndex; - - subMatchAttempt: - { - - for (int j = i; j < inputString.length(); j++) { - if (wildCardPosition >= wildcardExpression.length()) - break subMatchAttempt; - final char wildCardChar = wildcardExpression - .charAt(wildCardPosition); - - if (wildCardChar == '*') - return matchPiece(inputString, j, wildcardExpression, - wildCardPosition + 1); - - if (wildCardChar != '?') - if (inputString.charAt(j) != wildCardChar) - break subMatchAttempt; - - wildCardPosition++; - } - - return checkWildCardEnd(wildcardExpression, wildCardPosition); - } - - } - - return checkWildCardEnd(wildcardExpression, wildCardPosition); - } - -} diff --git a/src/test/java/eu/svjatoslav/commons/string/GlobMatcherTest.java b/src/test/java/eu/svjatoslav/commons/string/GlobMatcherTest.java new file mode 100755 index 0000000..e2713de --- /dev/null +++ b/src/test/java/eu/svjatoslav/commons/string/GlobMatcherTest.java @@ -0,0 +1,44 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 3 of the GNU Lesser General Public License + * or later as published by the Free Software Foundation. + */ + +package eu.svjatoslav.commons.string; + +import org.junit.Test; + +import static org.junit.Assert.fail; + +public class GlobMatcherTest { + + + private static void testWildcard(final String string, final String pattern, + final boolean expectedResult) { + + final boolean result = GlobMatcher.match(string, pattern); + + if (result != expectedResult) + fail("Wildcard match failed."); + + } + + @Test + public void test() { + + testWildcard("IMG_9770.JPG", "*.J*", true); + testWildcard("1", "1", true); + testWildcard("1", "*", true); + testWildcard("f", "1", false); + testWildcard("Hello !", "Hell*!***", true); + testWildcard("Hello !", "Hell*!", true); + testWildcard("Hello !", "Hell*", true); + testWildcard("Hello !", "Hell", false); + testWildcard("Hello !", "* *", true); + + } + +} diff --git a/src/test/java/eu/svjatoslav/commons/string/WildCardMatcherTest.java b/src/test/java/eu/svjatoslav/commons/string/WildCardMatcherTest.java deleted file mode 100755 index ce2f2f6..0000000 --- a/src/test/java/eu/svjatoslav/commons/string/WildCardMatcherTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 3 of the GNU Lesser General Public License - * or later as published by the Free Software Foundation. - */ - -package eu.svjatoslav.commons.string; - -import org.junit.Test; - -import static org.junit.Assert.fail; - -public class WildCardMatcherTest { - - - private static void testWildcard(final String string, final String pattern, - final boolean expectedResult) { - - final boolean result = WildCardMatcher.match(string, pattern); - - if (result != expectedResult) - fail("Wildcard match failed."); - - } - - @Test - public void test() { - - testWildcard("IMG_9770.JPG", "*.J*", true); - testWildcard("1", "1", true); - testWildcard("1", "*", true); - testWildcard("f", "1", false); - testWildcard("Hello !", "Hell*!***", true); - testWildcard("Hello !", "Hell*!", true); - testWildcard("Hello !", "Hell*", true); - testWildcard("Hello !", "Hell", false); - testWildcard("Hello !", "* *", true); - - } - -}