Skip to content

Commit 851a67b

Browse files
author
Marcel Overdijk
authored
Merge pull request #52 from marciopd/stringutils-quoteMetacharacters-performance-optimization
Huge performance improvement in StringUtils.quoteMetacharacters.
2 parents fc8ad60 + c14ae17 commit 851a67b

1 file changed

Lines changed: 40 additions & 8 deletions

File tree

rivescript-core/src/main/java/com/rivescript/util/StringUtils.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222

2323
package com.rivescript.util;
2424

25-
import com.rivescript.regexp.Regexp;
25+
import static com.rivescript.regexp.Regexp.RE_NASTIES;
2626

27-
import java.util.regex.Pattern;
27+
import java.util.HashSet;
28+
import java.util.Set;
2829

29-
import static com.rivescript.regexp.Regexp.RE_NASTIES;
30+
import com.rivescript.regexp.Regexp;
3031

3132
/**
3233
* Miscellaneous {@link String} utility methods.
@@ -36,6 +37,30 @@
3637
*/
3738
public class StringUtils {
3839

40+
private static final char ESCAPE_CHAR = '\\';
41+
private static final Set<Character> UNSAFE_CHARS = new HashSet<Character>();
42+
static {
43+
UNSAFE_CHARS.add('\\');
44+
UNSAFE_CHARS.add('.');
45+
UNSAFE_CHARS.add('+');
46+
UNSAFE_CHARS.add('*');
47+
UNSAFE_CHARS.add('?');
48+
UNSAFE_CHARS.add('[');
49+
UNSAFE_CHARS.add('^');
50+
UNSAFE_CHARS.add(']');
51+
UNSAFE_CHARS.add('$');
52+
UNSAFE_CHARS.add('(');
53+
UNSAFE_CHARS.add(')');
54+
UNSAFE_CHARS.add('{');
55+
UNSAFE_CHARS.add('}');
56+
UNSAFE_CHARS.add('=');
57+
UNSAFE_CHARS.add('!');
58+
UNSAFE_CHARS.add('<');
59+
UNSAFE_CHARS.add('>');
60+
UNSAFE_CHARS.add('|');
61+
UNSAFE_CHARS.add(':');
62+
}
63+
3964
/**
4065
* Counts the number of words in a {@link String}.
4166
*
@@ -93,15 +118,21 @@ public static String join(String[] array, String delimiter) {
93118
* @param str the string to escape
94119
* @return the escaped string
95120
*/
96-
public static String quoteMetacharacters(String str) {
121+
public static String quoteMetacharacters(final String str) {
97122
if (str == null) {
98123
return null;
99124
}
100-
String unsafe = "\\.+*?[^]$(){}=!<>|:";
101-
for (char c : unsafe.toCharArray()) {
102-
str = str.replaceAll(Pattern.quote(Character.toString(c)), "\\\\\\" + c);
125+
126+
final StringBuilder sb = new StringBuilder();
127+
for (int i = 0; i < str.length(); i++) {
128+
final char c = str.charAt(i);
129+
if (UNSAFE_CHARS.contains(c)) {
130+
sb.append(ESCAPE_CHAR);
131+
}
132+
sb.append(c);
103133
}
104-
return str;
134+
135+
return sb.toString();
105136
}
106137

107138
/**
@@ -118,3 +149,4 @@ public static String stripNasties(String str) {
118149
return RE_NASTIES.matcher(str).replaceAll("");
119150
}
120151
}
152+

0 commit comments

Comments
 (0)