Skip to content

Commit 4c2eb8d

Browse files
authored
Merge pull request #348 from metafacture/empty-char-class
Don't handle empty '[]' as character class
2 parents fdd7035 + 49af69c commit 4c2eb8d

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

metafacture-commons/src/main/java/org/metafacture/commons/tries/SimpleRegexTrie.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
*/
2929
public class SimpleRegexTrie<P> {
3030

31+
// Non-empty character class, containing non-[] characters, e.g.
32+
// matches: `lit-[A]`, `lit-[AB]`, does not match: `a[].1`, `a[].1.b[].1`
33+
public static final String SIMPLE_CHARACTER_CLASS = ".*\\[[^\\[\\]]+\\].*";
34+
3135
private final WildcardTrie<P> trie;
32-
public static final String SIMPLE_CHARACTER_CLASS = "\\[.*\\]";
3336

3437
public SimpleRegexTrie() {
3538
trie = new WildcardTrie<P>();
@@ -43,7 +46,7 @@ public SimpleRegexTrie() {
4346
* @param value value to associate with the key pattern
4447
*/
4548
public void put(final String keys, final P value) {
46-
if (keys.matches(".*" + SIMPLE_CHARACTER_CLASS + ".*")) {
49+
if (keys.matches(SIMPLE_CHARACTER_CLASS)) {
4750
int charClassStart = keys.indexOf('[', 0);
4851
final int charClassEnd = keys.indexOf(']', 1);
4952
String begin = keys.substring(0, charClassStart);

metafacture-commons/src/test/java/org/metafacture/commons/tries/SimpleRegexTrieTest.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013, 2014 Pascal Christoph, hbz
2+
* Copyright 2013, 2020 Pascal Christoph, hbz and others
33
* Licensed under the Apache License, Version 2.0 the "License";
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -21,17 +21,28 @@
2121
/**
2222
* tests {@link SimpleRegexTrie}
2323
*
24-
* @author Pascal Christoph
24+
* @author Pascal Christoph, Fabian Steeg
2525
*
2626
*/
2727
public final class SimpleRegexTrieTest {
28-
private static final String SCC = "aacbb|a[ab]bb";
28+
2929
private static final String AACBB = "aacbb";
30+
private static final String ABCBB = "abcbb";
3031

3132
@Test
3233
public void testWithSimpleCharacterClass() {
3334
final SimpleRegexTrie<String> trie = new SimpleRegexTrie<String>();
34-
trie.put(SCC, SCC);
35-
assertTrue(AACBB, trie.get(AACBB).size() == 1);
35+
trie.put("a[ab]cbb", "value");
36+
assertTrue("Expecting to find: " + AACBB, trie.get(AACBB).size() == 1);
37+
assertTrue("Expecting to find: " + ABCBB, trie.get(ABCBB).size() == 1);
38+
}
39+
40+
@Test
41+
public void testWithEmptyCharacterClass() {
42+
final SimpleRegexTrie<String> trie = new SimpleRegexTrie<String>();
43+
// Should not be treated as character class (used for JSON arrays):
44+
final String key = "a[].1.b[].1";
45+
trie.put(key, "value");
46+
assertTrue("Expecting to find: " + key, trie.get(key).size() == 1);
3647
}
3748
}

0 commit comments

Comments
 (0)