44 "bytes"
55 "errors"
66 "fmt"
7- "reflect"
87 "strconv"
9- "unsafe"
108)
119
1210// Errors
@@ -127,10 +125,10 @@ func searchKeys(data []byte, keys ...string) int {
127125
128126 // if string is a Key, and key level match
129127 if data [i ] == ':' {
130- key := unsafeBytesToString ( data [keyBegin :keyEnd ])
128+ key := data [keyBegin :keyEnd ]
131129
132130 if keyLevel == level - 1 && // If key nesting level match current object nested level
133- keys [level - 1 ] == key {
131+ equalStr ( & key , keys [level - 1 ]) {
134132 keyLevel ++
135133 // If we found all keys in path
136134 if keyLevel == lk {
@@ -351,7 +349,7 @@ func GetUnsafeString(data []byte, keys ...string) (val string, err error) {
351349 return "" , e
352350 }
353351
354- return unsafeBytesToString ( v ), nil
352+ return bytesToString ( & v ), nil
355353}
356354
357355// GetString returns the value retrieved by `Get`, cast to a string if possible, trying to properly handle escape and utf8 symbols
@@ -372,7 +370,7 @@ func GetString(data []byte, keys ...string) (val string, err error) {
372370 return string (v ), nil
373371 }
374372
375- s , err := strconv .Unquote (`"` + unsafeBytesToString (v ) + `"` )
373+ s , err := strconv .Unquote (`"` + string (v ) + `"` )
376374
377375 return s , err
378376}
@@ -391,7 +389,7 @@ func GetFloat(data []byte, keys ...string) (val float64, err error) {
391389 return 0 , fmt .Errorf ("Value is not a number: %s" , string (v ))
392390 }
393391
394- val , err = strconv . ParseFloat ( unsafeBytesToString ( v ), 64 )
392+ val , err = parseFloat ( & v )
395393 return
396394}
397395
@@ -408,8 +406,11 @@ func GetInt(data []byte, keys ...string) (val int64, err error) {
408406 return 0 , fmt .Errorf ("Value is not a number: %s" , string (v ))
409407 }
410408
411- val , err = strconv .ParseInt (unsafeBytesToString (v ), 10 , 64 )
412- return
409+ if val , ok := parseInt (v ); ! ok {
410+ return 0 , MalformedValueError
411+ } else {
412+ return val , nil
413+ }
413414}
414415
415416// GetBoolean returns the value retrieved by `Get`, cast to a bool if possible.
@@ -434,11 +435,3 @@ func GetBoolean(data []byte, keys ...string) (val bool, err error) {
434435
435436 return
436437}
437-
438- // A hack until issue golang/go#2632 is fixed.
439- // See: https://github.com/golang/go/issues/2632
440- func unsafeBytesToString (data []byte ) string {
441- h := (* reflect .SliceHeader )(unsafe .Pointer (& data ))
442- sh := reflect.StringHeader {Data : h .Data , Len : h .Len }
443- return * (* string )(unsafe .Pointer (& sh ))
444- }
0 commit comments