stringutil

package
v0.0.0-...-ba7cdbd Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2014 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package stringutil contains small utility functions for string manipulation and parsing.

Index

Constants

View Source
const LineSeparator = "\n"

LineSeparator is the standard line separator for GOOS.

View Source
const (
	// Unicode non-character. Used to signal that there are no
	// quoting characters.
	NO_QUOTES = "\uffff"
)

Variables

This section is empty.

Functions

func CamelCaseToLower

func CamelCaseToLower(s string, sep string) string

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

func CamelCaseToWords(s string, sep string) string

CamelCaseToWords transforms a camel-cased string into a string, separating the words with sep.

func FileLines

func FileLines(filename string, begin int, count int, nonEmpty bool) (string, error)

FileLines works like lines, but it reads the initial text from the given filename.

func Lines

func Lines(text string, begin int, count int, nonEmpty bool) string

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

func MustReadLines(path string) []string

MustReadLines works like ReadLines, but panics if there's an error

func ParseIni

func ParseIni(r io.Reader) (map[string]string, error)

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

func ParseIniOptions(r io.Reader, opts *IniOptions) (map[string]string, error)

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

func Random(length int) string

Random returns a random string with the given length. The alphabet used includes ASCII lowercase and uppercase letters and numbers.

func RandomBytes

func RandomBytes(n int) []byte

RandomBytes returns a slice of n random bytes.

func RandomPrintable

func RandomPrintable(length int) string

RandomPrintable returns a random string with the given length, using all the ASCII printable characters as the alphabet.

func ReadLines

func ReadLines(path string) ([]string, error)

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

func ReadTextFile(path string) (string, error)

ReadTextFile returns the contents of the file at the given path as a string.

func Reverse

func Reverse(s string) string

Reverse reverses the given string.

func Slug

func Slug(s string) string

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

func SlugN(s string, n int) string

SlugN works like Slug, but returns at string with, at most n characters. If n is <= 0, it works exactly like Slug.

func SplitFields

func SplitFields(text string, sep string) ([]string, error)

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

func SplitLines(text string) []string

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

func UnCamelCase(s string) []string

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

type StringFunc func(string) string

StringFunc is a function which accepts an string and returns another string.

Jump to

Keyboard shortcuts

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