2019-05-13 01:27:26 +02:00
|
|
|
package jsn
|
2019-05-09 01:03:18 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
)
|
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
func Strip(b []byte, path [][]byte) []byte {
|
2019-05-09 05:52:04 +02:00
|
|
|
s, e, d := 0, 0, 0
|
2019-05-12 07:36:52 +02:00
|
|
|
|
|
|
|
ob := b
|
|
|
|
pi := 0
|
2019-05-09 05:52:04 +02:00
|
|
|
pm := false
|
2019-05-12 07:36:52 +02:00
|
|
|
state := expectKey
|
2020-02-23 21:29:50 +01:00
|
|
|
instr := false
|
2020-03-30 16:03:47 +02:00
|
|
|
slash := 0
|
2019-05-09 05:52:04 +02:00
|
|
|
|
2019-05-09 01:03:18 +02:00
|
|
|
for i := 0; i < len(b); i++ {
|
2020-03-30 16:03:47 +02:00
|
|
|
if instr && b[i] == '\\' {
|
|
|
|
slash++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if b[i] == '"' && (slash%2 == 0) {
|
|
|
|
instr = !instr
|
|
|
|
}
|
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
if state == expectObjClose || state == expectListClose {
|
2020-02-23 21:29:50 +01:00
|
|
|
if !instr {
|
|
|
|
switch b[i] {
|
|
|
|
case '{', '[':
|
|
|
|
d++
|
|
|
|
case '}', ']':
|
|
|
|
d--
|
|
|
|
}
|
2019-05-09 05:52:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 01:03:18 +02:00
|
|
|
switch {
|
|
|
|
case state == expectKey && b[i] == '"':
|
|
|
|
state = expectKeyClose
|
2019-05-09 05:52:04 +02:00
|
|
|
s = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2020-03-30 16:03:47 +02:00
|
|
|
case state == expectKeyClose && (b[i] == '"' && (slash%2 == 0)):
|
2019-05-09 01:03:18 +02:00
|
|
|
state = expectColon
|
2019-05-12 07:36:52 +02:00
|
|
|
if pi == len(path) {
|
|
|
|
pi = 0
|
2019-05-09 05:52:04 +02:00
|
|
|
}
|
2019-05-12 07:36:52 +02:00
|
|
|
pm = bytes.Equal(b[(s+1):i], path[pi])
|
2019-05-09 05:52:04 +02:00
|
|
|
if pm {
|
2019-05-12 07:36:52 +02:00
|
|
|
pi++
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectColon && b[i] == ':':
|
|
|
|
state = expectValue
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectValue && b[i] == '"':
|
|
|
|
state = expectString
|
|
|
|
s = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2020-03-30 16:03:47 +02:00
|
|
|
case state == expectString && (b[i] == '"' && (slash%2 == 0)):
|
2019-05-09 05:52:04 +02:00
|
|
|
e = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectValue && b[i] == '[':
|
|
|
|
state = expectListClose
|
|
|
|
s = i
|
|
|
|
d++
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectListClose && d == 0 && b[i] == ']':
|
|
|
|
e = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectValue && b[i] == '{':
|
|
|
|
state = expectObjClose
|
|
|
|
s = i
|
|
|
|
d++
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectObjClose && d == 0 && b[i] == '}':
|
|
|
|
e = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectValue && (b[i] >= '0' && b[i] <= '9'):
|
|
|
|
state = expectNumClose
|
|
|
|
s = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectNumClose &&
|
|
|
|
((b[i] < '0' || b[i] > '9') &&
|
|
|
|
(b[i] != '.' && b[i] != 'e' && b[i] != 'E' && b[i] != '+' && b[i] != '-')):
|
2019-10-26 09:02:05 +02:00
|
|
|
e = i - 1
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectValue &&
|
|
|
|
(b[i] == 'f' || b[i] == 'F' || b[i] == 't' || b[i] == 'T'):
|
|
|
|
state = expectBoolClose
|
|
|
|
s = i
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectBoolClose && (b[i] == 'e' || b[i] == 'E'):
|
|
|
|
e = i
|
2019-05-13 06:05:08 +02:00
|
|
|
|
|
|
|
case state == expectValue && b[i] == 'n':
|
|
|
|
state = expectNull
|
2020-02-10 07:45:37 +01:00
|
|
|
s = i
|
2019-05-13 06:05:08 +02:00
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
case state == expectNull && (b[i-1] == 'l' && b[i] == 'l'):
|
2019-05-13 06:05:08 +02:00
|
|
|
e = i
|
2019-05-09 05:52:04 +02:00
|
|
|
}
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
if e != 0 {
|
|
|
|
if pm && (b[s] == '[' || b[s] == '{') {
|
|
|
|
b = b[s:(e + 1)]
|
2019-05-09 01:03:18 +02:00
|
|
|
i = 0
|
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
if pi == len(path) {
|
2019-05-09 01:03:18 +02:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 05:52:04 +02:00
|
|
|
|
|
|
|
state = expectKey
|
|
|
|
e = 0
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
2020-03-30 16:03:47 +02:00
|
|
|
|
|
|
|
slash = 0
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
return ob
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|