Documentation ¶
Overview ¶
Package util provides miscellaneous utility functions that don't deserve their own package.
Index ¶
- func FancyFileName(path string, extras map[byte]string) (string, error)
- func Hexdump(data []byte, opts ...func(*hdopt)) string
- func OverrideBool(key string, value bool) func(SimpleConfigurationData) error
- func OverrideBoolWithNegation(key string, value bool, neg bool) func(SimpleConfigurationData) error
- func OverrideInt(key string, value int) func(SimpleConfigurationData) error
- func OverrideString(key string, value string) func(SimpleConfigurationData) error
- func PluralizeCustom(base, singularSuffix, pluralSuffix string, qty int) string
- func PluralizeString(base string, qty int) string
- func UpdateSimpleConfig(inputFile io.Reader, data SimpleConfigurationData) error
- func VersionCompare(a, b string) (int, error)
- func WithStartingAddress(a int) func(*hdopt)
- func WithWidth(w int) func(*hdopt)
- func WithWordSize(w int) func(*hdopt)
- func WithoutText(o *hdopt)
- type SimpleConfigurationData
- func (c SimpleConfigurationData) Get(key string) (string, bool)
- func (c SimpleConfigurationData) GetBool(key string) (bool, error)
- func (c SimpleConfigurationData) GetBoolDefault(key string, def bool) bool
- func (c SimpleConfigurationData) GetDefault(key, def string) (string, bool)
- func (c SimpleConfigurationData) GetInt(key string) (int, error)
- func (c SimpleConfigurationData) GetIntDefault(key string, def int) (int, error)
- func (c SimpleConfigurationData) Override(opts ...func(SimpleConfigurationData) error) error
- func (c SimpleConfigurationData) Set(key, value string)
- func (c SimpleConfigurationData) SetInt(key string, value int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FancyFileName ¶
FancyFileName expands tokens found in the path string to allow the user to specify dynamically-named files at runtime. If there's a problem with the formatting, an error is returned along with the original path.
The tokens which may appear in the path include the following (note that all of these are modified as appropriate to the locale's national conventions and language):
%A full weekday name %a abbreviated weekday name %B full month name %b abbreviated month name %C zero-padded two-digit year 00-99 %c time and date %d day of month as number 01-31 (zero padded) %e day of month as number 1-31 (space padded) %F == %Y-%m-%d %H hour as number 00-23 (zero padded) %h abbreviated month name (same as %b) %I hour as number 01-12 (zero padded) %j day of year as number 001-366 %k hour as number 0-23 (space padded) %L milliseconds as number 000-999 %l hour as number 1-12 (space padded) %M minute as number 00-59 %m month as number 01-12 %P process ID %p AM or PM %R == %H:%M %r == %I:%M:%S %p %S second as number 00-60 %s Unix timestamp as a number %T == %H:%M:%S %U week of the year as number 00-53 (Sunday as first day of week) %u weekday as number (1=Monday .. 7=Sunday) %V week of the year as number 00-53 (Monday as first day of week) %v == %e-%b-%Y %W week of the year as number 00-53 (Monday as first day of week) %w weekday as number (0=Sunday .. 6=Saturday) %X time %x date %Y full year %y two-digit year (00-99) %Z time zone name %z time zone offset from UTC %µ microseconds as number 000-999 %% literal % character
The extras parameter maps token names to static string values, so more tokens specific to the task at hand can be added, such as these that the mapper client adds:
%G "GM" if logged in as the GM, otherwise "" %N username %n module name
func Hexdump ¶
Hexdump takes an array of bytes and returns a multi-line string representing those bytes in a traditional hexdump format with an address field on the left, starting at address 0, showing 16 bytes per line, and a text bar along the right showing any printable ASCII characters found in the hexdump.
For example, calling
Hexdump([]byte("\x00\x81\x02\x03Hello, World™<>ABCDEFG"))
will return the string
00000000: 00 81 02 03 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 |....Hello, World| 00000010: E2 84 A2 3C 3E 41 42 43 44 45 46 47 |...<>ABCDEFG |
Options may be added after the data slice to control how the hex dump will be formatted: WithStartingAddress(addr), WithWidth(nbytes), WithWordSize(nbytes), and/or WithoutText.
func OverrideBool ¶
func OverrideBool(key string, value bool) func(SimpleConfigurationData) error
if the supplied value is true, update the config data to be true.
func OverrideBoolWithNegation ¶
func OverrideBoolWithNegation(key string, value bool, neg bool) func(SimpleConfigurationData) error
if the supplied value is true, update the config data to be true. if the supplied negation value is true, update the config data to be false.
func OverrideInt ¶
func OverrideInt(key string, value int) func(SimpleConfigurationData) error
if the supplied value is nonzero, update the config data to match.
func OverrideString ¶
func OverrideString(key string, value string) func(SimpleConfigurationData) error
if the supplied value is nonempty, update the config data to match.
func PluralizeCustom ¶
PluralizeCustom emits a properly-pluralized version of a string, where that is more complicated than just adding an "s" to the end.
func PluralizeString ¶
PluralizeString emits a properly-pluralized version of a string, by adding an "s" for quantities other than one.
func UpdateSimpleConfig ¶
func UpdateSimpleConfig(inputFile io.Reader, data SimpleConfigurationData) error
UpdateSimpleConfig reads a configuration file as described for ParseSimpleConfig, but instead of creating a new set of config data, it updates an existing data set.
func VersionCompare ¶
VersionCompare compares version strings a and b. These strings must consist of integers separated with dots, such as "2" or "3.1". Any number of version levels are allowed, although generally only 2 or 3 are of practical use.
Returns <0 if a is a version before b, >0 if a is after b, or zero if they are the same.
As of version 5.0.0, this is simply a wrapper to the hashicorp go-version package.
func WithStartingAddress ¶
func WithStartingAddress(a int) func(*hdopt)
WithStartingAddress may be added as an option to the Hexdump function to change the starting address of the data being shown.
Example:
data := []byte("\x00\x81\x02\x03Hello, World™<>ABCDEFG") Hexdump(data, WithStartingAddress(0x4444))
will return the string
00004444: 00 81 02 03 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 |....Hello, World| 00004454: E2 84 A2 3C 3E 41 42 43 44 45 46 47 |...<>ABCDEFG |
func WithWidth ¶
func WithWidth(w int) func(*hdopt)
WithWidth may be added as an option to the Hexdump function to change the output width in bytes.
The behavior is undefined if the width is not a multiple of the word size.
Example:
data := []byte("\x00\x81\x02\x03Hello, World™<>ABCDEFG") Hexdump(data, WithWidth(8), WithStartingAddress(0x4444))
will return the string
00004444: 00 81 02 03 48 65 6C 6C |....Hell| 0000444C: 6F 2C 20 57 6F 72 6C 64 |o, World| 00004454: E2 84 A2 3C 3E 41 42 43 |...<>ABC| 0000445C: 44 45 46 47 |DEFG |
func WithWordSize ¶
func WithWordSize(w int) func(*hdopt)
WithWordSize may be added as an option to the Hexdump function to change the output word size in bytes.
Example:
data := []byte("\x00\x81\x02\x03Hello, World™<>ABCDEFG") Hexdump(data, WithWordSize(2))
will return the string
00000000: 0081 0203 4865 6C6C 6F2C 2057 6F72 6C64 |....Hello, World| 00000010: E284 A23C 3E41 4243 4445 4647 |...<>ABCDEFG |
func WithoutText ¶
func WithoutText(o *hdopt)
WithoutText may be added as an option to the Hexdump function to suppress the text column from the generated display.
Example:
data := []byte("\x00\x81\x02\x03Hello, World™<>ABCDEFG") Hexdump(data, WithWordSize(2), WithoutText)
will return the string
00000000: 0081 0203 4865 6C6C 6F2C 2057 6F72 6C64 00000010: E284 A23C 3E41 4243 4445 4647
Types ¶
type SimpleConfigurationData ¶
func NewSimpleConfigurationData ¶
func NewSimpleConfigurationData() SimpleConfigurationData
NewSimpleConfigurationData creates a ready-to-use SampleConfigurationData value which you can call Set, et al. directly without having read in a configuration from a file first.
func ParseSimpleConfig ¶
func ParseSimpleConfig(inputFile io.Reader) (SimpleConfigurationData, error)
ParseSimpleConfig parses a minimal configuration file format used by the mapper that isn't a full INI file. Rather, it's a simple "key=value" collection with one entry per line in the file. The key must be alphanumeric (including underscores and hyphens), while the value may include any characters. Spaces before or after the key are ignored, as are spaces before or after the value.
A key alone on a line (without an = sign) indicates a boolean true value for that key.
Lines starting with a # sign (allowing for leading spaces before that) are ignored as comments.
func (SimpleConfigurationData) Get ¶
func (c SimpleConfigurationData) Get(key string) (string, bool)
Get retrieves a string value from the configuration data. Returns the string value, or "" if the key does not exist, and a boolean indicating whether the value existed in the data.
func (SimpleConfigurationData) GetBool ¶
func (c SimpleConfigurationData) GetBool(key string) (bool, error)
GetBool retrieves a boolean value from the configuration data. Returns an error if the value does not exist or could not be converted to a boolean.
This considers values "0", "false", "no", or "off" to be false, and non-zero integers, "true", "yes", or "on" to be true. Non-existent keys are considered to be false.
func (SimpleConfigurationData) GetBoolDefault ¶
func (c SimpleConfigurationData) GetBoolDefault(key string, def bool) bool
func (SimpleConfigurationData) GetDefault ¶
func (c SimpleConfigurationData) GetDefault(key, def string) (string, bool)
GetDefault retrieves a string value from the configuration data, or the supplied default value if no such key exists.
func (SimpleConfigurationData) GetInt ¶
func (c SimpleConfigurationData) GetInt(key string) (int, error)
GetInt retrieves an integer value from the configuration data. Returns an error if the value does not exist or could not be converted to an integer.
func (SimpleConfigurationData) GetIntDefault ¶
func (c SimpleConfigurationData) GetIntDefault(key string, def int) (int, error)
GetIntDefault retrieves an integer value from the configuration data. Returns an error if the value could not be converted to an integer, or the given default value if the key could not be found.
func (SimpleConfigurationData) Override ¶
func (c SimpleConfigurationData) Override(opts ...func(SimpleConfigurationData) error) error
func (SimpleConfigurationData) Set ¶
func (c SimpleConfigurationData) Set(key, value string)
Set adds a key/value pair to the SimpleConfigurationData receiver. If key already exists, it will be replaced with this new value.
func (SimpleConfigurationData) SetInt ¶
func (c SimpleConfigurationData) SetInt(key string, value int)