stringutils

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2022 License: MIT Imports: 9 Imported by: 30

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 InitTemplate = NewTemplate

for backward compability

Functions

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 UnsafeString

func UnsafeString(b []byte) string

UnsafeString returns the string under byte buffer

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) Write

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

Write like WriteBytes, but realized io.Writer interface

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, to dst and returns the extended buffer.

func (*Builder) WriteInt

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

WriteInt appends the string form of the integer i, as generated by FormatInt, to dst and returns the extended buffer.

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, to dst and returns the extended buffer.

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