byter

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2024 License: LGPL-3.0 Imports: 9 Imported by: 1

Documentation

Overview

Package byter provides functions that manipulate or return byte arrays.

Index

Examples

Constants

View Source
const (
	SUB       = 26   // SUB is the ASCII control code for substitute.
	DosSUB    = 8594 // DosSub is the Unicode for the right-arrow.
	SymbolSUB = 9242 // SymbolSUB is the Unicode for the substitute character.
)

Variables

View Source
var (
	ErrCharmap = errors.New("the c charmap cannot be nil")
	ErrUTF8    = errors.New("string cannot encode to utf-8")
)

Functions

func BOM

func BOM() []byte

BOM is the UTF-8 byte order mark prefix.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/byter"
)

func main() {
	fmt.Printf("#%x", byter.BOM())
}
Output:

#efbbbf

func Decode

func Decode(c *charmap.Charmap, s string) ([]byte, error)

Decode a string using the character map.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/byter"
	"golang.org/x/text/encoding/charmap"
)

func main() {
	s := "\xCD\xB9\xB2\xCC\xCD"
	p, _ := byter.Decode(charmap.CodePage437, s)
	fmt.Printf("%s\n", p)
	p, _ = byter.Decode(charmap.ISO8859_1, s)
	fmt.Printf("%s\n", p)
}
Output:

═╣▓╠═
͹²ÌÍ

func Encode

func Encode(c *charmap.Charmap, s string) ([]byte, error)

Encode the string using the character map.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/byter"
	"golang.org/x/text/encoding/charmap"
)

func main() {
	p, _ := byter.Encode(charmap.CodePage437, "═╣▓╠═")
	fmt.Printf("%X\n", p)
	p, _ = byter.Encode(charmap.ISO8859_1, "hello")
	fmt.Printf("%s (%X)\n", p, p)
	p, _ = byter.Encode(charmap.CodePage1140, "hello")
	fmt.Printf("%X\n", p) // not ascii compatible
}
Output:

CDB9B2CCCD
hello (68656C6C6F)
8885939396

func HexDecode

func HexDecode(s string) ([]byte, error)

HexDecode decodes a hexadecimal string into bytes.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/byter"
)

func main() {
	b, _ := byter.HexDecode("6F6B")
	fmt.Printf("%s\n", b)
	fmt.Printf("%d\n", b)
}
Output:

ok
[111 107]

func HexEncode

func HexEncode(s string) []byte

HexEncode encodes a string into hexadecimal bytes.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/byter"
)

func main() {
	b := byter.HexEncode("ok")
	fmt.Printf("#%s\n", b)
}
Output:

#6f6b

func MakeBytes

func MakeBytes() []byte

MakeBytes generates a 256 character or 8-bit container ready to hold legacy code point values.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/byter"
)

func main() {
	fmt.Printf("%d", len(byter.MakeBytes()))
}
Output:

256

func Mark

func Mark(b []byte) []byte

Mark adds a UTF-8 byte order mark to the text if it doesn't already exist.

func TrimEOF

func TrimEOF(b []byte) []byte

TrimEOF will cut text at the first occurrence of the SUB character. The SUB is used by DOS and CP/M as an end-of-file marker.

Example
package main

import (
	"fmt"

	"github.com/bengarrett/retrotxtgo/byter"
)

func main() {
	fmt.Printf("%q", byter.TrimEOF([]byte("hello world\x1a")))
}
Output:

"hello world"

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL