178 lines
3.5 KiB
Go
178 lines
3.5 KiB
Go
// Copyright 2010 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package edn
|
|
|
|
import (
|
|
"reflect"
|
|
"strconv"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// getu4 decodes \uXXXX from the beginning of s, returning the hex value,
|
|
// or it returns -1.
|
|
func getu4(s []byte) rune {
|
|
if len(s) < 6 || s[0] != '\\' || s[1] != 'u' {
|
|
return -1
|
|
}
|
|
r, err := strconv.ParseUint(string(s[2:6]), 16, 64)
|
|
if err != nil {
|
|
return -1
|
|
}
|
|
return rune(r)
|
|
}
|
|
|
|
// indirect walks down v allocating pointers as needed,
|
|
// until it gets to a non-pointer.
|
|
// if it encounters an Unmarshaler, indirect stops and returns that.
|
|
// if decodingNull is true, indirect stops at the last pointer so it can be set to nil.
|
|
func (d *Decoder) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, reflect.Value) {
|
|
// If v is a named type and is addressable,
|
|
// start with its address, so that if the type has pointer methods,
|
|
// we find them.
|
|
if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
|
|
v = v.Addr()
|
|
}
|
|
for {
|
|
// Load value from interface, but only if the result will be
|
|
// usefully addressable.
|
|
if v.Kind() == reflect.Interface && !v.IsNil() {
|
|
e := v.Elem()
|
|
if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
|
|
v = e
|
|
continue
|
|
}
|
|
}
|
|
|
|
if v.Kind() != reflect.Ptr {
|
|
break
|
|
}
|
|
|
|
if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
|
|
break
|
|
}
|
|
if v.IsNil() {
|
|
v.Set(reflect.New(v.Type().Elem()))
|
|
}
|
|
if v.Type().NumMethod() > 0 {
|
|
if u, ok := v.Interface().(Unmarshaler); ok {
|
|
return u, reflect.Value{}
|
|
}
|
|
}
|
|
v = v.Elem()
|
|
}
|
|
return nil, v
|
|
}
|
|
|
|
// unquote converts a quoted EDN string literal s into an actual string t.
|
|
// The rules are different than for Go, so cannot use strconv.Unquote.
|
|
func unquote(s []byte) (t string, ok bool) {
|
|
s, ok = unquoteBytes(s)
|
|
t = string(s)
|
|
return
|
|
}
|
|
|
|
func unquoteBytes(s []byte) (t []byte, ok bool) {
|
|
if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
|
|
return
|
|
}
|
|
s = s[1 : len(s)-1]
|
|
|
|
// Check for unusual characters. If there are none,
|
|
// then no unquoting is needed, so return a slice of the
|
|
// original bytes.
|
|
r := 0
|
|
for r < len(s) {
|
|
c := s[r]
|
|
if c == '\\' || c == '"' {
|
|
break
|
|
}
|
|
if c < utf8.RuneSelf {
|
|
r++
|
|
continue
|
|
}
|
|
rr, size := utf8.DecodeRune(s[r:])
|
|
if rr == utf8.RuneError && size == 1 {
|
|
break
|
|
}
|
|
r += size
|
|
}
|
|
if r == len(s) {
|
|
return s, true
|
|
}
|
|
|
|
b := make([]byte, len(s)+2*utf8.UTFMax)
|
|
w := copy(b, s[0:r])
|
|
for r < len(s) {
|
|
// Out of room? Can only happen if s is full of
|
|
// malformed UTF-8 and we're replacing each
|
|
// byte with RuneError.
|
|
if w >= len(b)-2*utf8.UTFMax {
|
|
nb := make([]byte, (len(b)+utf8.UTFMax)*2)
|
|
copy(nb, b[0:w])
|
|
b = nb
|
|
}
|
|
switch c := s[r]; {
|
|
case c == '\\':
|
|
r++
|
|
if r >= len(s) {
|
|
return
|
|
}
|
|
switch s[r] {
|
|
default:
|
|
return
|
|
case '"', '\\', '/', '\'':
|
|
b[w] = s[r]
|
|
r++
|
|
w++
|
|
case 'b':
|
|
b[w] = '\b'
|
|
r++
|
|
w++
|
|
case 'f':
|
|
b[w] = '\f'
|
|
r++
|
|
w++
|
|
case 'n':
|
|
b[w] = '\n'
|
|
r++
|
|
w++
|
|
case 'r':
|
|
b[w] = '\r'
|
|
r++
|
|
w++
|
|
case 't':
|
|
b[w] = '\t'
|
|
r++
|
|
w++
|
|
case 'u':
|
|
r--
|
|
rr := getu4(s[r:])
|
|
if rr < 0 {
|
|
return
|
|
}
|
|
r += 6
|
|
w += utf8.EncodeRune(b[w:], rr)
|
|
}
|
|
|
|
// Quote is invalid
|
|
case c == '"':
|
|
return
|
|
|
|
// ASCII
|
|
case c < utf8.RuneSelf:
|
|
b[w] = c
|
|
r++
|
|
w++
|
|
|
|
// Coerce to well-formed UTF-8.
|
|
default:
|
|
rr, size := utf8.DecodeRune(s[r:])
|
|
r += size
|
|
w += utf8.EncodeRune(b[w:], rr)
|
|
}
|
|
}
|
|
return b[0:w], true
|
|
}
|