1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

binary: avoid alloc in reading integer

This commit is contained in:
wdvxdr 2022-03-22 21:34:05 +08:00
parent aa657c0f09
commit 4314fdcb39
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
3 changed files with 19 additions and 21 deletions

View File

@ -49,7 +49,7 @@ func (r *JceReader) skipHead() {
} }
func (r *JceReader) skip(l int) { func (r *JceReader) skip(l int) {
r.skipBytes(l) r.off += l
} }
func (r *JceReader) skipField(t byte) { func (r *JceReader) skipField(t byte) {
@ -105,17 +105,6 @@ func (r *JceReader) readBytes(n int) []byte {
return b return b
} }
func (r *JceReader) skipBytes(n int) {
if r.off+n > len(r.buf) {
panic("skipBytes: EOF")
}
lremain := len(r.buf[r.off:])
if lremain < n {
n = lremain
}
r.off += n
}
func (r *JceReader) readByte() byte { func (r *JceReader) readByte() byte {
if r.off >= len(r.buf) { if r.off >= len(r.buf) {
panic("readByte: EOF") panic("readByte: EOF")
@ -126,15 +115,21 @@ func (r *JceReader) readByte() byte {
} }
func (r *JceReader) readUInt16() uint16 { func (r *JceReader) readUInt16() uint16 {
return goBinary.BigEndian.Uint16(r.readBytes(2)) b := make([]byte, 2)
r.off += copy(b, r.buf[r.off:])
return goBinary.BigEndian.Uint16(b)
} }
func (r *JceReader) readUInt32() uint32 { func (r *JceReader) readUInt32() uint32 {
return goBinary.BigEndian.Uint32(r.readBytes(4)) b := make([]byte, 4)
r.off += copy(b, r.buf[r.off:])
return goBinary.BigEndian.Uint32(b)
} }
func (r *JceReader) readUInt64() uint64 { func (r *JceReader) readUInt64() uint64 {
return goBinary.BigEndian.Uint64(r.readBytes(8)) b := make([]byte, 8)
r.off += copy(b, r.buf[r.off:])
return goBinary.BigEndian.Uint64(b)
} }
func (r *JceReader) readFloat32() float32 { func (r *JceReader) readFloat32() float32 {

View File

@ -52,17 +52,20 @@ func (r *Reader) ReadBytesShort() []byte {
} }
func (r *Reader) ReadUInt16() uint16 { func (r *Reader) ReadUInt16() uint16 {
b := r.ReadBytes(2) b := make([]byte, 2)
_, _ = r.buf.Read(b)
return binary.BigEndian.Uint16(b) return binary.BigEndian.Uint16(b)
} }
func (r *Reader) ReadInt32() int32 { func (r *Reader) ReadInt32() int32 {
b := r.ReadBytes(4) b := make([]byte, 4)
_, _ = r.buf.Read(b)
return int32(binary.BigEndian.Uint32(b)) return int32(binary.BigEndian.Uint32(b))
} }
func (r *Reader) ReadInt64() int64 { func (r *Reader) ReadInt64() int64 {
b := r.ReadBytes(8) b := make([]byte, 8)
_, _ = r.buf.Read(b)
return int64(binary.BigEndian.Uint64(b)) return int64(binary.BigEndian.Uint64(b))
} }
@ -154,7 +157,8 @@ func (r *NetworkReader) ReadBytes(len int) ([]byte, error) {
} }
func (r *NetworkReader) ReadInt32() (int32, error) { func (r *NetworkReader) ReadInt32() (int32, error) {
b, err := r.ReadBytes(4) b := make([]byte, 4)
_, err := r.conn.Read(b)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -48,9 +48,8 @@ func (c *QQClient) getGtk(domain string) int {
accu = accu + (accu << 5) + int(b) accu = accu + (accu << 5) + int(b)
} }
return 2147483647 & accu return 2147483647 & accu
} else {
return 0
} }
return 0
} }
func (c *QQClient) GetModelShow(modelName string) ([]*ModelVariant, error) { func (c *QQClient) GetModelShow(modelName string) ([]*ModelVariant, error) {