Go to file
ever dbbe46162a Initial commit 2024-01-27 12:49:30 +01:00
.markdownlint.json Initial commit 2024-01-27 12:49:30 +01:00
LICENSE Initial commit 2024-01-27 12:49:30 +01:00
README.md Initial commit 2024-01-27 12:49:30 +01:00
go.mod Initial commit 2024-01-27 12:49:30 +01:00
simplebuf.go Initial commit 2024-01-27 12:49:30 +01:00

README.md

simplebuf

simplebuf provides simple and versatile buffers for Go.

There are two types of buffers:

  • Buffer: An easy way to read from an array and manipulate it.
  • WriteBuffer: Enables you to efficiently build an array out of several arrays, which are pushed onto a stack.

Use Cases

Binary Formats

Networking

package main

import (
	"fmt"

	"git.brokenmouse.studio/bms/go-simplebuf"
)

func readString(buf *simplebuf.Buffer[byte]) (str string) {
	strLen := int(buf.Elem())
	str = string(buf.Remaining()[:strLen])
	buf.Pos += strLen
	return
}

func pushString(buf *simplebuf.WriteBuffer[byte], str []byte) {
	buf.Push([]byte{uint8(len(str))})
	buf.Push(str)
}

func main() {
	packetWriteBuf := simplebuf.NewWriteBuffer[byte]()
	pushString(packetWriteBuf, []byte("doe32"))
	packetWriteBuf.Push([]byte{uint8(1), uint8(150)})

	packetBuf := simplebuf.NewBuffer(packetWriteBuf.Read())

	username := readString(packetBuf)

	var status string
	if uint8(packetBuf.Elem()) == 1 {
		status = "Online"
	} else {
		status = "Offline"
	}

	score := uint8(packetBuf.Elem())

	fmt.Printf("Player \"%s\":\n  - Status: %s\n  - Score: %d\n",
		username,
		status,
		score)
}