MCPcopy
hub / github.com/TarsCloud/TarsGo / ReadString

Method ReadString

tars/protocol/codec/codec.go:837–866  ·  view source on GitHub ↗

ReadString reads the string value for the tag and the require or optional sign.

(data *string, tag byte, require bool)

Source from the content-addressed store, hash-verified

835
836// ReadString reads the string value for the tag and the require or optional sign.
837func (b *Reader) ReadString(data *string, tag byte, require bool) error {
838 have, ty, err := b.SkipToNoCheck(tag, require)
839 if err != nil {
840 return err
841 }
842 if !have {
843 return nil
844 }
845
846 if ty == STRING4 {
847 var length uint32
848 err = bReadU32(b.buf, &length)
849 if err != nil {
850 return fmt.Errorf("read string4 tag:%d error:%v", tag, err)
851 }
852 buff := b.Next(int(length))
853 *data = string(buff)
854 } else if ty == STRING1 {
855 var length uint8
856 err = bReadU8(b.buf, &length)
857 if err != nil {
858 return fmt.Errorf("read string1 tag:%d error:%v", tag, err)
859 }
860 buff := b.Next(int(length))
861 *data = string(buff)
862 } else {
863 return fmt.Errorf("need string, tag:%d, but type is %s", tag, getTypeStr(int(ty)))
864 }
865 return nil
866}
867
868// ToString make the reader to string
869func (b *Reader) ToString() string {

Callers 15

DispatchMethod · 0.95
ListConfigWithContextMethod · 0.95
LoadConfigWithContextMethod · 0.95
DispatchMethod · 0.95
DispatchMethod · 0.95
NotifyWithContextMethod · 0.95

Calls 6

SkipToNoCheckMethod · 0.95
NextMethod · 0.95
bReadU32Function · 0.85
bReadU8Function · 0.85
getTypeStrFunction · 0.85
ErrorfMethod · 0.45

Tested by 2

TestSkipStringFunction · 0.64
BenchmarkStringFunction · 0.64