Documentation ¶
Overview ¶
Package encode implements functions for encoding recordjar fields.
Index ¶
- func Boolean(b bool) []byte
- func Bytes(data []byte) []byte
- func DateTime(t time.Time) []byte
- func DoubleInteger(i1, i2 int) []byte
- func Duration(d time.Duration) []byte
- func Integer(i int) []byte
- func KeyedStringList(pairs map[string]string, delimiter rune) (data []byte)
- func Keyword(s string) []byte
- func KeywordList(s []string) []byte
- func PairList(data map[string]string, delimiter rune) []byte
- func String(s string) []byte
- func StringList(data []string) []byte
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Boolean ¶
Boolean returns the given boolean as a []byte containing either "TRUE" or "FALSE".
func Bytes ¶
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 ¶
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
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 ¶
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 ¶
Integer returns the passed integer value as a stringified []byte.
func KeyedStringList ¶ added in v0.0.9
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 ¶
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 ¶
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 ¶
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 ¶
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.
Types ¶
This section is empty.