Skip to content

Commit 1c8ac84

Browse files
committed
re-working RelativeJsonPointerFormatValidator parser
1 parent 7f1410f commit 1c8ac84

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

core/src/main/java/org/everit/json/schema/internal/RelativeJsonPointerFormatValidator.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public ParseException(String input) {
1616

1717
private static final class Parser {
1818

19+
public static final int EOF = 26;
20+
1921
private static boolean isDigit(char c) {
2022
return '0' <= c && c <= '9';
2123
}
@@ -44,20 +46,27 @@ private void parseTrailingHashmark() throws ParseException {
4446
fail();
4547
}
4648

49+
private char next() {
50+
++pos;
51+
if (pos == input.length()) {
52+
return 26;
53+
}
54+
return curr();
55+
}
56+
4757
private char curr() {
48-
return input.charAt(pos++);
58+
if (pos == input.length()) {
59+
return EOF;
60+
}
61+
return input.charAt(pos);
4962
}
5063

5164
private void parseUpwardsStepCount() throws ParseException {
5265
if (!isDigit(curr())) {
5366
fail();
5467
}
55-
if (pos == input.length()) {
56-
return;
57-
}
58-
for (char current = curr(); isDigit(current) && pos < input.length() - 1; current = curr())
68+
for (char current = next(); isDigit(current) && pos < input.length(); current = next())
5969
;
60-
pos--;
6170
}
6271

6372
private void fail() throws ParseException {
@@ -66,9 +75,10 @@ private void fail() throws ParseException {
6675

6776
private void parseJsonPointer() throws ParseException {
6877
StringBuilder sb = new StringBuilder();
69-
char current;
70-
while (pos < input.length() && (current = curr()) != '#') {
78+
char current = curr();
79+
while (pos < input.length() && current != '#') {
7180
sb.append(current);
81+
current = next();
7282
}
7383
String pointer = sb.toString();
7484
if (pointer.length() == 0) {

core/src/test/java/org/everit/json/schema/internal/RelativeJsonPointerFormatValidatorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public void onlyUpwardsStepCount() {
2020
assertSuccess("1", SUBJECT);
2121
}
2222

23+
@Test
24+
public void onlyUpwardsCount_multipleDigits() {
25+
assertSuccess("234", SUBJECT);
26+
}
27+
2328
@Test
2429
public void upwardsStepCountWithJsonPointer() {
2530
assertSuccess("23/foo/bar", SUBJECT);
@@ -65,4 +70,9 @@ public void upwardsStepCountFollowedByURLFormJsonPointer() {
6570
assertFailure("123#/a/b", SUBJECT, "[123#/a/b] is not a valid relative JSON Pointer");
6671
}
6772

73+
@Test
74+
public void noUpwardsStepCount() {
75+
assertFailure("/foo/bar", SUBJECT, "[/foo/bar] is not a valid relative JSON Pointer");
76+
}
77+
6878
}

0 commit comments

Comments
 (0)