strutil

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2017 License: Apache-2.0, BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package strutil collects utils supplemental to the standard strings package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base32ExtDecode

func Base32ExtDecode(text []byte) (data []byte, err error)

Base32ExtDecode decodes base32 extended (RFC 4648) text to binary data.

func Base32ExtEncode

func Base32ExtEncode(data []byte) (text []byte)

Base32ExtEncode encodes binary data to base32 extended (RFC 4648) encoded text.

func Base64Decode

func Base64Decode(text []byte) (data []byte, err error)

Base64Decode decodes base64 text to binary data.

func Base64Encode

func Base64Encode(data []byte) (text []byte)

Base64Encode encodes binary data to base64 encoded text.

func JoinFields

func JoinFields(flds []string, sep string) string

JoinFields returns strings in flds joined by sep. Flds may contain arbitrary bytes, including the sep as they are safely escaped. JoinFields panics if sep is the backslash character or if len(sep) != 1.

func PrettyPrint

func PrettyPrint(w io.Writer, v interface{}, prefix, suffix string, hooks PrettyPrintHooks)

PrettyPrint pretty prints v to w. Zero values and unexported struct fields are omitted.

func PrettyString

func PrettyString(v interface{}, prefix, suffix string, hooks PrettyPrintHooks) string

PrettyString returns the output of PrettyPrint as a string.

func SplitFields

func SplitFields(s, sep string) (flds []string)

SplitFields splits s, which must be produced by JoinFields using the same sep, into flds. SplitFields panics if sep is the backslash character or if len(sep) != 1.

func StrPack

func StrPack(s string) string

StrPack returns a new instance of s which is tightly packed in memory. It is intended for avoiding the situation where having a live reference to a string slice over an unreferenced biger underlying string keeps the biger one in memory anyway - it can't be GCed.

Types

type Dict

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

Dict is a string <-> id bijection. Dict is *not* concurrent access safe for assigning new ids to strings not yet contained in the bijection. Id for an empty string is guaranteed to be 0, thus Id for any non empty string is guaranteed to be non zero.

func NewDict

func NewDict() (d *Dict)

NewDict returns a newly created Dict.

func (*Dict) Count

func (d *Dict) Count() int

Count returns the number of items in the dict.

func (*Dict) Id

func (d *Dict) Id(s string) (y int)

Id maps string s to its numeric identificator.

func (*Dict) S

func (d *Dict) S(id int) (s string, ok bool)

S maps an id to its string value and ok == true. Id values not contained in the bijection return "", false.

type Formatter

type Formatter interface {
	io.Writer
	Format(format string, args ...interface{}) (n int, errno error)
}

Formatter is an io.Writer extended by a fmt.Printf like function Format

func FlatFormatter

func FlatFormatter(w io.Writer) Formatter

FlatFormatter returns a newly created Formatter with the same functionality as the one returned by IndentFormatter except it allows a newline in the 'format' string argument of Format to pass through iff indent level is currently zero.

If indent level is non-zero then such new lines are changed to a space character. There is no indent string, the %i and %u format verbs are used solely to determine the indent level.

The FlatFormatter is intended for flattening of normally nested structure textual representation to a one top level structure per line form.

FlatFormatter(os.Stdout, " ").Format("abc%d%%e%i\nx\ny\n%uz\n", 3)

output in the form of a Go quoted string literal:

"abc3%%e x y z\n"

func IndentFormatter

func IndentFormatter(w io.Writer, indent string) Formatter

IndentFormatter returns a new Formatter which interprets %i and %u in the Format() format string as indent and undent commands. The commands can nest. The Formatter writes to io.Writer 'w' and inserts one 'indent' string per current indent level value. Behaviour of commands reaching negative indent levels is undefined.

IndentFormatter(os.Stdout, "\t").Format("abc%d%%e%i\nx\ny\n%uz\n", 3)

output:

abc3%e
	x
	y
z

The Go quoted string literal form of the above is:

"abc%%e\n\tx\n\tx\nz\n"

The commands can be scattered between separate invocations of Format(), i.e. the formatter keeps track of the indent level and knows if it is positioned on start of a line and should emit indentation(s). The same output as above can be produced by e.g.:

f := IndentFormatter(os.Stdout, " ")
f.Format("abc%d%%e%i\nx\n", 3)
f.Format("y\n%uz\n")

type GoDict

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

GoDict is a concurrent access safe version of Dict.

func NewGoDict

func NewGoDict() (d *GoDict)

NewGoDict returns a newly created GoDict.

func (*GoDict) Count

func (d *GoDict) Count() int

Count returns the number of items in the dict.

func (*GoDict) Id

func (d *GoDict) Id(s string) (y int)

Id maps string s to its numeric identificator. The implementation honors getting an existing id at the cost of assigning a new one.

func (*GoDict) S

func (d *GoDict) S(id int) (s string, ok bool)

S maps an id to its string value and ok == true. Id values not contained in the bijection return "", false.

type GoPool

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

GoPool is a concurrent access safe version of Pool.

func NewGoPool

func NewGoPool() (p *GoPool)

NewGoPool returns a newly created GoPool.

func (*GoPool) Align

func (p *GoPool) Align(s string) (y string)

Align returns a string with the same value as its argument. It guarantees that all aligned strings share a single instance in memory.

func (*GoPool) Count

func (p *GoPool) Count() int

Count returns the number of items in the pool.

type Pool

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

Pool handles aligning of strings having equal values to the same string instance. Intended use is to conserve some memory e.g. where a large number of identically valued strings with non identical backing arrays may exists in several semantically distinct instances of some structs. Pool is *not* concurrent access safe. It doesn't handle common prefix/suffix aligning, e.g. having s1 == "abc" and s2 == "bc", s2 is not automatically aligned as s1[1:].

func NewPool

func NewPool() *Pool

NewPool returns a newly created Pool.

func (*Pool) Align

func (p *Pool) Align(s string) string

Align returns a string with the same value as its argument. It guarantees that all aligned strings share a single instance in memory.

func (*Pool) Count

func (p *Pool) Count() int

Count returns the number of items in the pool.

type PrettyPrintHooks

type PrettyPrintHooks map[reflect.Type]func(f Formatter, v interface{}, prefix, suffix string)

PrettyPrintHooks allow to customize the result of PrettyPrint for types listed in the map value.

Jump to

Keyboard shortcuts

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