package yu_pack // Pack // @Description: Pack 底层实现 type Pack struct { // 每次扩容的字节数 grow_size int buf []byte get_i int } // New // // @Description: 创建 Pack 对象 func New() (t *Pack) { t = &Pack{} t.grow_size = 128 return } // SetGrowSize // // @Description: 设置每次扩容的尺寸 func (t *Pack) SetGrowSize(size int) *Pack { t.grow_size = size return t } // Grow // // @Description: 将缓冲区复制到一个新的、更大的缓冲区,以便至少有 size 个超过 len(this.buf) 的容量字节。 func (t *Pack) Grow(size int) *Pack { if len(t.buf) == 0 && cap(t.buf) == 0 { t.buf = make([]byte, 0, size+t.grow_size) return t } if len(t.buf)+size > cap(t.buf) { j_buf := make([]byte, len(t.buf), size+cap(t.buf)+t.grow_size) copy(j_buf, t.buf) t.buf = j_buf } return t } // -----------------------------------------------------------------------------------------------------基础 // SetBytes // // @Description: 置缓冲区数据 func (t *Pack) SetBytes(buf []byte) *Pack { t.buf = nil t.Grow(len(buf)) t.buf = append(t.buf, buf...) t.get_i = 0 return t } // String // // @Description: 返回缓冲区总数据的字符串 func (t *Pack) String() string { return string(t.buf) } // Bytes // // @Description: 返回缓冲区总数据 func (t *Pack) Bytes() []byte { j_buf := make([]byte, len(t.buf)) copy(j_buf, t.buf) return j_buf } // Len // // @Description: 返回缓冲区总长度 func (t *Pack) Len() int { return len(t.buf) } // Cap returns the capacity of the builder's underlying byte slice. It is the // total space allocated for the string being built and includes any bytes // already written. func (t *Pack) Cap() int { return cap(t.buf) } // Reset // // @Description: 重置缓冲区 func (t *Pack) Reset() *Pack { t.buf = nil t.get_i = 0 return t }