package bin
import (
"encoding/binary"
"io"
"math"
)
func (b *Buffer ) PeekID () (uint32 , error ) {
if len (b .Buf ) < Word {
return 0 , io .ErrUnexpectedEOF
}
v := binary .LittleEndian .Uint32 (b .Buf )
return v , nil
}
func (b *Buffer ) PeekN (target []byte , n int ) error {
if len (b .Buf ) < n {
return io .ErrUnexpectedEOF
}
copy (target , b .Buf [:n ])
return nil
}
func (b *Buffer ) ID () (uint32 , error ) {
return b .Uint32 ()
}
func (b *Buffer ) Uint32 () (uint32 , error ) {
v , err := b .PeekID ()
if err != nil {
return 0 , err
}
b .Buf = b .Buf [Word :]
return v , nil
}
func (b *Buffer ) Uint64 () (uint64 , error ) {
const size = Word * 2
if len (b .Buf ) < size {
return 0 , io .ErrUnexpectedEOF
}
v := binary .LittleEndian .Uint64 (b .Buf )
b .Buf = b .Buf [size :]
return v , nil
}
func (b *Buffer ) Int32 () (int32 , error ) {
v , err := b .Uint32 ()
if err != nil {
return 0 , err
}
return int32 (v ), nil
}
func (b *Buffer ) ConsumeN (target []byte , n int ) error {
if err := b .PeekN (target , n ); err != nil {
return err
}
b .Buf = b .Buf [n :]
return nil
}
func (b *Buffer ) Bool () (bool , error ) {
v , err := b .PeekID ()
if err != nil {
return false , err
}
switch v {
case TypeTrue :
b .Buf = b .Buf [Word :]
return true , nil
case TypeFalse :
b .Buf = b .Buf [Word :]
return false , nil
default :
return false , NewUnexpectedID (v )
}
}
func (b *Buffer ) ConsumeID (id uint32 ) error {
v , err := b .PeekID ()
if err != nil {
return err
}
if v != id {
return NewUnexpectedID (v )
}
b .Buf = b .Buf [Word :]
return nil
}
func (b *Buffer ) VectorHeader () (int , error ) {
if err := b .ConsumeID (TypeVector ); err != nil {
return 0 , err
}
n , err := b .Int ()
if err != nil {
return 0 , err
}
if n < 0 {
return 0 , &InvalidLengthError {
Length : n ,
Where : "vector" ,
}
}
return n , nil
}
func (b *Buffer ) String () (string , error ) {
n , v , err := decodeString (b .Buf )
if err != nil {
return "" , err
}
if len (b .Buf ) < n {
return "" , io .ErrUnexpectedEOF
}
b .Buf = b .Buf [n :]
return v , nil
}
func (b *Buffer ) Bytes () ([]byte , error ) {
n , v , err := decodeBytes (b .Buf )
if err != nil {
return nil , err
}
if len (b .Buf ) < n {
return nil , io .ErrUnexpectedEOF
}
b .Buf = b .Buf [n :]
return append ([]byte (nil ), v ...), nil
}
func (b *Buffer ) Int () (int , error ) {
v , err := b .Int32 ()
if err != nil {
return 0 , err
}
return int (v ), nil
}
func (b *Buffer ) Double () (float64 , error ) {
v , err := b .Long ()
if err != nil {
return 0 , err
}
return math .Float64frombits (uint64 (v )), nil
}
func (b *Buffer ) Int53 () (int64 , error ) {
return b .Long ()
}
func (b *Buffer ) Long () (int64 , error ) {
v , err := b .Uint64 ()
if err != nil {
return 0 , err
}
return int64 (v ), nil
}
func (b *Buffer ) Int128 () (Int128 , error ) {
v := Int128 {}
size := len (v )
if len (b .Buf ) < size {
return Int128 {}, io .ErrUnexpectedEOF
}
copy (v [:size ], b .Buf [:size ])
b .Buf = b .Buf [size :]
return v , nil
}
func (b *Buffer ) Int256 () (Int256 , error ) {
v := Int256 {}
size := len (v )
if len (b .Buf ) < size {
return Int256 {}, io .ErrUnexpectedEOF
}
copy (v [:size ], b .Buf [:size ])
b .Buf = b .Buf [size :]
return v , nil
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .