github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/json/decode-unsafe.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package json
     4  
     5  import (
     6  	"bytes"
     7  
     8  	"../convert"
     9  	"../protocol"
    10  )
    11  
    12  // DecoderUnsafe store data to decode data by each method!
    13  type DecoderUnsafe struct {
    14  	Decoder
    15  }
    16  
    17  // DecodeString return string. pass d.Buf start from after " and receive from from after "
    18  func (d *DecoderUnsafe) DecodeString() (s string, err protocol.Error) {
    19  	if d.CheckNullValue() {
    20  		return
    21  	}
    22  
    23  	var loc = bytes.IndexByte(d.Buf, '"')
    24  	d.Buf = d.Buf[loc+1:] // remove any byte before first " due to don't need them
    25  
    26  	loc = bytes.IndexByte(d.Buf, '"')
    27  	if loc < 0 {
    28  		err = ErrEncodedStringCorrupted
    29  		return
    30  	}
    31  
    32  	var slice []byte = d.Buf[:loc]
    33  
    34  	d.Buf = d.Buf[loc+1:]
    35  	s = convert.UnsafeByteSliceToString(slice)
    36  	return
    37  }