decode

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: 8 Imported by: 0

Documentation

Overview

Package decode implements functions for decoding recordjar fields.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Boolean

func Boolean(data []byte) (b bool)

Boolean returns the []byte data as a boolean value. The data is parsed using strconv.ParseBool and will default to false if the data cannot be parsed. Using strconv.parseBool allows true and false to be represented in many ways. For example: 0, f, F, false, False, FALSE, 1, t, T, true, True, TRUE. As a special case data of length zero will default to true. This allows true to be represented as the presence or absence of just a keyword. For example:

Door: EXIT→E RESET→1m JITTER→1m OPEN

Here OPEN is a boolean and will default to true.

func Bytes

func Bytes(data []byte) []byte

Bytes returns a copy of the []byte data. Important so we don't accidentally pin a larger backing array in memory via the source 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 decode a free text section as it allows for leading/trailing blank lines.

func DateTime added in v0.0.9

func DateTime(data []byte) (t time.Time)

DateTime returns the []byte data as a time.Time. The data is parsed using time.Parse and is expected to conform to RFC1123Z - as written by encode.DateTime. For example: Thu, 20 Sep 2018 20:24:33 +0000

If there is an error parsing the data the date and time will default to the current date and time. The returned date/time will use the UTC timezone.

func DoubleInteger added in v0.0.21

func DoubleInteger(data []byte) (i1, i2 int)

DoubleInteger return the []byte data as two integer values. The []byte should contain the two integers separated by either a plus '+' or minus '-' sign. The leading integer may have a plus '+' or minus '-' sign. If both integers or the second integer is not specified zero will be assumed. For example:

      =  0,  0
 1    =  1,  0
-1    = -1,  0
+1    =  1,  0
 1+2  =  1,  2
+1+2  =  1,  2
 1-2  =  1, -2
-1+2  = -1,  2
-1-2  = -1, -2

func Duration

func Duration(data []byte) (t time.Duration)

Duration returns the []byte data as a time.Duration rounded (half up) to the nearest second. The data is parsed using time.ParseDuration and will default to 0 if the data cannot be parsed.

func Integer

func Integer(data []byte) (i int)

Integer returns the []byte data as an integer value. The []byte is parsed using strconv.Atoi and will default to 0 if the data cannot be parsed. The valid range is at least -2147483648 to 2147483647.

func IsSeparator added in v0.0.16

func IsSeparator(r rune) bool

IsSeparator returns true if r is considered a name/value separator else false. A rune is considered a separator if it is not a letter, digit, white space, hyphen/minus '-' or underscore '_'.

func KeyedStringList

func KeyedStringList(data []byte) (list map[string]string)

KeyedStringList splits the []byte data into a map of keywords and strings. The []byte data is first split on a colon (:) separator to determine the pairs. The keyword is then split from the beginning of each pair on the first non-letter, non-digit, non-hyphen/minus '-' or non-underscore '_'. That means the keyword can contain letters, digits, hyphens/minuses or underscores.

For example:

Vetoes:  GET→You can't get it.
      : DROP→You can't drop it.

Would produce a map with two entries:

map[string]string{
  "GET": "You can't get it.",
  "DROP": "You can't drop it.",
}

If a keyword is specified more than once only the first instance will be used. Leading and trailing whitespace will be removed from the returned strings.

func Keyword

func Keyword(data []byte) string

Keyword returns the []bytes data as an uppercased string. 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(data []byte) []string

KeywordList returns the []byte data as an uppercased slice of strings. The data is split on whitespace, extra whitespace is stripped and the individual 'words' are returned in the string slice. Duplicate keywords will be removed.

func PairList

func PairList(data []byte) (pairs map[string]string)

PairList returns the []byte data as uppercassed pairs of strings in a map. The data is first split on white space and extra white space is stripped. The pairs are then split into a keyword and value on the first non-letter, non-digit, non-underscore '_' or non-hyphen/minus '-'. That means the keyword can contain letters, digits, underscores or hyphens/minuses.

In addition a keyword may have a leading exclamation mark '!' that is not treated as a delimiter. A keyword with a leading exclamation mark is distinct from a keyword without. For example 'KEYWORD' and '!KEYWORD' are distinct keywords.

If we take exits as an example:

Exits: E→L3 SE→L4 S→ W

Results in a map with four pairs:

map[string]string {
  "E": "L3",
  "SE": "L4",
  "S": "",
  "W": "",
}

Here the separator used is '→' but any non-letter, non-digit, non-underscore or non-hyphen/minus may be used. If the same keyword occurs more than once only the first instance will be used. A keyword may appear by itself, as in 'E', or with a separator, as in 'E→' in which case the value will be an empty string. If no keyword is given, for example '→L3' any value will be ignored. The keywords in the returned map will always be uppercased.

func String

func String(data []byte) string

String returns the []bytes data as a string with leading and trailing white space removed. This should only be used to decode fields and not the free text section. The decoder.Bytes function is preferred for the free text section as the section can contain meaningful leading and/or trailing blank lines for formatting.

func StringList

func StringList(data []byte) (s []string)

StringList returns the []byte data as a []string by splitting the data on a colon separator. Any leading or trailing white space will be removed from the returned strings. 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