Files
tma-back/models/writer.go
2026-01-14 23:10:23 +03:00

50 lines
830 B
Go

package models
import (
"encoding/binary"
"math"
)
type Writer struct {
buf []byte
}
func (w *Writer) Bytes() []byte {
return w.buf
}
func (w *Writer) WriteU8(v uint8) {
w.buf = append(w.buf, v)
}
func (w *Writer) WriteU16(v uint16) {
tmp := make([]byte, 2)
binary.LittleEndian.PutUint16(tmp, v)
w.buf = append(w.buf, tmp...)
}
func (w *Writer) WriteU32(v uint32) {
tmp := make([]byte, 4)
binary.LittleEndian.PutUint32(tmp, v)
w.buf = append(w.buf, tmp...)
}
func (w *Writer) WriteF32(v float32) {
tmp := make([]byte, 4)
binary.LittleEndian.PutUint32(tmp, math.Float32bits(v))
w.buf = append(w.buf, tmp...)
}
func (w *Writer) WriteBool(v bool) {
if v {
w.WriteU8(1)
} else {
w.WriteU8(0)
}
}
func (w *Writer) WriteString(s string) {
w.WriteU16(uint16(len(s)))
w.buf = append(w.buf, []byte(s)...)
}