Skip to content

Commit

Permalink
Remove adjacent duplicates to optimize regex before processing
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Nov 2, 2023
1 parent 8673fa9 commit c1ffbc4
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion server/src/main/java/org/opensearch/common/regex/Regex.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ public static Automaton simpleMatchToAutomaton(String... patterns) {
return Operations.union(automata);
}

/**
*
* @param str - The input string to remove adjacent duplicate characters from
* @param target - The target character to remove duplicates of
* @return
*/
public static String removeDuplicates(String str, char target) {
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
int size = sb.length();
if (size > 0 && c == target && sb.charAt(size - 1) == c) {
sb.deleteCharAt(size - 1);
} else {
sb.append(c);
}
}
return sb.toString();
}

/**
* Match a String against the given pattern, supporting the following simple
* pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" matches (with an
Expand All @@ -104,7 +123,8 @@ public static Automaton simpleMatchToAutomaton(String... patterns) {
* @return whether the String matches the given pattern
*/
public static boolean simpleMatch(String pattern, String str) {
return simpleMatch(pattern, str, false);
String trimmedPattern = removeDuplicates(pattern, '*');
return simpleMatch(trimmedPattern, str, false);
}

/**
Expand Down

0 comments on commit c1ffbc4

Please sign in to comment.