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+ candidateKey := data [keyBegin :keyEnd ]
131129
132130 if keyLevel == level - 1 && // If key nesting level match current object nested level
133- keys [level - 1 ] == key {
131+ BytesEqualStr ( & candidateKey , keys [level - 1 ]) {
134132 keyLevel ++
135133 // If we found all keys in path
136134 if keyLevel == lk {
@@ -343,17 +341,6 @@ func ArrayEach(data []byte, cb func(value []byte, dataType ValueType, offset int
343341 return nil
344342}
345343
346- // GetUnsafeString returns the value retrieved by `Get`, use creates string without memory allocation by mapping string to slice memory. It does not handle escape symbols.
347- func GetUnsafeString (data []byte , keys ... string ) (val string , err error ) {
348- v , _ , _ , e := Get (data , keys ... )
349-
350- if e != nil {
351- return "" , e
352- }
353-
354- return unsafeBytesToString (v ), nil
355- }
356-
357344// GetString returns the value retrieved by `Get`, cast to a string if possible, trying to properly handle escape and utf8 symbols
358345// If key data type do not match, it will return an error.
359346func GetString (data []byte , keys ... string ) (val string , err error ) {
@@ -372,7 +359,7 @@ func GetString(data []byte, keys ...string) (val string, err error) {
372359 return string (v ), nil
373360 }
374361
375- s , err := strconv .Unquote (`"` + unsafeBytesToString (v ) + `"` )
362+ s , err := strconv .Unquote (`"` + string (v ) + `"` )
376363
377364 return s , err
378365}
@@ -391,7 +378,7 @@ func GetFloat(data []byte, keys ...string) (val float64, err error) {
391378 return 0 , fmt .Errorf ("Value is not a number: %s" , string (v ))
392379 }
393380
394- val , err = strconv . ParseFloat ( unsafeBytesToString ( v ) , 64 )
381+ val , err = BytesParseFloat ( & v , 64 )
395382 return
396383}
397384
@@ -408,8 +395,11 @@ func GetInt(data []byte, keys ...string) (val int64, err error) {
408395 return 0 , fmt .Errorf ("Value is not a number: %s" , string (v ))
409396 }
410397
411- val , err = strconv .ParseInt (unsafeBytesToString (v ), 10 , 64 )
412- return
398+ if val , ok := BytesParseInt (v ); ! ok {
399+ return 0 , MalformedValueError
400+ } else {
401+ return val , nil
402+ }
413403}
414404
415405// GetBoolean returns the value retrieved by `Get`, cast to a bool if possible.
@@ -434,11 +424,3 @@ func GetBoolean(data []byte, keys ...string) (val bool, err error) {
434424
435425 return
436426}
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