Refactor String#replaceAll (#1146)

If we repeatedly call String#replaceAll, we internally repeatedly call the regular expression pattern compilation every time as following:

```java
    public String replaceAll(String regex, String replacement) {
        return Pattern.compile(regex).matcher(this).replaceAll(replacement);
    }
```
The modifications are to keep the compiled pattern.
Therefore, compiling a relatively expensive regular expression pattern does not have to be done every time.
This commit is contained in:
durigon
2018-09-20 09:39:35 +09:00
committed by Ryan Baxter
parent 226358e553
commit ccd3bc2ff6

View File

@@ -17,6 +17,7 @@ package org.springframework.cloud.config.server.support;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.internal.JGitText;
@@ -37,6 +38,8 @@ import org.eclipse.jgit.transport.URIish;
*/
public class GitSkipSslValidationCredentialsProvider extends CredentialsProvider {
private static final Pattern FORMAT_PLACEHOLDER_PATTERN = Pattern.compile("\\s*\\{\\d}\\s*");
private final CredentialsProvider delegate;
public GitSkipSslValidationCredentialsProvider(CredentialsProvider delegate) {
@@ -128,6 +131,6 @@ public class GitSkipSslValidationCredentialsProvider extends CredentialsProvider
}
private static String stripFormattingPlaceholders(String string) {
return string.replaceAll("\\s*\\{\\d}\\s*", "");
return FORMAT_PLACEHOLDER_PATTERN.matcher(string).replaceAll("");
}
}