stringutils

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2023 License: MIT Imports: 12 Imported by: 26

README

stringutils

Some string utils

UnsafeString([]byte) string return unsafe string from bytes slice indirectly (without allocation)

UnsafeStringFromPtr(*byte, length) string return unsafe string from bytes slice pointer indirectly (without allocation)

UnsafeStringBytes(*string) []bytes return unsafe string bytes indirectly (without allocation)

Split2(s string, sep string) (string, string, int) Split2 return the split string results (without memory allocations). Use Index for find separator.

Split(s string, sep string, buf []string) []string // Split return splitted slice (use pre-allocated buffer, reallocated if needed). Use Index for find separator. SplitByte(s string, sep byte, buf []string) []string // SplitByte return splitted slice (use pre-allocated buffer, reallocated if needed). Use Index for find separator. SplitRune(s string, sep rune, buf []string) []string // SplitByte return splitted slice (use pre-allocated buffer, reallocated if needed). Use Index for find separator. For Unicode long-width rune it's slower than Split

SplitN(s string, sep string, buf []string) []string // SplitN deprecated and removed, use Split

Reverse(string) string return reversed string (rune-wise left to right) ReverseSegments(string, delim) string return reversed string by segments around string delimiter (ReverseSegments("hello, world", ", ") return world, hello).

Replace(s, old, new string, n int) (string, changed) // Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. Also return change flag.

ReplaceAll(s, old, new string) (string, changed) // Replace returns a copy of the string s with all non-overlapping instances of old replaced by new. Also return change flag.

WriteString(w io.Writer, s string) (int, error) writes the contents of the string s to w, which accepts a slice of bytes. No bytes alloation instead of io.WriteString.

Builder very simular to strings.Builder, but has better perfomance in some cases (reallocate with scale 2, if needed, also append numbers in-place) (at golang 1.14).

Template is a simple templating system

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	String     func(ptr *byte, length int) string = UnsafeStringFromPtr
	StringData func(str string) *byte             = UnsafeStringBytePtr
)

compability with Golang 1.20 proposal https://github.com/golang/go/issues/53003

View Source
var InitTemplate = NewTemplate

for backward compability

Functions

func Clone added in v0.1.4

func Clone(s string) string

Clone returns a fresh copy of s. It guarantees to make a copy of s into a new allocation, which can be important when retaining only a small substring of a much larger string. Using Clone can help such programs use less memory. Of course, since using Clone makes a copy, overuse of Clone can make programs use more memory. Clone should typically be used only rarely, and only when profiling indicates that it is needed. For strings of length zero the string "" will be returned and no allocation is made.

func CloneBytes added in v0.1.5

func CloneBytes(b []byte) []byte

Clone returns a fresh copy of s. It guarantees to make a copy of s into a new allocation, which can be important when retaining only a small substring of a much larger string. Using Clone can help such programs use less memory. Of course, since using Clone makes a copy, overuse of Clone can make programs use more memory. Clone should typically be used only rarely, and only when profiling indicates that it is needed. For strings of length zero the string "" will be returned and no allocation is made.

func EqualFold added in v0.1.5

func EqualFold(b, s string) bool

EqualFold tests ascii strings for equality case-insensitively

func EqualFoldBytes added in v0.1.5

func EqualFoldBytes(b, s []byte) bool

EqualFoldBytes tests ascii slices for equality case-insensitively

func NewTemplateParam added in v0.1.1

func NewTemplateParam(s string) templateParam

func Replace added in v0.0.13

func Replace(s, old, new string, n int) (string, bool)

Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. Also return change flag. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements.

func ReplaceAll added in v0.0.13

func ReplaceAll(s, old, new string) (string, bool)

ReplaceAll returns a copy of the string s with all non-overlapping instances of old replaced by new. Also return change flag. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.

func Reverse added in v0.0.12

func Reverse(s string) string

Reverse return reversed string (rune-wise left to right).

func ReverseSegments added in v0.0.12

func ReverseSegments(target, delim string) string

ReverseSegments return reversed string by segments around delimiter.

func Split added in v0.1.0

func Split(s string, sep string, buf []string) []string

Split return splitted slice (use pre-allocated buffer) (realloc if needed)

func Split2

func Split2(s string, sep string) (string, string, int)

Split2 return the split string results (without memory allocations)

If sep string not found: 's' '' 1
If s or sep string is empthy: 's' '' 1
In other cases: 's0' 's2' 2

func SplitByte added in v0.1.0

func SplitByte(s string, sep byte, buf []string) []string

SplitByte return splitted slice (use pre-allocated buffer) (realloc if needed)

func SplitRune added in v0.1.0

func SplitRune(s string, sep rune, buf []string) []string

SplitRune return splitted slice (use pre-allocated buffer) (realloc if needed)

func ToLower added in v0.1.5

func ToLower(b string) string

ToLower converts ascii string to lower-case

func ToLowerBytes added in v0.1.5

func ToLowerBytes(b []byte) []byte

ToLowerBytes converts ascii slice to lower-case in-place.

func ToUpper added in v0.1.5

func ToUpper(b string) string

ToUpper converts ascii string to upper-case

func ToUpperBytes added in v0.1.5

func ToUpperBytes(b []byte) []byte

ToUpperBytes converts ascii slice to upper-case in-place.

func Trim added in v0.1.5

func Trim(s string, cutset byte) string

Trim is the equivalent of strings.Trim

func TrimBytes added in v0.1.5

func TrimBytes(b []byte, cutset byte) []byte

TrimBytes is the equivalent of bytes.Trim

func TrimLeft added in v0.1.5

func TrimLeft(s string, cutset byte) string

TrimLeft is the equivalent of strings.TrimLeft

func TrimLeftBytes added in v0.1.5

func TrimLeftBytes(b []byte, cutset byte) []byte

TrimLeftBytes is the equivalent of bytes.TrimLeft

func TrimRight added in v0.1.5

func TrimRight(s string, cutset byte) string

TrimRight is the equivalent of strings.TrimRight

func TrimRightBytes added in v0.1.5

func TrimRightBytes(b []byte, cutset byte) []byte

TrimRightBytes is the equivalent of bytes.TrimRight

func UUID added in v0.1.5

func UUID() string

UUID generates an universally unique identifier (UUID)

func UnsafeBytes added in v0.1.5

func UnsafeBytes(ptr *byte, length, cap int) (b []byte)

func UnsafeString

func UnsafeString(b []byte) string

UnsafeString returns the string under byte buffer

func UnsafeStringBytePtr added in v0.1.5

func UnsafeStringBytePtr(s string) *byte

UnsafeStringBytePtr returns the string byte ptr

func UnsafeStringBytes

func UnsafeStringBytes(s *string) []byte

UnsafeStringBytes returns the string bytes

func UnsafeStringFromPtr

func UnsafeStringFromPtr(ptr *byte, length int) (s string)

UnsafeStringFromPtr returns the string with specific length under byte buffer

func WriteString added in v0.0.14

func WriteString(w io.Writer, s string) (n int, err error)

WriteString writes the contents of the string s to w, which accepts a slice of bytes. No bytes alloation instead of io.WriteString.

Types

type Builder

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

A Builder is used to efficiently build a string using Write methods (with better perfomance than strings.Builder). It minimizes memory copying. The zero value is ready to use. Do not copy a non-zero Builder.

func (*Builder) Bytes

func (sb *Builder) Bytes() []byte

Bytes returns the accumulated bytes.

func (*Builder) Cap

func (sb *Builder) Cap() int

Cap returns the capacity of the builder's underlying byte slice. It is the total space allocated for the string being built and includes any bytes already written.

func (*Builder) Flush added in v0.0.15

func (sb *Builder) Flush() error

Flush fake makethod for combatibility with buffered writer

func (*Builder) Grow

func (sb *Builder) Grow(capacity int)

Grow grows b's capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to b without another allocation.

func (*Builder) Len

func (sb *Builder) Len() int

Len returns the number of accumulated bytes; b.Len() == len(b.String()).

func (*Builder) Map

func (sb *Builder) Map(mapping func(rune) rune, s string)

based on strings.Map Map returns a copy of the string s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.

func (*Builder) Release

func (sb *Builder) Release()

Release resets the Builder to be empty and free buffer

func (*Builder) Reset

func (sb *Builder) Reset()

Reset resets the Builder to be empty.

func (*Builder) String

func (sb *Builder) String() string

String returns the accumulated string.

func (*Builder) Truncate added in v0.1.3

func (sb *Builder) Truncate(length int)

Truncate descrease the Builder length (dangerouse for partually truncated UTF strings).

func (*Builder) Write

func (sb *Builder) Write(bytes []byte) (int, error)

Write like WriteBytes, but realized io.Writer interface

func (*Builder) WriteBool added in v0.1.2

func (sb *Builder) WriteBool(v bool)

WriteBool appends the string form of the bool v, as generated by FormatBool.

func (*Builder) WriteByte

func (sb *Builder) WriteByte(c byte) error

WriteByte appends the byte c to b's buffer.

func (*Builder) WriteBytes

func (sb *Builder) WriteBytes(bytes []byte)

WriteBytes appends the contents of p to b's buffer.

func (*Builder) WriteFloat

func (sb *Builder) WriteFloat(f float64, fmt byte, prec, bitSize int)

WriteFloat appends the string form of the floating-point number f, as generated by FormatFloat.

func (*Builder) WriteInt

func (sb *Builder) WriteInt(i int64, base int)

WriteInt appends the string form of the integer i, as generated by FormatInt.

func (*Builder) WriteQuote added in v0.1.2

func (sb *Builder) WriteQuote(s string)

WriteQuote appends the string form of the quoted string s, as generated by Quote.

func (*Builder) WriteQuoteRune added in v0.1.2

func (sb *Builder) WriteQuoteRune(r rune)

WriteQuoteRune appends the string form of the quoted rune r, as generated by QuoteRune.

func (*Builder) WriteQuoteRuneToASCII added in v0.1.2

func (sb *Builder) WriteQuoteRuneToASCII(r rune)

WriteQuoteRuneToASCII appends the string form of the single-quoted rune r, as generated by QuoteRuneToASCII.

func (*Builder) WriteQuoteRuneToGraphic added in v0.1.2

func (sb *Builder) WriteQuoteRuneToGraphic(r rune)

WriteQuoteRuneToGraphic appends the string form of the quoted rune r, as generated by QuoteRuneToGraphic.

func (*Builder) WriteQuoteToASCII added in v0.1.2

func (sb *Builder) WriteQuoteToASCII(s string)

WriteQuoteToASCII appends the string form of the single-quoted string s, as generated by QuoteToASCII.

func (*Builder) WriteQuoteToGraphic added in v0.1.2

func (sb *Builder) WriteQuoteToGraphic(s string)

WriteQuoteToGraphic appends the string form of the quoted string s, as generated by QuoteToGraphic.

func (*Builder) WriteRune

func (sb *Builder) WriteRune(r rune) (int, error)

WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.

func (*Builder) WriteString

func (sb *Builder) WriteString(s string) (int, error)

WriteString appends the contents of s to b's buffer.

func (*Builder) WriteStringLower

func (sb *Builder) WriteStringLower(s string)

based on strings.ToLower ToLower returns s with all Unicode letters mapped to their lower case.

func (*Builder) WriteStringUpper

func (sb *Builder) WriteStringUpper(s string)

based on strings.ToUpper ToUpper returns s with all Unicode letters mapped to their upper case.

func (*Builder) WriteUint

func (sb *Builder) WriteUint(i uint64, base int)

WriteUint appends the string form of the unsigned integer i, as generated by FormatUint.

type Template

type Template []interface{}

Template parsed and splited format string (stored in first field)

func NewTemplate added in v0.1.1

func NewTemplate(format string) (Template, error)

NewTemplate parse and split format string (format string stored in first field)

@format Format string like 'string %{param} %{param1.param2}'

func (*Template) Execute

func (t *Template) Execute(params map[string]interface{}) (string, error)

Execute process template with mapped params

@Params Params in map[string]interface{}

params := map[string]interface{}{
	"param":  "URL",
	"param1": map[string]interface{}{ "param2": "2" },
}

func (*Template) ExecutePartial added in v0.1.1

func (t *Template) ExecutePartial(params map[string]interface{}) (string, bool)

ExecutePartial process template with mapped params, if parameter not found - use segment as is (without error)

@Params Params in map[string]interface{}

params := map[string]interface{}{
	"param":  "URL",
	"param1": map[string]interface{}{ "param2": "2" },
}

Jump to

Keyboard shortcuts

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