Possibility to repeat string.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / GlobMatcher.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.commons.string;
6
7 public class GlobMatcher {
8
9     /**
10      * Allow only for * in wildcards
11      */
12     private static boolean checkWildCardEnd(final String wildcardExpression,
13                                             int wildCardPosition) {
14         for (; wildCardPosition < wildcardExpression.length(); wildCardPosition++) {
15             final char wildCardChar = wildcardExpression
16                     .charAt(wildCardPosition);
17             if (wildCardChar != '*')
18                 return false;
19         }
20
21         return true;
22     }
23
24     /**
25      * <pre>
26      * Test input string against wildcard expression.
27      * * -- corresponds to any amount of characters.
28      * ? -- corresponds to any single character.
29      * </pre>
30      *
31      * @param inputString        input string
32      * @param wildcardExpression wildcard expression
33      * @return <code>true</code> if input string matches input pattern
34      */
35     public static boolean match(final String inputString,
36                                 final String wildcardExpression) {
37
38         if (inputString == null)
39             return false;
40
41         if (wildcardExpression == null)
42             return false;
43
44         int i;
45
46         for (i = 0; i < inputString.length(); i++) {
47             if (i >= wildcardExpression.length())
48                 return false;
49             final char wildCardChar = wildcardExpression.charAt(i);
50             if (wildCardChar == '*')
51                 return matchPiece(inputString, i, wildcardExpression, i + 1);
52             if (wildCardChar != '?')
53                 if (inputString.charAt(i) != wildCardChar)
54                     return false;
55         }
56
57         return checkWildCardEnd(wildcardExpression, i);
58     }
59
60     private static boolean matchPiece(final String inputString,
61                                       final int inputStringIndex, final String wildcardExpression,
62                                       final int wildCardExpressionIndex) {
63
64         int wildCardPosition = wildCardExpressionIndex;
65
66         for (int i = inputStringIndex; i < inputString.length(); i++) {
67
68             wildCardPosition = wildCardExpressionIndex;
69
70             subMatchAttempt:
71             {
72
73                 for (int j = i; j < inputString.length(); j++) {
74                     if (wildCardPosition >= wildcardExpression.length())
75                         break subMatchAttempt;
76                     final char wildCardChar = wildcardExpression
77                             .charAt(wildCardPosition);
78
79                     if (wildCardChar == '*')
80                         return matchPiece(inputString, j, wildcardExpression,
81                                 wildCardPosition + 1);
82
83                     if (wildCardChar != '?')
84                         if (inputString.charAt(j) != wildCardChar)
85                             break subMatchAttempt;
86
87                     wildCardPosition++;
88                 }
89
90                 return checkWildCardEnd(wildcardExpression, wildCardPosition);
91             }
92
93         }
94
95         return checkWildCardEnd(wildcardExpression, wildCardPosition);
96     }
97
98 }