mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 19:17:38 +08:00
binary: avoid alloc in reading integer
This commit is contained in:
parent
aa657c0f09
commit
4314fdcb39
@ -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 {
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user