-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathescape.go
200 lines (182 loc) · 3.25 KB
/
escape.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2016-2020 Olivier Mengué. All rights reserved.
// Use of this source code is governed by the Apache 2.0 license that
// can be found in the LICENSE file.
package jsonptr
// AppendEscape appends the escaped name to dst and returns it.
// The buffer grows (and so is reallocated) if necessary.
func AppendEscape(dst []byte, name string) []byte {
if len(name) == 0 {
return dst
}
var shift int
for i := 0; i < len(name); i++ {
switch name[i] {
case '~', '/':
shift++
}
}
if shift == 0 {
return append(dst, name...)
}
var result []byte
if cap(dst) >= len(dst)+len(name)+shift {
result = dst[0 : len(dst)+len(name)+shift]
} else {
result = make([]byte, len(dst)+len(name)+shift)
copy(result, dst)
}
b := result[len(dst):]
for i := len(name) - 1; i >= 0; i-- {
switch name[i] {
case '~':
b[i+shift] = '0'
case '/':
b[i+shift] = '1'
default:
b[i+shift] = name[i]
continue
}
shift--
b[i+shift] = '~'
if shift == 0 {
if i > 0 {
copy(b[:i], name[:i])
}
break
}
}
return result
}
// EscapeString escapes a property name with JSON Pointer escapes:
//
// '~' => `~0`
// '/' => `~1`
func EscapeString(name string) string {
var shift int
for i := 0; i < len(name); i++ {
switch name[i] {
case '~', '/':
shift++
}
}
if shift == 0 {
return name
}
b := make([]byte, len(name)+shift)
copy(b, name[:])
for i := len(name) - 1; i >= 0; i-- {
switch b[i] {
case '~':
b[i+shift] = '0'
case '/':
b[i+shift] = '1'
default:
b[i+shift] = b[i]
continue
}
shift--
b[i+shift] = '~'
if shift == 0 {
break
}
}
return string(b)
}
// Unescape unescapes a property name in place:
//
// `~1` => '/'
// `~0` => '~'
//
// Any '~' followed by something else (or nothing) is an error ErrSyntax.
// Any '/' is an error ErrSyntax.
func Unescape(b []byte) ([]byte, error) {
p := -1
Loop:
for q := 0; q < len(b); q++ {
switch b[q] {
case '~':
p = q
break Loop
case '/':
return nil, ErrUsage
}
}
// Nothing to replace
if p == -1 {
return b, nil
}
if b[len(b)-1] == '~' {
return nil, ErrSyntax
}
for q := p; q < len(b); q++ {
switch b[q] {
case '~':
q++
switch b[q] {
case '0':
b[p] = '~'
case '1':
b[p] = '/'
default:
return nil, ErrSyntax
}
case '/':
return nil, ErrUsage
default:
// Move byte
b[p] = b[q]
}
p++
}
return b[:p], nil
}
// UnescapeString unescapes a property name:
//
// `~1` => '/'
// `~0` => '~'
//
// Any '~' followed by something else (or nothing) is an error ErrSyntax.
// If the input contains '/' the result is undefined (may panic).
func UnescapeString(token string) (string, error) {
p := -1
Loop:
for q := 0; q < len(token); q++ {
switch token[q] {
case '~':
p = q
break Loop
case '/':
return "", ErrUsage
}
}
// Nothing to replace
if p == -1 {
return token, nil
}
if token[len(token)-1] == '~' {
return "", ErrSyntax
}
// Copy to a working buffer
b := []byte(token)
for q := p; q < len(token); q++ {
switch b[q] {
case '~':
q++
switch b[q] {
case '0':
b[p] = '~'
case '1':
b[p] = '/'
default:
return "", ErrSyntax
}
case '/':
return "", ErrUsage
default:
// Move byte
b[p] = b[q]
}
p++
}
return string(b[:p]), nil
}