You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.3 KiB
128 lines
3.3 KiB
// ------------------------------------------------------------------------
|
|
// Project atila
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name buffer_binary.go
|
|
// Created by DEV
|
|
// Modified 16/01/2024
|
|
//
|
|
// Copyright 2024 activething.com
|
|
// ------------------------------------------------------------------------
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
// ------------------------------------------------------------------------
|
|
|
|
package bfers
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math"
|
|
"time"
|
|
)
|
|
|
|
|
|
|
|
func (b *Buffer) WriteBool(value bool) {
|
|
if value {
|
|
b.WriteByte(1)
|
|
return
|
|
}
|
|
b.WriteByte(0)
|
|
}
|
|
|
|
|
|
func (b *Buffer) WriteRune(value rune, byteOrder binary.ByteOrder) {
|
|
b.WriteUint32(uint32(value), byteOrder) }
|
|
|
|
|
|
func (b *Buffer) WriteUint16(value uint16, byteOrder binary.ByteOrder) {
|
|
b.lastOp = opInvalid
|
|
if byteOrder == nil {
|
|
byteOrder = binary.LittleEndian
|
|
}
|
|
byteOrder.PutUint16(b.data[b.tryGrow(2):], value)
|
|
}
|
|
|
|
|
|
func (b *Buffer) WriteUint32(value uint32, byteOrder binary.ByteOrder) {
|
|
b.lastOp = opInvalid
|
|
if byteOrder == nil {
|
|
byteOrder = binary.LittleEndian
|
|
}
|
|
byteOrder.PutUint32(b.data[b.tryGrow(4):], value)
|
|
}
|
|
|
|
|
|
func (b *Buffer) WriteUint64(value uint64, byteOrder binary.ByteOrder) {
|
|
if byteOrder == nil {
|
|
byteOrder = binary.LittleEndian
|
|
}
|
|
byteOrder.PutUint64(b.data[b.tryGrow(8):], value)
|
|
}
|
|
|
|
|
|
func (b *Buffer) WriteInt16(value int16, byteOrder binary.ByteOrder) {
|
|
b.WriteUint16(uint16(value), byteOrder) }
|
|
|
|
|
|
func (b *Buffer) WriteInt32(value int32, byteOrder binary.ByteOrder) {
|
|
b.WriteUint32(uint32(value), byteOrder) }
|
|
|
|
|
|
func (b *Buffer) WriteInt64(value int64, byteOrder binary.ByteOrder) {
|
|
b.WriteUint64(uint64(value), byteOrder) }
|
|
|
|
|
|
func (b *Buffer) WriteFloat32(value float32, byteOrder binary.ByteOrder) {
|
|
b.WriteUint32(math.Float32bits(value), byteOrder) }
|
|
|
|
|
|
func (b *Buffer) WriteFloat64(value float64, byteOrder binary.ByteOrder) {
|
|
b.WriteUint64(math.Float64bits(value), byteOrder) }
|
|
|
|
|
|
func (b *Buffer) WriteComplex64(value complex64, byteOrder binary.ByteOrder) {
|
|
b.lastOp = opInvalid
|
|
if byteOrder == nil {
|
|
byteOrder = binary.LittleEndian
|
|
}
|
|
l := b.tryGrow(8)
|
|
byteOrder.PutUint32(b.data[l:], math.Float32bits(real(value)))
|
|
byteOrder.PutUint32(b.data[l+4:], math.Float32bits(imag(value)))
|
|
}
|
|
|
|
|
|
func (b *Buffer) WriteComplex128(value complex128, byteOrder binary.ByteOrder) {
|
|
b.lastOp = opInvalid
|
|
if byteOrder == nil {
|
|
byteOrder = binary.LittleEndian
|
|
}
|
|
l := b.tryGrow(8)
|
|
byteOrder.PutUint64(b.data[l:], math.Float64bits(real(value)))
|
|
byteOrder.PutUint64(b.data[l+4:], math.Float64bits(imag(value)))
|
|
}
|
|
|
|
|
|
func (b *Buffer) WriteTime(value time.Time) error {
|
|
b.lastOp = opInvalid
|
|
d, e := value.MarshalBinary()
|
|
if e != nil {
|
|
return e
|
|
}
|
|
copy(b.data[b.tryGrow(len(d)):], d)
|
|
return nil
|
|
}
|
|
|
|
|
|
func (b *Buffer) WriteDuration(val time.Duration) {
|
|
b.WriteInt64(int64(val), binary.LittleEndian) }
|