Add Len method for VarInt and VarLong.

This commit is contained in:
Tnze
2023-03-31 13:12:45 +08:00
parent 2e211573fb
commit dd67478593
2 changed files with 85 additions and 0 deletions

View File

@ -290,6 +290,23 @@ func (v *VarInt) ReadFrom(r io.Reader) (n int64, err error) {
return
}
func (v VarInt) Len() int {
switch {
case v < 0:
return MaxVarIntLen
case v < 1<<(7*1):
return 1
case v < 1<<(7*2):
return 2
case v < 1<<(7*3):
return 3
case v < 1<<(7*4):
return 4
default:
return 5
}
}
func (v VarLong) WriteTo(w io.Writer) (n int64, err error) {
vi := make([]byte, 0, MaxVarLongLen)
num := uint64(v)
@ -328,6 +345,31 @@ func (v *VarLong) ReadFrom(r io.Reader) (n int64, err error) {
return
}
func (v VarLong) Len() int {
switch {
case v < 0:
return MaxVarLongLen
case v < 1<<(7*1):
return 1
case v < 1<<(7*2):
return 2
case v < 1<<(7*3):
return 3
case v < 1<<(7*4):
return 4
case v < 1<<(7*5):
return 5
case v < 1<<(7*6):
return 6
case v < 1<<(7*7):
return 7
case v < 1<<(7*8):
return 8
default:
return 9
}
}
func (p Position) WriteTo(w io.Writer) (n int64, err error) {
var b [8]byte
position := uint64(p.X&0x3FFFFFF)<<38 | uint64((p.Z&0x3FFFFFF)<<12) | uint64(p.Y&0xFFF)