Skip to content

Commit 6bcb92e

Browse files
committed
Add support for case-insensitive matching to RegExPatternMatcher
1 parent 422166d commit 6bcb92e

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

core/src/main/java/org/apache/shiro/util/RegExPatternMatcher.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@
2929
*/
3030
public class RegExPatternMatcher implements PatternMatcher {
3131

32+
private static final int DEFAULT = Pattern.DOTALL;
33+
34+
private static final int CASE_INSENSITIVE = DEFAULT | Pattern.CASE_INSENSITIVE;
35+
36+
private boolean caseInsensitive = false;
37+
3238
/**
3339
* Simple implementation that merely uses the default pattern comparison logic provided by the
3440
* JDK.
3541
* <p/>This implementation essentially executes the following:
3642
* <pre>
37-
* Pattern p = Pattern.compile(pattern);
43+
* Pattern p = Pattern.compile(pattern, Pattern.DOTALL);
3844
* Matcher m = p.matcher(source);
3945
* return m.matches();</pre>
4046
* @param pattern the pattern to match against
@@ -45,8 +51,24 @@ public boolean matches(String pattern, String source) {
4551
if (pattern == null) {
4652
throw new IllegalArgumentException("pattern argument cannot be null.");
4753
}
48-
Pattern p = Pattern.compile(pattern);
54+
Pattern p = Pattern.compile(pattern, caseInsensitive ? CASE_INSENSITIVE : DEFAULT);
4955
Matcher m = p.matcher(source);
5056
return m.matches();
5157
}
58+
59+
/**
60+
* Returns true if regex match should be case-insensitive.
61+
* @return true if regex match should be case-insensitive.
62+
*/
63+
public boolean isCaseInsensitive() {
64+
return caseInsensitive;
65+
}
66+
67+
/**
68+
* Adds the Pattern.CASE_INSENSITIVE flag when compiling patterns.
69+
* @param caseInsensitive true if patterns should match case-insensitive.
70+
*/
71+
public void setCaseInsensitive(boolean caseInsensitive) {
72+
this.caseInsensitive = caseInsensitive;
73+
}
5274
}

core/src/test/java/org/apache/shiro/util/RegExPatternMatcherTest.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import org.junit.Test;
2222
import static org.junit.Assert.*;
2323

24-
import java.util.regex.Pattern;
25-
2624
/**
2725
* Unit tests for the {@link RegExPatternMatcher}.
2826
*
@@ -32,12 +30,44 @@ public class RegExPatternMatcherTest {
3230

3331
@Test
3432
public void testSimplePattern() {
35-
PatternMatcher pm = new RegExPatternMatcher();
36-
String pattern = "a*b";
37-
String test = "aaaaaaab";
38-
//not necessary for the test, but Idea performs auto validation when it sees this:
39-
Pattern.compile(pattern);
40-
assertTrue(pm.matches(pattern, test));
33+
assertPatternMatch("a*b", "aaaaaaab");
34+
}
35+
36+
@Test
37+
public void testMatchesWithCarriageReturn() {
38+
assertPatternMatch(".*", "/blah\n");
39+
}
40+
41+
@Test
42+
public void testMatchesWithLineFeed() {
43+
assertPatternMatch(".*", "/blah\r");
44+
}
45+
46+
@Test
47+
public void testCaseInsensitive() {
48+
RegExPatternMatcher pm = new RegExPatternMatcher();
49+
pm.setCaseInsensitive(true);
50+
assertPatternMatch("/blah", "/BlaH", pm);
4151
}
4252

53+
@Test
54+
public void testCaseSensitive() {
55+
assertPatternNotMatch("/blah", "/BlaH");
56+
}
57+
58+
private void assertPatternMatch(String pattern, String path) {
59+
assertPatternMatch(pattern, path, new RegExPatternMatcher());
60+
}
61+
62+
private void assertPatternMatch(String pattern, String path, PatternMatcher pm) {
63+
assertTrue("Expected path '" + path + "' to match pattern '" + pattern + "'" , pm.matches(pattern, path));
64+
}
65+
66+
private void assertPatternNotMatch(String pattern, String path) {
67+
assertPatternNotMatch(pattern, path, new RegExPatternMatcher());
68+
}
69+
70+
private void assertPatternNotMatch(String pattern, String path, PatternMatcher pm) {
71+
assertFalse("Expected path '" + path + "' to NOT match pattern '" + pattern + "'" , pm.matches(pattern, path));
72+
}
4373
}

0 commit comments

Comments
 (0)