Documentation ¶
Overview ¶
Package stringutil contains small utility functions for string manipulation and parsing.
Index ¶
- Constants
- func CamelCaseToLower(s string, sep string) string
- func CamelCaseToString(s string, sep string, f StringFunc) string
- func CamelCaseToWords(s string, sep string) string
- func FileLines(filename string, begin int, count int, nonEmpty bool) (string, error)
- func Lines(text string, begin int, count int, nonEmpty bool) string
- func MustReadLines(path string) []string
- func ParseIni(r io.Reader) (map[string]string, error)
- func ParseIniOptions(r io.Reader, opts *IniOptions) (map[string]string, error)
- func Random(length int) string
- func RandomBytes(n int) []byte
- func RandomPrintable(length int) string
- func ReadLines(path string) ([]string, error)
- func ReadTextFile(path string) (string, error)
- func Reverse(s string) string
- func Slug(s string) string
- func SlugN(s string, n int) string
- func SplitCommonPrefix(values []string) (string, []string)
- func SplitFields(text string, sep string) ([]string, error)
- func SplitFieldsOptions(text string, sep string, opts *SplitOptions) ([]string, error)
- func SplitLines(text string) []string
- func UnCamelCase(s string) []string
- type IniOptions
- type SplitError
- type SplitOptions
- type StringFunc
Constants ¶
const LineSeparator = "\n"
LineSeparator is the standard line separator for GOOS.
const ( // Unicode non-character. Used to signal that there are no // quoting characters. NO_QUOTES = "\uffff" )
Variables ¶
This section is empty.
Functions ¶
func CamelCaseToLower ¶
CamelCaseToLower transform a camel-cased string into a lowercase string, separating the words with sep. e.g. FooBar with '_' as sep becomes foo_bar.
func CamelCaseToString ¶
func CamelCaseToString(s string, sep string, f StringFunc) string
CamelCaseToString transform a camel-cased string into a string containing the words in the original string, separated by sep. Optionally, a f argument might be provided, which will be used to transform the original strings before concatenating them into the final string.
func CamelCaseToWords ¶
CamelCaseToWords transforms a camel-cased string into a string, separating the words with sep.
func Lines ¶
Lines returns the lines in text between begin and begin+count, including both. Invalid line numbers (< 0 or > number of lines) are just ignored, so the number of returned lines might be different than count. If nonEmpty is true, empty lines will be removed from the output before selecting the specified lines. The string returned will have its lines separated by just the '\n' character. Any '\r' characters in the provided text will be removed.
func MustReadLines ¶
MustReadLines works like ReadLines, but panics if there's an error
func ParseIni ¶
ParseIni parses a .ini style file in the form:
key1 = value key2 = value
Values that contain newlines ("\n" or "\r\n") need to be escaped by ending the previous line with a '\' character. Lines starting with ';' or '#' are considered comments and ignored. Empty lines are ignored too. If a non-empty, non-comment line does not contain a '=' an error is returned.
func ParseIniOptions ¶
ParseIniOptions works like ParseIni, but allows the caller to specify the strings which represent separators and comments. If opts is nil, this function acts like ParseIni. If Separator is empty, it defaults to '='. If Comment is empty, no lines are considered comments.
func Random ¶
Random returns a random string with the given length. The alphabet used includes ASCII lowercase and uppercase letters and numbers.
func RandomPrintable ¶
RandomPrintable returns a random string with the given length, using all the ASCII printable characters as the alphabet.
func ReadLines ¶
ReadLines returns the non-empty lines from the file at the given path. The OS specific line separator is used to split the text into lines.
func ReadTextFile ¶
ReadTextFile returns the contents of the file at the given path as a string.
func Slug ¶
Slug returns a slugified version of the given string, which consists in transliterating unicode characters to ascii (e.g. ó becomes o and â becomes a), replacing all sequences of whitespaces with '-' and converting to lowercase. Very useful for making arbitrary strings, like a post title, part of URLs.
func SlugN ¶
SlugN works like Slug, but returns at string with, at most n characters. If n is <= 0, it works exactly like Slug.
func SplitCommonPrefix ¶
SplitCommonPrefix returns the common prefix in values and slice with each string in values with the common prefix removed.
func SplitFields ¶
SplitFields separates the given text into multiple fields, using any character in sep as separator between fields. Additionally, fields using a separator character in their values might be quoted using ' or " (this can be changed with SplitFieldsOptions). Any separator or quoting character might also be escaped by prepending a \ to it. Also, whitespaces between fields are ignored (if you want a field starting or ending with spaces, quote it).
func SplitFieldsOptions ¶
func SplitFieldsOptions(text string, sep string, opts *SplitOptions) ([]string, error)
SplitFieldsOptions works like SplitFields, but accepts an additional options parameter. See the type SplitOptions for the available options.
func SplitLines ¶
SplitLines splits the given text into lines. Lines might be terminated by either "\r\n" (as in Windows) or just "\n" (as in Unix). Newlines might be escaped by prepending them with the '\' character.
func UnCamelCase ¶
UnCamelCase separates the a camel-cased string into lowercase using sep as the separator between words. Multiple uppercase characters together are treated as a single word (e.g. TESTFoo is interpreted as the words 'TEST' and 'Foo', while FooBAR is intepreted as 'Foo' and 'BAR').
Types ¶
type IniOptions ¶
type IniOptions struct { // Separator indicates the characters used as key-value separator. // If empty, "=" is used. Separator string // Comment indicates the characters used to check if a line is a comment. // Lines starting with any character in this string are ignored. // If empty, all lines are parsed. Comment string }
IniOptions specify the options for ParseIniOptions.
type SplitError ¶
type SplitError struct { // Pos indicates the position in the input while the error originated, // zero indexed. Pos int // Err is the original error. Err error }
SplitError represents an error while splitting the fields. Note that not all errors returned for SplitFields are *SplitError (e.g. if the number of fields does not match ExactCount, the error is NOT an *SplitError).
func (*SplitError) Error ¶
func (s *SplitError) Error() string
type SplitOptions ¶
type SplitOptions struct { // Quotes includes the characters that are admitted as quote characters. If empty, // the default quoting characters ' and " are used. If you want // no quoting characters set this string to NO_QUOTES. Quotes string // ExactCount specifies the exact number of fields that // the text must have after splitting them. If the // number does not match, an error is returned. // Values <= 0 are ignored. ExactCount int // MaxSplits indicates the maximum number of splits performed. Id est, // MaxSplits = 1 will yield at most 2 fields. Values <= 0 are ignored. MaxSplits int // KeepQuotes indicates wheter to keep the quotes in the quoted fields. // Otherwise, quotes are removed from the fields. KeepQuotes bool }
SplitOptions represent options which can be specified when calling SplitFieldsOptions.
type StringFunc ¶
StringFunc is a function which accepts an string and returns another string.