Skip to content

Commit

Permalink
Merge pull request json-path#51 from jochenberger/optimizations
Browse files Browse the repository at this point in the history
more tiny improvements
  • Loading branch information
kallestenflo committed Sep 17, 2014
2 parents daaeb1a + 2b116b2 commit a7fd288
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
2 changes: 1 addition & 1 deletion json-path/src/main/java/com/jayway/jsonpath/Criteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ public Criteria matches(Predicate p) {
}

public static Criteria create(String path, String operator, String expected) {
if (expected.startsWith("'") && expected.endsWith("'")) {
if (! expected.isEmpty() && expected.charAt(0) == '\'' && expected.charAt(expected.length()-1) == '\'') {
expected = expected.substring(1, expected.length() - 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static Path compile(String path, Predicate... filters) {

LinkedList<Predicate> filterList = new LinkedList<Predicate>(asList(filters));

if (!path.startsWith("$")) {
if (path.charAt(0) != '$') {
path = "$." + path;
}

Expand All @@ -57,13 +57,12 @@ public static Path compile(String path, Predicate... filters) {
RootPathToken root = null;


char[] chars = path.toCharArray();
int i = 0;
int positions;
String fragment = "";

do {
char current = chars[i];
char current = path.charAt(i);

switch (current) {
case SPACE:
Expand All @@ -73,27 +72,27 @@ public static Path compile(String path, Predicate... filters) {
i++;
break;
case BRACKET_OPEN:
positions = fastForwardUntilClosed(chars, i);
fragment = new String(chars, i, positions);
positions = fastForwardUntilClosed(path, i);
fragment = path.substring(i, i+positions);
i += positions;
break;
case PERIOD:
i++;
if (chars[i] == PERIOD) {
if (path.charAt(i) == PERIOD) {
//This is a deep scan
fragment = "..";
i++;
} else {
positions = fastForward(chars, i);
positions = fastForward(path, i);
if (positions == 0) {
continue;

} else if (positions == 1 && chars[i] == '*') {
} else if (positions == 1 && path.charAt(i) == '*') {
fragment = new String("[*]");
} else {
assertValidFieldChars(chars, i, positions);
assertValidFieldChars(path, i, positions);

fragment = PROPERTY_OPEN + new String(chars, i, positions) + PROPERTY_CLOSE;
fragment = PROPERTY_OPEN + path.substring(i, i+positions) + PROPERTY_CLOSE;
}
i += positions;
}
Expand All @@ -103,9 +102,9 @@ public static Path compile(String path, Predicate... filters) {
i++;
break;
default:
positions = fastForward(chars, i);
positions = fastForward(path, i);

fragment = PROPERTY_OPEN + new String(chars, i, positions) + PROPERTY_CLOSE;
fragment = PROPERTY_OPEN + path.substring(i, i+positions) + PROPERTY_CLOSE;
i += positions;
break;
}
Expand All @@ -115,7 +114,7 @@ public static Path compile(String path, Predicate... filters) {
root.append(PathComponentAnalyzer.analyze(fragment, filterList));
}

} while (i < chars.length);
} while (i < path.length());

Path pa = new CompiledPath(root);

Expand All @@ -124,10 +123,10 @@ public static Path compile(String path, Predicate... filters) {
return pa;
}

private static void assertValidFieldChars(char[] chars, int start, int positions) {
private static void assertValidFieldChars(String s, int start, int positions) {
int i = start;
while (i < start + positions) {
char c = chars[i];
char c = s.charAt(i);

if (!Character.isLetterOrDigit(c) && c != '-' && c != '_' && c != '$' && c != '@') {
throw new InvalidPathException("Invalid field name! Use bracket notation if your filed names does not match pattern: ([a-zA-Z@][a-zA-Z0-9@\\$_\\-]*)$");
Expand All @@ -136,10 +135,10 @@ private static void assertValidFieldChars(char[] chars, int start, int positions
}
}

private static int fastForward(char[] chars, int index) {
private static int fastForward(String s, int index) {
int skipCount = 0;
while (index < chars.length) {
char current = chars[index];
while (index < s.length()) {
char current = s.charAt(index);
if (current == PERIOD || current == BRACKET_OPEN || current == SPACE) {
break;
}
Expand All @@ -149,16 +148,16 @@ private static int fastForward(char[] chars, int index) {
return skipCount;
}

private static int fastForwardUntilClosed(char[] chars, int index) {
private static int fastForwardUntilClosed(String s, int index) {
int skipCount = 0;
int nestedBrackets = 0;

//First char is always '[' no need to check it
index++;
skipCount++;

while (index < chars.length) {
char current = chars[index];
while (index < s.length()) {
char current = s.charAt(index);

index++;
skipCount++;
Expand All @@ -185,7 +184,6 @@ private static int fastForwardUntilClosed(char[] chars, int index) {
static class PathComponentAnalyzer {

private static final Pattern FILTER_PATTERN = Pattern.compile("^\\[\\s*\\?\\s*[,\\s*\\?]*?\\s*]$"); //[?] or [?, ?, ...]
private char[] chars;
private int i;
private char current;

Expand Down Expand Up @@ -218,10 +216,9 @@ else if (FILTER_PATTERN.matcher(pathFragment).matches()) {
return new PredicatePathToken(filters);
}

this.chars = pathFragment.toCharArray();
this.i = 0;
do {
current = chars[i];
current = pathFragment.charAt(i);

switch (current) {
case '?':
Expand All @@ -237,7 +234,7 @@ else if (FILTER_PATTERN.matcher(pathFragment).matches()) {
}


} while (i < chars.length);
} while (i < pathFragment.length());

throw new InvalidPathException("Could not analyze path component: " + pathFragment);
}
Expand All @@ -259,7 +256,7 @@ private PathToken analyzeCriteriaSequence() {
boolean functionBracketClosed = false;
boolean propertyOpen = false;

current = chars[++i]; //skip the '?'
current = pathFragment.charAt(++i); //skip the '?'

while (current != ']' || bracketCount != 0) {

Expand Down Expand Up @@ -303,7 +300,7 @@ private PathToken analyzeCriteriaSequence() {

} else if (bracketCount == 0 && isLogicOperatorChar(current)) {

if (isLogicOperatorChar(chars[i + 1])) {
if (isLogicOperatorChar(pathFragment.charAt(i + 1))) {
++i;
}
criteria.add(createCriteria(pathBuffer, operatorBuffer, valueBuffer));
Expand All @@ -320,7 +317,7 @@ private PathToken analyzeCriteriaSequence() {

break;
}
current = chars[++i];
current = pathFragment.charAt(++i);
}

if (!functionBracketOpened || !functionBracketClosed) {
Expand Down Expand Up @@ -381,7 +378,7 @@ private PathToken analyzeProperty() {
}
break;
}
current = chars[++i];
current = pathFragment.charAt(++i);
}
return new PropertyPathToken(properties);
}
Expand All @@ -406,15 +403,15 @@ private PathToken analyzeArraySequence() {

if (contextSize) {

current = chars[++i];
current = chars[++i];
current = pathFragment.charAt(++i);
current = pathFragment.charAt(++i);
while (current != '-') {
if (current == ' ' || current == '(' || current == ')') {
current = chars[++i];
current = pathFragment.charAt(++i);
continue;
}
buffer.append(current);
current = chars[++i];
current = pathFragment.charAt(++i);
}
String function = buffer.toString();
buffer.setLength(0);
Expand All @@ -423,11 +420,11 @@ private PathToken analyzeArraySequence() {
}
while (current != ')') {
if (current == ' ') {
current = chars[++i];
current = pathFragment.charAt(++i);
continue;
}
buffer.append(current);
current = chars[++i];
current = pathFragment.charAt(++i);
}

} else {
Expand All @@ -442,27 +439,27 @@ private PathToken analyzeArraySequence() {
if (buffer.length() == 0) {
//this is a tail slice [:12]
sliceTo = true;
current = chars[++i];
current = pathFragment.charAt(++i);
while (Character.isDigit(current) || current == ' ' || current == '-') {
if (current != ' ') {
buffer.append(current);
}
current = chars[++i];
current = pathFragment.charAt(++i);
}
numbers.add(Integer.parseInt(buffer.toString()));
buffer.setLength(0);
} else {
//we now this starts with [12:???
numbers.add(Integer.parseInt(buffer.toString()));
buffer.setLength(0);
current = chars[++i];
current = pathFragment.charAt(++i);

//this is a tail slice [:12]
while (Character.isDigit(current) || current == ' ' || current == '-') {
if (current != ' ') {
buffer.append(current);
}
current = chars[++i];
current = pathFragment.charAt(++i);
}

if (buffer.length() == 0) {
Expand All @@ -486,7 +483,7 @@ private PathToken analyzeArraySequence() {
if (current == ']') {
break;
}
current = chars[++i];
current = pathFragment.charAt(++i);
}
}
if (buffer.length() > 0) {
Expand Down

0 comments on commit a7fd288

Please sign in to comment.