util

package
v4.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2022 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package util provides miscellaneous utility functions that don't deserve their own package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hexdump

func Hexdump(data []byte, opts ...func(*hdopt)) string

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 VersionCompare

func VersionCompare(a, b string) (int, error)

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.

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 added in v4.3.12

type SimpleConfigurationData map[string]string

func NewSimpleConfigurationData added in v4.3.12

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 added in v4.3.12

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 added in v4.3.12

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 added in v4.3.12

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) GetDefault added in v4.3.12

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 added in v4.3.12

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 added in v4.3.12

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) Set added in v4.3.12

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 added in v4.3.12

func (c SimpleConfigurationData) SetInt(key string, value int)

Jump to

Keyboard shortcuts

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