ReadString reads the string value for the tag and the require or optional sign.
(data *string, tag byte, require bool)
| 835 | |
| 836 | // ReadString reads the string value for the tag and the require or optional sign. |
| 837 | func (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 |
| 869 | func (b *Reader) ToString() string { |