2019-05-13 01:27:26 +02:00
|
|
|
package jsn
|
2019-05-09 01:03:18 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2019-05-12 07:36:52 +02:00
|
|
|
|
|
|
|
"github.com/cespare/xxhash/v2"
|
2019-05-09 01:03:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
|
|
|
|
if len(from) != len(to) {
|
|
|
|
return errors.New("'from' and 'to' must be of the same length")
|
|
|
|
}
|
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
h := xxhash.New()
|
|
|
|
tmap := make(map[uint64]int, len(from))
|
2019-05-09 01:03:18 +02:00
|
|
|
|
|
|
|
for i, f := range from {
|
2019-05-12 07:36:52 +02:00
|
|
|
h.Write(f.Key)
|
|
|
|
h.Write(f.Value)
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
tmap[h.Sum64()] = i
|
|
|
|
h.Reset()
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
s, e, d := 0, 0, 0
|
2019-05-12 07:36:52 +02:00
|
|
|
|
|
|
|
state := expectKey
|
|
|
|
ws, we := -1, len(b)
|
2019-05-09 05:52:04 +02:00
|
|
|
|
2019-05-09 01:03:18 +02:00
|
|
|
for i := 0; i < len(b); i++ {
|
2019-05-09 05:52:04 +02:00
|
|
|
// skip any left padding whitespace
|
2019-05-12 07:36:52 +02:00
|
|
|
if ws == -1 && (b[i] == '{' || b[i] == '[') {
|
2019-05-09 01:03:18 +02:00
|
|
|
ws = 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 state == expectObjClose || state == expectListClose {
|
|
|
|
switch b[i] {
|
|
|
|
case '{', '[':
|
|
|
|
d++
|
|
|
|
case '}', ']':
|
|
|
|
d--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2019-05-09 01:03:18 +02:00
|
|
|
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
|
|
|
|
|
|
|
case state == expectKeyClose && b[i] == '"':
|
|
|
|
state = expectColon
|
2019-05-12 07:36:52 +02:00
|
|
|
h.Write(b[(s + 1):i])
|
|
|
|
we = s
|
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
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
case state == expectString && 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 = 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] != '-')):
|
|
|
|
i--
|
|
|
|
e = i
|
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-09 01:03:18 +02:00
|
|
|
|
2019-05-09 05:52:04 +02:00
|
|
|
if e != 0 {
|
|
|
|
e++
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
h.Write(b[s:e])
|
|
|
|
n, ok := tmap[h.Sum64()]
|
|
|
|
h.Reset()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
if _, err := w.Write(b[ws:(we + 1)]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
if len(to[n].Key) != 0 {
|
|
|
|
var err error
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
if _, err := w.Write(to[n].Key); err != nil {
|
2019-05-09 05:52:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := w.WriteString(`":`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-12 07:36:52 +02:00
|
|
|
if len(to[n].Value) != 0 {
|
|
|
|
_, err = w.Write(to[n].Value)
|
|
|
|
} else {
|
|
|
|
_, err = w.WriteString("null")
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-05-09 05:52:04 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-05-12 07:36:52 +02:00
|
|
|
|
|
|
|
ws = e
|
|
|
|
} else if b[e] == ',' {
|
|
|
|
ws = e + 1
|
|
|
|
} else {
|
2019-05-09 05:52:04 +02:00
|
|
|
ws = e
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
2019-05-09 05:52:04 +02:00
|
|
|
}
|
2019-05-09 01:03:18 +02:00
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
if !ok && (b[s] == '[' || b[s] == '{') {
|
2019-05-09 05:52:04 +02:00
|
|
|
// the i++ in the for loop will add 1 so we account for that (s - 1)
|
|
|
|
i = s - 1
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
2019-05-09 05:52:04 +02:00
|
|
|
|
|
|
|
state = expectKey
|
|
|
|
we = len(b)
|
|
|
|
e = 0
|
|
|
|
d = 0
|
2019-05-09 01:03:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 07:36:52 +02:00
|
|
|
if ws == -1 || (ws == 0 && we == len(b)) {
|
|
|
|
w.Write(b)
|
|
|
|
} else {
|
|
|
|
w.Write(b[ws:we])
|
|
|
|
}
|
|
|
|
|
2019-05-09 01:03:18 +02:00
|
|
|
return nil
|
|
|
|
}
|