Skip to content

Commit

Permalink
Reuse own constant and refactor magic chars into new constants for (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Feb 15, 2024
1 parent c6b5649 commit f16a739
Showing 1 changed file with 51 additions and 14 deletions.
65 changes: 51 additions & 14 deletions src/main/java/com/fasterxml/jackson/core/JsonPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,46 @@
*/
public class JsonPointer implements Serializable
{
/**
* Escape character {@value #ESC} per <a href="https://datatracker.ietf.org/doc/html/rfc6901">RFC6901</a>.
* <pre>
* escaped = "~" ( "0" / "1" )
* ; representing '~' and '/', respectively
* </pre>
*
* @since 2.17
*/
public static final char ESC = '~';

/**
* Escaped slash string {@value #ESC_TILDE} per <a href="https://datatracker.ietf.org/doc/html/rfc6901">RFC6901</a>.
* <pre>
* escaped = "~" ( "0" / "1" )
* ; representing '~' and '/', respectively
* </pre>
*
* @since 2.17
*/
public static final String ESC_SLASH = "~1";

/**
* Escaped tilde string {@value #ESC_TILDE} per <a href="https://datatracker.ietf.org/doc/html/rfc6901">RFC6901</a>.
* <pre>
* escaped = "~" ( "0" / "1" )
* ; representing '~' and '/', respectively
* </pre>
*
* @since 2.17
*/
public static final String ESC_TILDE = "~0";

private static final long serialVersionUID = 1L;

/**
* Character used to separate segments.
* <pre>
* json-pointer = *( "/" reference-token )
* </pre>
*
* @since 2.9
*/
Expand Down Expand Up @@ -158,7 +194,7 @@ public static JsonPointer compile(String expr) throws IllegalArgumentException
return EMPTY;
}
// And then quick validity check:
if (expr.charAt(0) != '/') {
if (expr.charAt(0) != SEPARATOR) {
throw new IllegalArgumentException("Invalid input: JSON Pointer expression must start with '/': "+"\""+expr+"\"");
}
return _parseTail(expr);
Expand Down Expand Up @@ -249,7 +285,7 @@ public static JsonPointer forPath(JsonStreamContext context,
// Let's find the last segment as well, for reverse traversal
last = next;
next.pathOffset = pathBuilder.length();
pathBuilder.append('/');
pathBuilder.append(SEPARATOR);
if (next.property != null) {
_appendEscaped(pathBuilder, next.property);
} else {
Expand Down Expand Up @@ -281,12 +317,12 @@ private static void _appendEscaped(StringBuilder sb, String segment)
{
for (int i = 0, end = segment.length(); i < end; ++i) {
char c = segment.charAt(i);
if (c == '/') {
sb.append("~1");
if (c == SEPARATOR) {
sb.append(ESC_SLASH);
continue;
}
if (c == '~') {
sb.append("~0");
if (c == ESC) {
sb.append(ESC_TILDE);
continue;
}
sb.append(c);
Expand Down Expand Up @@ -417,7 +453,7 @@ public JsonPointer appendProperty(String property) {
}
// 14-Dec-2023, tatu: [core#1145] Must escape `property`; accept empty String
// as valid segment to match as well
StringBuilder sb = toStringBuilder(property.length() + 2).append('/');
StringBuilder sb = toStringBuilder(property.length() + 2).append(SEPARATOR);
_appendEscaped(sb, property);
return compile(sb.toString());
}
Expand Down Expand Up @@ -578,6 +614,7 @@ public String toString() {
*
* @param slack Number of characters to reserve in StringBuilder beyond
* minimum copied
* @return a new StringBuilder
*
* @since 2.17
*/
Expand Down Expand Up @@ -685,7 +722,7 @@ protected static JsonPointer _parseTail(final String fullPath)

while (i < end) {
char c = fullPath.charAt(i);
if (c == '/') { // common case, got a segment
if (c == SEPARATOR) { // common case, got a segment
parent = new PointerParent(parent, startOffset,
fullPath.substring(startOffset + 1, i));
startOffset = i;
Expand All @@ -694,7 +731,7 @@ protected static JsonPointer _parseTail(final String fullPath)
}
++i;
// quoting is different; offline this case
if (c == '~' && i < end) { // possibly, quote
if (c == ESC && i < end) { // possibly, quote
// 04-Oct-2022, tatu: Let's decode escaped segment
// instead of recursive call
StringBuilder sb = new StringBuilder(32);
Expand Down Expand Up @@ -749,11 +786,11 @@ protected static int _extractEscapedSegment(String input, int firstCharOffset,
_appendEscape(sb, input.charAt(i++));
while (i < end) {
char c = input.charAt(i);
if (c == '/') { // end is nigh!
if (c == SEPARATOR) { // end is nigh!
return i;
}
++i;
if (c == '~' && i < end) {
if (c == ESC && i < end) {
_appendEscape(sb, input.charAt(i++));
continue;
}
Expand All @@ -765,11 +802,11 @@ protected static int _extractEscapedSegment(String input, int firstCharOffset,

private static void _appendEscape(StringBuilder sb, char c) {
if (c == '0') {
c = '~';
c = ESC;
} else if (c == '1') {
c = '/';
c = SEPARATOR;
} else {
sb.append('~');
sb.append(ESC);
}
sb.append(c);
}
Expand Down

0 comments on commit f16a739

Please sign in to comment.