mirror of
https://github.com/lightbend/config.git
synced 2025-02-15 22:00:52 +08:00
avoid full parser for simple paths
Kind of a stupid overoptimization probably but what the hell
This commit is contained in:
parent
da09c9790c
commit
d13f7861fd
@ -511,6 +511,10 @@ final class Parser {
|
|||||||
static ConfigOrigin apiOrigin = new SimpleConfigOrigin("path parameter");
|
static ConfigOrigin apiOrigin = new SimpleConfigOrigin("path parameter");
|
||||||
|
|
||||||
static Path parsePath(String path) {
|
static Path parsePath(String path) {
|
||||||
|
Path speculated = speculativeFastParsePath(path);
|
||||||
|
if (speculated != null)
|
||||||
|
return speculated;
|
||||||
|
|
||||||
StringReader reader = new StringReader(path);
|
StringReader reader = new StringReader(path);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -521,4 +525,41 @@ final class Parser {
|
|||||||
reader.close();
|
reader.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the idea is to see if the string has any chars that might require the
|
||||||
|
// full parser to deal with.
|
||||||
|
private static boolean hasUnsafeChars(String s) {
|
||||||
|
for (int i = 0; i < s.length(); ++i) {
|
||||||
|
char c = s.charAt(i);
|
||||||
|
if (Character.isLetter(c) || c == '.')
|
||||||
|
continue;
|
||||||
|
else
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void appendPathString(PathBuilder pb, String s) {
|
||||||
|
int splitAt = s.indexOf('.');
|
||||||
|
if (splitAt < 0) {
|
||||||
|
pb.appendKey(s);
|
||||||
|
} else {
|
||||||
|
pb.appendKey(s.substring(0, splitAt));
|
||||||
|
appendPathString(pb, s.substring(splitAt + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// do something much faster than the full parser if
|
||||||
|
// we just have something like "foo" or "foo.bar"
|
||||||
|
private static Path speculativeFastParsePath(String path) {
|
||||||
|
String s = path.trim();
|
||||||
|
if (hasUnsafeChars(s))
|
||||||
|
return null;
|
||||||
|
if (s.startsWith(".") || s.endsWith(".") || s.contains(".."))
|
||||||
|
return null; // let the full parser throw the error
|
||||||
|
|
||||||
|
PathBuilder pb = new PathBuilder();
|
||||||
|
appendPathString(pb, s);
|
||||||
|
return pb.result();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user