encode

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: BSD-2-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package encode implements functions for encoding recordjar fields.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Boolean

func Boolean(b bool) []byte

Boolean returns the given boolean as a []byte containing either "TRUE" or "FALSE".

func Bytes

func Bytes(data []byte) []byte

Bytes returns a copy of the passed []byte. Important so we don't accidentally pin a larger backing array in memory via the slice. Any leading or trailing white space will be trimmed EXCEPT new lines '\n', which the trimming will end at. This is the preferred way to encode a free text section as it allows for leading/trailing blank lines.

func DateTime

func DateTime(t time.Time) []byte

DateTime returns the given time.Time as a []byte. The byte slice will be formatted according to RFC1123Z and converted to the UTC timezone. For example: Thu, 20 Sep 2018 20:24:33 +0000

func DoubleInteger added in v0.0.21

func DoubleInteger(i1, i2 int) []byte

DoubleInteger returns the passed integers as a stringified []byte with the integers separated by the sign of the second integer - either a plus '+' or minus '-'. If the second integer is zero it will be omitted. For example:

DoubleInteger( 0,  0) == ""
DoubleInteger( 1,  0) == "1"
DoubleInteger(-1,  0) == "-1"
DoubleInteger(+1,  0) == "1"
DoubleInteger( 0,  2) == "0+2"
DoubleInteger( 1,  2) == "1+2"
DoubleInteger( 1, -2) == "1-2"
DoubleInteger(-1, -2) == "1-2"

func Duration

func Duration(d time.Duration) []byte

Duration returns the given time.Duration as a []byte. The byte slice will have the format "0h0m0s" and is rounded (half up) to the nearest second. Leading and trailing zero units will be omitted.

func Integer

func Integer(i int) []byte

Integer returns the passed integer value as a stringified []byte.

func KeyedStringList added in v0.0.9

func KeyedStringList(pairs map[string]string, delimiter rune) (data []byte)

KeyedStringList returns the map of names and strings as a list of colon separated keyed strings. For example:

m := map[string]string{
	"get":  "You cannot get that!",
	"look": "Your eyes hurt to look at it!",
}
data := KeyedStringList(m, '→')

Results in data containing:

GET→You cannot get that!\n: LOOK→Your eyes hurt to look at it!

func Keyword

func Keyword(s string) []byte

Keyword returns the passed string as an uppercased []byte. This is helpful for keeping IDs and references consistent and independent of how they appear in e.g. data files. Any white space will be removed, either leading, trailing or within the keyword - a keyword with white space would actually be two or more keywords.

func KeywordList

func KeywordList(s []string) []byte

KeywordList returns the []string data as a white space separated, uppercased slice of bytes. Multiple keywords will have consistent ordering. Duplicate keywords will be omitted. Any white space will be removed, either leading, trailing or within a keyword - a keyword with white space would actually be two or more keywords.

func PairList

func PairList(data map[string]string, delimiter rune) []byte

PairList returns the passed map of string name/value pairs as an uppercased []byte. Each name/value pair is separated with the given delimiter. Any white space will be removed, either leading, trailing or within a name or value - a name or value with white space would actually be more than a single pair of name/value. All of the string name/delimiter/value pairs are then concatenated together separated by white space.

exits := map[string]string{
	"E":  "L3",
	"SE": "L4",
	"S":  "L2",
}
data := PairList(exits, '→')

Results in data being a byte slice containing "E→L3 SE→L4 S→L2". Multiple name/value pairs will have consistent ordering. It should be noted that using a space for a delimiter will result in a keyword list, not a pair list, which may cause problems when the field is decoded again. If no name is given for a pair any value will be ignored.

func String

func String(s string) []byte

String returns the given string as a []byte with leading and trailing white space removed. This should only be used for fields, not the free text section - which can contain meaningful leading or trailing blank lines. For the free text section encode.Bytes is preferred.

func StringList added in v0.0.9

func StringList(data []string) []byte

StringList returns a list of strings delimited by a colon separator. Each string in the list will start with the delimiter on a new line. The original order of the list will be preserved.

Types

This section is empty.

Jump to

Keyboard shortcuts

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