strutil

package
v0.0.0-...-df725e9 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: GPL-3.0 Imports: 12 Imported by: 655

Documentation

Overview

auto-generated, DO NOT EDIT!

Index

Constants

This section is empty.

Variables

View Source
var Ctrl = &unicode.RangeTable{
	R16: []unicode.Range16{
		{Lo: 0x00ad, Hi: 0x0600, Stride: 1363},
		{Lo: 0x0601, Hi: 0x0605, Stride: 1},
		{Lo: 0x061c, Hi: 0x06dd, Stride: 193},
		{Lo: 0x070f, Hi: 0x08e2, Stride: 467},
		{Lo: 0x180e, Hi: 0x200b, Stride: 2045},
		{Lo: 0x200c, Hi: 0x200f, Stride: 1},
		{Lo: 0x202a, Hi: 0x202e, Stride: 1},
		{Lo: 0x2060, Hi: 0x2064, Stride: 1},
		{Lo: 0x2066, Hi: 0x206f, Stride: 1},
		{Lo: 0xd800, Hi: 0xf8ff, Stride: 1},
		{Lo: 0xfdd0, Hi: 0xfdef, Stride: 1},
		{Lo: 0xfeff, Hi: 0xfff9, Stride: 250},
		{Lo: 0xfffa, Hi: 0xfffb, Stride: 1},
		{Lo: 0xfffe, Hi: 0xffff, Stride: 1},
	},
	R32: []unicode.Range32{
		{Lo: 0x110bd, Hi: 0x1bca0, Stride: 44003},
		{Lo: 0x1bca1, Hi: 0x1bca3, Stride: 1},
		{Lo: 0x1d173, Hi: 0x1d17a, Stride: 1},
		{Lo: 0x1fffe, Hi: 0x1ffff, Stride: 1},
		{Lo: 0x2fffe, Hi: 0x2ffff, Stride: 1},
		{Lo: 0x3fffe, Hi: 0x3ffff, Stride: 1},
		{Lo: 0x4fffe, Hi: 0x4ffff, Stride: 1},
		{Lo: 0x5fffe, Hi: 0x5ffff, Stride: 1},
		{Lo: 0x6fffe, Hi: 0x6ffff, Stride: 1},
		{Lo: 0x7fffe, Hi: 0x7ffff, Stride: 1},
		{Lo: 0x8fffe, Hi: 0x8ffff, Stride: 1},
		{Lo: 0x9fffe, Hi: 0x9ffff, Stride: 1},
		{Lo: 0xafffe, Hi: 0xaffff, Stride: 1},
		{Lo: 0xbfffe, Hi: 0xbffff, Stride: 1},
		{Lo: 0xcfffe, Hi: 0xcffff, Stride: 1},
		{Lo: 0xdfffe, Hi: 0xdffff, Stride: 1},
		{Lo: 0xe0001, Hi: 0xe0020, Stride: 31},
		{Lo: 0xe0021, Hi: 0xe007f, Stride: 1},
		{Lo: 0xefffe, Hi: 0x10ffff, Stride: 1},
	},
}

using golang.org/x/text/unicode/rangetable do rangetable.Merge(unicode.Co, unicode.Cf, unicode.Cs, unicode.Noncharacter_Code_Point) (we also care about unicode.Cc but that's handled by hand) this makes the lookup in escape quite a bit faster (over 5×)

Functions

func CommaSeparatedList

func CommaSeparatedList(str string) []string

CommaSeparatedList takes a comma-separated series of identifiers, and returns a slice of the space-trimmed identifiers, without empty entries. So " foo ,, bar,baz" -> {"foo", "bar", "baz"}

func Deduplicate

func Deduplicate(sl []string) []string

Deduplicate returns a newly allocated slice with the same contents as the input, excluding duplicates.

func ElliptLeft

func ElliptLeft(str string, n int) string

ElliptLeft returns a string that is at most n runes long, replacing the first rune with an ellipsis if necessary. If N is less than 1 it's treated as a 1.

func ElliptRight

func ElliptRight(str string, n int) string

ElliptRight returns a string that is at most n runes long, replacing the last rune with an ellipsis if necessary. If N is less than 1 it's treated as a 1.

func FindCommonPrefix

func FindCommonPrefix(patterns []string) (string, error)

func Intersection

func Intersection(slices ...[]string) []string

Intersection computes the intersection of a set of slices, treating each slice as a set. It does not mutate any of the input slices and returns a new slice. It is recursive.

func IntsToCommaSeparated

func IntsToCommaSeparated(vals []int) string

IntsToCommaSeparated converts an int array to a comma-separated string without whitespace

func JoinNonEmpty

func JoinNonEmpty(strs []string, sep string) string

JoinNonEmpty concatenates non-empty strings using sep as separator, and trimming sep from beginning and end of the strings. This overcomes a problem with strings.Join, which will introduce separators for empty strings.

func ListContains

func ListContains(list []string, str string) bool

ListContains determines whether the given string is contained in the given list of strings.

func MultiCommaSeparatedList

func MultiCommaSeparatedList(strs []string) []string

MultiCommaSeparatedList parses each string in strs with CommaSeparatedList and returns the concatenation of all parsed values.

func ParseByteSize

func ParseByteSize(inp string) (int64, error)

ParseByteSize parses a value like 500kB and returns the number in bytes. The case of the unit will be ignored for user convenience.

func Quoted

func Quoted(names []string) string

Quoted formats a slice of strings to a quoted list of comma-separated strings, e.g. `"snap1", "snap2"`

func SizeToStr

func SizeToStr(size int64) string

SizeToStr converts the given size in bytes to a readable string

func SortedListContains

func SortedListContains(list []string, str string) bool

SortedListContains determines whether the given string is contained in the given list of strings, which must be sorted.

func SortedListsUniqueMerge

func SortedListsUniqueMerge(sl1, sl2 []string) []string

SortedListsUniqueMerge merges the two given sorted lists of strings, repeated values will appear once in the result.

func SplitUnit

func SplitUnit(inp string) (number int64, unit string, err error)

SplitUnit takes a string of the form "123unit" and splits it into the number and non-number parts (123,"unit").

func TruncateOutput

func TruncateOutput(data []byte, maxLines, maxBytes int) []byte

TruncateOutput truncates input data by maxLines, imposing maxBytes limit (total) for them. The maxLines may be 0 to avoid the constraint on number of lines.

func VersionCompare

func VersionCompare(va, vb string) (res int, err error)

VersionCompare compare two version strings that follow the debian version policy and Returns:

-1 if a is smaller than b
 0 if a equals b
+1 if a is bigger than b

func WordWrap

func WordWrap(out io.Writer, text []rune, indent, indent2 string, termWidth int) error

WordWrap wraps the given text to the given width, prefixing the first line with indent and the remaining lines with indent2

func WordWrapPadded

func WordWrapPadded(out io.Writer, text []rune, pad string, termWidth int) error

WordWrapPadded wraps the given text, assumed to be part of a block-style yaml string, to fit into termWidth, preserving the line's indent, and writes it out prepending padding to each line.

Types

type LimitedBuffer

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

func NewLimitedBuffer

func NewLimitedBuffer(maxLines, maxBytes int) *LimitedBuffer

func (*LimitedBuffer) Bytes

func (lb *LimitedBuffer) Bytes() []byte

func (*LimitedBuffer) Write

func (lb *LimitedBuffer) Write(data []byte) (int, error)

type MatchCounter

type MatchCounter struct {
	// Regexp to use to find matches in the stream
	Regexp *regexp.Regexp
	// Maximum number of matches to keep; if < 0, keep all matches
	N int
	// LastN when true indicates that only N last matches shall be kept.
	LastN bool
	// contains filtered or unexported fields
}

A MatchCounter is a discarding io.Writer that retains up to N matches to its Regexp before just counting matches.

It does not work with regexps that cross newlines; in fact it will probably not work if the data written isn't line-orineted.

If Regexp is not set (or nil), it matches whole non-empty lines.

func (*MatchCounter) Matches

func (w *MatchCounter) Matches() ([]string, int)

Matches returns the first few matches, and the total number of matches seen.

func (*MatchCounter) Write

func (w *MatchCounter) Write(p []byte) (int, error)

type OrderedMap

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

OrderedMap is a map of strings to strings that preserves the insert order when calling "Keys()".

Heavily based on the spread.Environment code (thanks for that!)

func NewOrderedMap

func NewOrderedMap(pairs ...string) *OrderedMap

NewOrderedMap creates a new ordered map initialized with the given pairs of strings.

func (*OrderedMap) Copy

func (o *OrderedMap) Copy() *OrderedMap

Copy makes a copy of the map

func (*OrderedMap) Del

func (o *OrderedMap) Del(key string)

Del removes the given key from the data structure

func (*OrderedMap) Get

func (o *OrderedMap) Get(k string) string

Get returns the value for the given key

func (*OrderedMap) Keys

func (o *OrderedMap) Keys() []string

Keys returns a list of keys in the map sorted by insertion order

func (*OrderedMap) Set

func (o *OrderedMap) Set(k, v string)

Set adds the given key, value to the map. If the key already exists it is removed and the new value is put on the end.

func (*OrderedMap) UnmarshalYAML

func (o *OrderedMap) UnmarshalYAML(u func(interface{}) error) error

UnmarshalYAML unmarshals a yaml string map and preserves the order

type OrderedSet

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

OrderedSet is a set of strings that maintains the order of insertion.

The order of putting items into the set is retained. Putting items "a", "b" and then "a", results in order "a", "b".

External synchronization is required for safe concurrent access.

func (*OrderedSet) Contains

func (o *OrderedSet) Contains(item string) bool

Contains returns true if the set contains a given item.

Contains is O(1) in the size of the set.

func (*OrderedSet) IndexOf

func (o *OrderedSet) IndexOf(item string) (idx int, ok bool)

IndexOf returns the position of an item in the set.

func (*OrderedSet) Items

func (o *OrderedSet) Items() []string

Items returns a slice of strings representing insertion order.

Items is O(N) in the size of the set.

func (*OrderedSet) Put

func (o *OrderedSet) Put(item string)

Put adds an item into the set.

If the item was not present then it is stored and ordered after all existing elements. If the item was already present its position is not changed.

Put is O(1) in the size of the set.

func (*OrderedSet) Size

func (o *OrderedSet) Size() int

Size returns the number of elements in the set.

type PathIterator

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

PathIterator traverses through parts (directories and files) of some path. The filesystem is never consulted, traversal is done purely in memory.

The iterator is useful in implementing secure traversal of absolute paths using the common idiom of opening the root directory followed by a chain of openat calls.

A simple example on how to use the iterator: ``` iter := NewPathIterator(path)

for iter.Next() {
   // Use iter.CurrentBase() with openat(2) family of functions.
   // Use iter.CurrentPath() or iter.CurrentDir() for context.
}

```

func NewPathIterator

func NewPathIterator(path string) (*PathIterator, error)

NewPathIterator returns an iterator for traversing the given path. The path must be clean (except for a possible trailing slash).

func (*PathIterator) CurrentBase

func (iter *PathIterator) CurrentBase() string

CurrentBase returns the last element of the current path. It removes any trailing slash unless the last element of the current path is the filesystem root. Before any iteration with Next, CurrentBase returns an empty string. Example:

iter := NewPathIterator("/foo/bar")  // iter.CurrentBase() == ""
iter.Next() // iter.CurrentBase() == "/" && iter.CurrentPath() == "/"
iter.Next() // iter.CurrentBase() == "foo" && iter.CurrentPath() == "/foo"
iter.Next() // iter.CurrentBase() == "bar" && iter.CurrentPath() == "/foo/bar"

func (*PathIterator) CurrentDir

func (iter *PathIterator) CurrentDir() string

CurrentDir returns all but the last element of the current path. The result always removes a trailing slash unless CurrentDir is the filesystem root. If the current path is empty because either Next has not yet been called or the current path only contains one element, then CurrentDir will return an empty string. Example:

iter := NewPathIterator("/foo/bar")  // iter.CurrentDir() == ""
iter.Next() // iter.CurrentDir() == "" && iter.CurrentPath() == "/"
iter.Next() // iter.CurrentDir() == "/" && iter.CurrentPath() == "/foo"
iter.Next() // iter.CurrentDir() == "/foo" && iter.CurrentPath() == "/foo/bar"

Example:

iter := NewPathIterator("foo/bar")  // iter.CurrentDir() == ""
iter.Next() // iter.CurrentDir() == "" && iter.CurrentPath() == "foo"
iter.Next() // iter.CurrentDir() == "foo" && iter.CurrentPath() == "foo/bar"

func (*PathIterator) CurrentPath

func (iter *PathIterator) CurrentPath() string

CurrentPath returns the path up to the current depth. Before any iteration with Next, CurrentPath returns an empty string. Example:

	iter := NewPathIterator("/foo/bar")  // iter.CurrentPath() == ""
 	iter.Next() // iter.CurrentPath() == "/" && iter.Depth() == 1
 	iter.Next() // iter.CurrentPath() == "/foo" && iter.Depth() == 2
 	iter.Next() // iter.CurrentPath() == "/foo/bar" && iter.Depth() == 3

func (*PathIterator) CurrentPathPlusSlash

func (iter *PathIterator) CurrentPathPlusSlash() string

Adds a trailing slash to the current path even if the current path does not have one. Before any iteration with Next, CurrentPathPlusSlash returns an empty string. Example:

    iter := NewPathIterator("/foo/bar")  // iter.CurrentPathPlusSlash() == ""
	iter.Next() // iter.CurrentPathPlusSlash() == "/"
	iter.Next() // iter.CurrentPathPlusSlash() == "/foo/"
	iter.Next() // iter.CurrentPathPlusSlash() == "/foo/bar/"

func (*PathIterator) Depth

func (iter *PathIterator) Depth() int

Depth returns the directory depth of the current path.

This is equal to the number of traversed directories, including that of the root directory.

func (*PathIterator) IsCurrentBaseLeaf

func (iter *PathIterator) IsCurrentBaseLeaf() bool

IsCurrentBaseLeaf returns true if CurrentBase is the leaf of the path. Example:

iter := NewPathIterator("foo/bar")  // iter.IsCurrentBaseLeaf() == false
iter.Next() // iter.CurrentBase() == "foo" && iter.IsCurrentBaseLeaf() == false
iter.Next() // iter.CurrentBase() == "bar" && iter.IsCurrentBaseLeaf() == true

func (*PathIterator) Next

func (iter *PathIterator) Next() bool

Next advances the iterator to the next name, returning true if one is found.

If this method returns false then no change is made and all helper methods retain their previous return values.

func (*PathIterator) Path

func (iter *PathIterator) Path() string

Path returns the entire path being traversed.

func (*PathIterator) Rewind

func (iter *PathIterator) Rewind()

Rewind returns the iterator to the initial state, allowing the path to be traversed again.

Directories

Path Synopsis
Package shlex implements a simple lexer which splits input in to tokens using shell-style rules for quoting and commenting.
Package shlex implements a simple lexer which splits input in to tokens using shell-style rules for quoting and commenting.

Jump to

Keyboard shortcuts

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