Skip to content

Commit 4162f47

Browse files
committed
[java] minimize overhead of regex compilation
1 parent 1d459cd commit 4162f47

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

java/src/org/openqa/selenium/By.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.Objects;
25+
import java.util.regex.Pattern;
26+
2527
import org.openqa.selenium.internal.Require;
2628

2729
/**
@@ -414,6 +416,7 @@ protected final Map<String, Object> toJson() {
414416
}
415417

416418
private abstract static class PreW3CLocator extends By implements Remotable {
419+
private static final Pattern CSS_ESCAPE = Pattern.compile("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])");
417420
private final Parameters remoteParams;
418421
private final ByCssSelector fallback;
419422

@@ -442,7 +445,7 @@ protected final Map<String, Object> toJson() {
442445
}
443446

444447
private String cssEscape(String using) {
445-
using = using.replaceAll("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])", "\\\\$1");
448+
using = CSS_ESCAPE.matcher(using).replaceAll("\\\\$1");
446449
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
447450
using = "\\" + (30 + Integer.parseInt(using.substring(0, 1))) + " " + using.substring(1);
448451
}

java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import java.util.List;
8585
import java.util.Map;
8686
import java.util.concurrent.ConcurrentHashMap;
87+
import java.util.regex.Pattern;
8788
import java.util.stream.Collectors;
8889
import java.util.stream.Stream;
8990
import org.openqa.selenium.InvalidSelectorException;
@@ -99,6 +100,7 @@
99100
public class W3CHttpCommandCodec extends AbstractHttpCommandCodec {
100101

101102
private static final ConcurrentHashMap<String, String> ATOM_SCRIPTS = new ConcurrentHashMap<>();
103+
private static final Pattern CSS_ESCAPE = Pattern.compile("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])");
102104

103105
public W3CHttpCommandCodec() {
104106
String sessionId = "/session/:sessionId";
@@ -376,7 +378,7 @@ private Map<String, String> asElement(Object id) {
376378
}
377379

378380
private String cssEscape(String using) {
379-
using = using.replaceAll("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])", "\\\\$1");
381+
using = CSS_ESCAPE.matcher(using).replaceAll("\\\\$1");
380382
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
381383
using = "\\" + (30 + Integer.parseInt(using.substring(0, 1))) + " " + using.substring(1);
382384
}

java/src/org/openqa/selenium/remote/http/jdk/JdkHttpMessages.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ public URI getRawUri(HttpRequest req) {
148148
|| uri.startsWith("https://")) {
149149
rawUrl = uri;
150150
} else {
151-
rawUrl = baseUrl.toString().replaceAll("/$", "") + uri;
151+
String base = baseUrl.toString();
152+
if (base.endsWith("/")) {
153+
rawUrl = base.substring(0, base.length() - 1) + uri;
154+
} else {
155+
rawUrl = base + uri;
156+
}
152157
}
153158

154159
return URI.create(rawUrl);

java/src/org/openqa/selenium/remote/http/netty/NettyMessages.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ private static String getRawUrl(URI baseUrl, String uri) {
138138
} else if (uri.startsWith("http://") || uri.startsWith("https://")) {
139139
rawUrl = uri;
140140
} else {
141-
rawUrl = baseUrl.toString().replaceAll("/$", "") + uri;
141+
String base = baseUrl.toString();
142+
if (base.endsWith("/")) {
143+
rawUrl = base.substring(0, base.length() - 1) + uri;
144+
} else {
145+
rawUrl = base + uri;
146+
}
142147
}
143148
return rawUrl;
144149
}

0 commit comments

Comments
 (0)