forked from wI2L/jsondiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer.go
147 lines (128 loc) · 2.87 KB
/
pointer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package jsondiff
import (
"errors"
"strconv"
"strings"
"unicode/utf8"
"unsafe"
)
const (
separator = '/'
emptyPtr = ""
escapeSlash = "~1"
escapeTilde = "~0"
)
var (
// rfc6901Escaper is a replacer that escapes a JSON
// Pointer string in compliance with the JavaScript
// Object Notation Pointer syntax.
// https://tools.ietf.org/html/rfc6901
rfc6901Escaper = strings.NewReplacer("~", "~0", "/", "~1")
// dotPathReplacer converts a RFC6901 JSON pointer to
// a JSON path, while also escaping any existing dot
// characters present in the original pointer.
dotPathReplacer = strings.NewReplacer(".", "\\.", "/", ".")
)
// pointer represents an RFC 6901 JSON Pointer.
type pointer struct {
buf []byte
end int
}
func (p pointer) string() string {
return *(*string)(unsafe.Pointer(&p.buf))
}
func (p pointer) copy() string {
return string(p.buf)
}
func (p pointer) snapshot() pointer {
return pointer{
buf: p.buf,
end: len(p.buf),
}
}
func (p pointer) rewind() pointer {
return pointer{
buf: p.buf[:p.end],
end: p.end,
}
}
func (p pointer) appendIndex(idx int) pointer {
p.buf = append(p.buf, separator)
p.buf = strconv.AppendInt(p.buf, int64(idx), 10)
return p
}
func (p pointer) appendKey(key string) pointer {
p.buf = append(p.buf, separator)
return p.appendEscapeKey(key)
}
func (p pointer) appendEscapeKey(k string) pointer {
for _, r := range k {
if r == '/' {
p.buf = append(p.buf, escapeSlash...)
continue
} else if r == '~' {
p.buf = append(p.buf, escapeTilde...)
continue
}
p.buf = utf8.AppendRune(p.buf, r)
}
return p
}
func (p pointer) isRoot() bool {
return len(p.buf) == 0
}
func (p pointer) reset() pointer {
p.buf = p.buf[:0]
p.end = 0
return p
}
func toJSONPath(s string) string {
if len(s) != 0 {
return dotPathReplacer.Replace(s[1:])
}
// @this is a special modifier that can
// be used to retrieve the root path.
return "@this"
}
var (
errLeadingSlash = errors.New("no leading slash")
errIncompleteEscapeSequence = errors.New("incomplete escape sequence")
errInvalidEscapeSequence = errors.New("invalid escape sequence")
)
func parsePointer(s string) ([]string, error) {
if s == "" {
return nil, nil
}
a := []rune(s)
if len(a) > 0 && a[0] != '/' {
return nil, errLeadingSlash
}
var tokens []string
ls := 0
for i, r := range a {
if r == '/' {
if i != 0 {
tokens = append(tokens, string(a[ls+1:i]))
}
if i == len(a)-1 {
// Last char is a '/', next fragment is an empty string.
tokens = append(tokens, "")
break
}
ls = i
} else if r == '~' {
if i == len(a)-1 {
return nil, errIncompleteEscapeSequence
}
if a[i+1] != '0' && a[i+1] != '1' {
return nil, errInvalidEscapeSequence
}
} else {
if i == len(a)-1 {
// End of string, accumulate from last separator.
tokens = append(tokens, string(a[ls+1:]))
}
}
}
return tokens, nil
}