-
Notifications
You must be signed in to change notification settings - Fork 41
/
pointer.go
139 lines (119 loc) · 2.66 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
package jsondiff
import (
"errors"
"strconv"
"strings"
"unsafe"
)
const (
separator = '/'
escapeSlash = "~1"
escapeTilde = "~0"
emptyPointer = ""
)
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("~", escapeTilde, "/", escapeSlash)
// rfc6901Unescaper is a replacer that unescape a JSON Pointer string.
rfc6901Unescaper = strings.NewReplacer(escapeTilde, "~", escapeSlash, "/")
)
type segment struct {
key string
idx int
}
// pointer represents an RFC 6901 JSON Pointer.
type pointer struct {
buf []byte
base segment
prev segment
sep int
}
func (p *pointer) clone() pointer {
return *p
}
func (p *pointer) copy() string {
return string(p.buf)
}
func (p *pointer) string() string {
return *(*string)(unsafe.Pointer(&p.buf))
}
func (p *pointer) isRoot() bool {
return len(p.buf) == 0
}
func (p *pointer) appendKey(key string) {
p.buf = append(p.buf, separator)
p.base = segment{key: key}
p.appendEscapeKey(key)
}
func (p *pointer) appendIndex(idx int) {
p.buf = append(p.buf, separator)
p.buf = strconv.AppendInt(p.buf, int64(idx), 10)
p.base = segment{idx: idx}
}
func (p *pointer) snapshot() {
p.sep = len(p.buf)
p.prev = p.base
}
func (p *pointer) rewind() {
p.buf = p.buf[:p.sep]
p.base = p.prev
}
func (p *pointer) reset() {
p.buf = p.buf[:0]
p.sep = 0
}
func (p *pointer) appendEscapeKey(k string) {
for _, c := range []byte(k) {
switch c {
case '/':
p.buf = append(p.buf, escapeSlash...)
case '~':
p.buf = append(p.buf, escapeTilde...)
default:
p.buf = append(p.buf, c)
}
}
}
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 {
switch {
case 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
case r == '~':
if i == len(a)-1 {
return nil, errIncompleteEscapeSequence
}
if a[i+1] != '0' && a[i+1] != '1' {
return nil, errInvalidEscapeSequence
}
case i == len(a)-1:
// End of string, accumulate from last separator.
tokens = append(tokens, string(a[ls+1:]))
}
}
return tokens, nil
}