Skip to content

Commit

Permalink
Replace regexp on getValue with linear search and replace
Browse files Browse the repository at this point in the history
  • Loading branch information
pcasaes authored and radcortez committed Jun 30, 2020
1 parent b0b61c2 commit c9e4b1b
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Pattern;

import io.smallrye.config.common.AbstractConfigSource;

Expand All @@ -30,8 +29,6 @@
public class EnvConfigSource extends AbstractConfigSource {
private static final long serialVersionUID = -4525015934376795496L;

private static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9_]");

EnvConfigSource() {
super("EnvConfigSource", 300);
}
Expand All @@ -57,7 +54,7 @@ public String getValue(String name) {
}

// replace non-alphanumeric characters by underscores
String sanitizedName = PATTERN.matcher(name).replaceAll("_");
String sanitizedName = replaceNonAlphanumericByUnderscores(name);

value = properties.get(sanitizedName);
if (value != null) {
Expand All @@ -67,4 +64,20 @@ public String getValue(String name) {
// replace non-alphanumeric characters by underscores and convert to uppercase
return properties.get(sanitizedName.toUpperCase());
}

private String replaceNonAlphanumericByUnderscores(String name) {
int length = name.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = name.charAt(i);
if ('a' <= c && c <= 'z' ||
'A' <= c && c <= 'Z' ||
'0' <= c && c <= '9') {
sb.append(c);
} else {
sb.append('_');
}
}
return sb.toString();
}
}

0 comments on commit c9e4b1b

Please sign in to comment.