Skip to content

Commit

Permalink
Fix character class recognition for multiple []
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Dec 3, 2020
1 parent 32dde6e commit e40f7d7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Expand Up @@ -28,8 +28,11 @@
*/
public class SimpleRegexTrie<P> {

// Non-empty character class, containing non-[] characters, e.g.
// matches: `lit-[A]`, `lit-[AB]`, does not match: `a[].1`, `a[].1.b[].1`
public static final String SIMPLE_CHARACTER_CLASS = ".*\\[[^\\[\\]]+\\].*";

private final WildcardTrie<P> trie;
public static final String SIMPLE_CHARACTER_CLASS = "\\[.+\\]";

public SimpleRegexTrie() {
trie = new WildcardTrie<P>();
Expand All @@ -43,7 +46,7 @@ public SimpleRegexTrie() {
* @param value value to associate with the key pattern
*/
public void put(final String keys, final P value) {
if (keys.matches(".*" + SIMPLE_CHARACTER_CLASS + ".*")) {
if (keys.matches(SIMPLE_CHARACTER_CLASS)) {
int charClassStart = keys.indexOf('[', 0);
final int charClassEnd = keys.indexOf(']', 1);
String begin = keys.substring(0, charClassStart);
Expand Down
Expand Up @@ -41,7 +41,8 @@ public void testWithSimpleCharacterClass() {
public void testWithEmptyCharacterClass() {
final SimpleRegexTrie<String> trie = new SimpleRegexTrie<String>();
// Should not be treated as character class (used for JSON arrays):
trie.put("a[].1", "value");
assertTrue("Expecting to find: a[].1", trie.get("a[].1").size() == 1);
final String key = "a[].1.b[].1";
trie.put(key, "value");
assertTrue("Expecting to find: " + key, trie.get(key).size() == 1);
}
}

0 comments on commit e40f7d7

Please sign in to comment.