rstrings

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2023 License: BSD-3-Clause Imports: 8 Imported by: 1

Documentation

Overview

Package rstrings contains string and file-based operations, such as reading a file into a string

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CsvToMap added in v0.3.1

func CsvToMap(fname string) (result map[string][]string, err error)

CsvToMap takes the given filename, reads the file in CSV format, and returns a 2d map first dimension is the column name, second dimension is the row values for that column. input CSV file is expected to have a header row.

func DefaultIfEmpty added in v0.3.1

func DefaultIfEmpty(s, deflt string) string

DefaultIfEmpty returns the given string. If the given string is empty (see rstrings.IsEmpty), the given default is returned

func EnsurePrefix added in v0.4.1

func EnsurePrefix(input string, prefix string) string

EnsurePrefix prefixes the given string with the given prefix if it doesn't have it already examples: EnsurePrefix("foo", "f") = "foo" EnsurePrefix("foo", "new-") = "new-foo"

func EnsureSuffix added in v0.4.1

func EnsureSuffix(input string, suffix string) string

EnsureSuffix suffixes the given string with the given suffix if it doesn't have it already examples: EnsureSuffix("foo", "o") = "foo" EnsureSuffix("foo", "-new") = "foo-new"

func EnsureWrapped added in v0.4.1

func EnsureWrapped(input string, wrapper string) string

EnsureWrapped wraps the given string with the given prefix/suffix if it doesn't have it already examples: EnsureWrapped("foo", "'") = "'foo'" EnsureSuffix("fluff", "f") = "fluff"

func FileToString added in v0.2.0

func FileToString(fname string) (result string, err error)

FileToString reads from the given filename and returns the contents as a string

func IsEmpty

func IsEmpty(s string) bool

IsEmpty returns whether the given string has a length of 0 or is only whitespace examples: IsEmpty("") = true IsEmpty(" ") = True IsEmpty("b") = false IsEmpty(" b") = false IsEmpty("b ") = false IsEmpty(" b ") = false

func IsNumeric added in v0.6.0

func IsNumeric(input string) bool

IsNumeric returns whether the given string is numeric (only contains digits) an empty string is considered non-numeric

func LeftPad added in v0.4.0

func LeftPad(input string, length int) string

LeftPad prepend-pads the given string to the desired length with space characters. See LeftPadWith for implementation details

func LeftPadWith added in v0.4.0

func LeftPadWith(input string, char rune, length int) string

LeftPadWith prepend-pads the given string to the given length with the given rune. If the input string is already longer than the desired length, it is returned and NOT truncated

func MapToString added in v0.3.1

func MapToString(input map[string]interface{}, isPretty ...bool) string

MapToString converts the given map to a string. The variadic bool is a flag indicating whether the result should be prettified with newlines and whitespace default is false, for compatibility

func ReadStdInOrArgs added in v0.6.0

func ReadStdInOrArgs() (string, error)

ReadStdInOrArgs is a forgiving function for receiving user input for CLI applications. It attempts to read from std input (e.g. `ls | myCoolProgram`). If this fails, it takes the `os.arguments`, and joins them with a whitespace. If the resulting string is only whitespace (e.g. `echo " " | myCoolProgram`), then the original string is returned alongside an error of type OnlyWhitespace.

func RightPad added in v0.4.0

func RightPad(input string, length int) string

RightPad append-pads the given string to the desired length with space characters. See RightPadWith for implementation details

func RightPadWith added in v0.4.0

func RightPadWith(input string, char rune, length int) string

RightPadWith append-pads the given string to the given length with the given rune. If the input string is already longer than the desired length, it is returned and NOT truncated

Types

type OnlyWhitespace added in v0.6.0

type OnlyWhitespace struct {
	// contains filtered or unexported fields
}

func (OnlyWhitespace) Error added in v0.6.0

func (o OnlyWhitespace) Error() string

Jump to

Keyboard shortcuts

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