strutil

package
v0.4.13 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

String utility pkg.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelCase

func CamelCase(s string) string

func ContainsAnyStr

func ContainsAnyStr(s string, substrings ...string) bool

func ContainsAnyStrIgnoreCase

func ContainsAnyStrIgnoreCase(s string, substrings ...string) bool

func CutAfterLast added in v0.4.10

func CutAfterLast(s string, sep string) string

func CutPrefixIgnoreCase

func CutPrefixIgnoreCase(s string, prefix string) (string, bool)

Cut prefix from s in a case-insensitive way.

func CutPrefixIgnoreCaseAny

func CutPrefixIgnoreCaseAny(s string, prefix ...string) (string, bool)

Cut any prefix from s in a case-insensitive way.

func CutSuffixIgnoreCase

func CutSuffixIgnoreCase(s string, suffix string) (string, bool)

Cut suffix from s in a case-insensitive way.

func CutSuffixIgnoreCaseAny

func CutSuffixIgnoreCaseAny(s string, suffix ...string) (string, bool)

Cut any suffix from s in a case-insensitive way.

func EqualAnyFold added in v0.4.3

func EqualAnyFold(s string, canditates ...string) bool

func EqualAnyStr

func EqualAnyStr(s string, canditates ...string) bool

func EqualAnyStrSlice added in v0.4.10

func EqualAnyStrSlice(s string, canditates []string) bool

func FmtFloat

func FmtFloat(f float64, width int, precision int) string

func HasAnyPrefix

func HasAnyPrefix(s string, suf ...string) bool

func HasAnySuffix

func HasAnySuffix(s string, suf ...string) bool

func HasPrefixIgnoreCase

func HasPrefixIgnoreCase(s string, prefix string) bool

Check if s has the prefix in a case-insensitive way.

func HasSuffixIgnoreCase

func HasSuffixIgnoreCase(s string, suffix string) bool

Check if s has the suffix in a case-insensitive way.

func IsBlankStr

func IsBlankStr(s string) bool

Check if the string is blank

func LastNStr

func LastNStr(s string, n int) string

func MatchPath

func MatchPath(pattern, s string) bool

func MatchPathAny

func MatchPathAny(pattern []string, s string) bool

Match any of the path pattern

See MatchPathAnyVal.

func MatchPathAnyVal added in v0.3.4

func MatchPathAnyVal(pattern []string, s string) (string, bool)

Match any of the path pattern.

func MaxLenStr

func MaxLenStr(s string, max int) string

Substring such that len(s) <= max

func NamedSprintf

func NamedSprintf(pat string, p map[string]any) string

Format message using named args, e.g., '${startTime} ${message}'

func NamedSprintfkv added in v0.4.9

func NamedSprintfkv[T constraint.BasicValue](pat string, kv ...T) string

Format message using fields in struct, e.g., '${startTime} ${message}'

e.g.,

NamedSprintfkv("my name is ${name}", "name", "slim shady")

func NamedSprintfv

func NamedSprintfv(pat string, v any) string

Format message using fields in struct, e.g., '${startTime} ${message}'

Equivalent to following code:

NamedSprintf(pat, ReflectGenMap(myStruct))

func NewReplacer added in v0.4.9

func NewReplacer(oldnew ...pair.StrPair) *strings.Replacer

func PadNum

func PadNum(n int, digit int) string

func PadSpace

func PadSpace(n int, s string) string

Pad spaces.

if n > 0, pad left, else pad right.

func PadToken

func PadToken(n int, s string, tok string) string

func QuoteStr

func QuoteStr(s string) string

func ReplaceAll added in v0.4.9

func ReplaceAll(s string, oldnew ...pair.StrPair) string

func RuneWidth

func RuneWidth(r rune) int

func SAddLineIndent

func SAddLineIndent(s string, indentChar string) string

func Spaces

func Spaces(count int) string

func SplitKV

func SplitKV(s string, token string) (string, string, bool)

Splist kv pair. Returns false if token is not found or key is absent.

func SplitStr

func SplitStr(s, sep string) []string

func SplitStrAnyRune added in v0.3.10

func SplitStrAnyRune(s, runes string) []string

func StrByteLen added in v0.4.0

func StrByteLen(s string) int

Get string's length in bytes

func StrWidth

func StrWidth(s string) int

func Tabs

func Tabs(count int) string

func ToStr

func ToStr(v any) string

func TrimSpace added in v0.4.4

func TrimSpace(s string, extra ...rune) string

func TrimSpaceAnd deprecated added in v0.4.3

func TrimSpaceAnd(s string, extraRunes string) string

Deprecated: since v0.4.4, use TrimSpace instead.

func TrimSpaceLeft added in v0.3.4

func TrimSpaceLeft(s string, extra ...rune) string

func TrimSpaceRight added in v0.3.4

func TrimSpaceRight(s string, extra ...rune) string

func TrimStrSlice

func TrimStrSlice(s []string)

func UnquoteStr

func UnquoteStr(s string) string

func UnsafeByt2Str added in v0.4.0

func UnsafeByt2Str(b []byte) string

Convert []byte to string without alloc.

Both the []byte and the string share the same memory.

Any modification on the original []byte is reflected on the returned string.

byt = []byte("abc")
s = UnsafeByt2Str(byt) // "abc" using the same memory
byt[0] = 'd' // modified in place at 0, also reflected on s ("dbc")

Tricks from https://github.com/valyala/fasthttp and https://go101.org/article/unsafe.html

See: https://github.com/golang/go/issues/53003

func UnsafeStr2Byt added in v0.4.0

func UnsafeStr2Byt(s string) (b []byte)

Convert string to []byte without alloc.

Both the []byte and the string share the same memory.

The resulting []byte is not modifiable, program will panic if modified.

s := "abc"
byt := UnsafeStr2Byt(s) // "abc" but in []byte
byt[0] = 'd' // will panic

Tricks from https://github.com/valyala/fasthttp and https://go101.org/article/unsafe.html

See: https://github.com/golang/go/issues/53003

Types

type Builder added in v0.4.12

type Builder struct {
	*strings.Builder
	// contains filtered or unexported fields
}

Enhanced strings.Builder.

Use NewBuilder instantiate.

func NewBuilder added in v0.4.11

func NewBuilder() *Builder

func (*Builder) DecrIndent added in v0.4.12

func (b *Builder) DecrIndent() *Builder

func (*Builder) IncrIndent added in v0.4.12

func (b *Builder) IncrIndent() *Builder

func (*Builder) NewLine added in v0.4.13

func (b *Builder) NewLine(st string) *Builder

func (*Builder) PPrintln added in v0.4.13

func (b *Builder) PPrintln(st string) *Builder

Add new content.

Only add line break at the start if the builder is not empty.

func (*Builder) PPrintlnf added in v0.4.13

func (b *Builder) PPrintlnf(st string, args ...any) *Builder

Add new formatted content

Only add line break at the start if the builder is not empty.

func (*Builder) Printf added in v0.4.12

func (b *Builder) Printf(st string, args ...any) *Builder

func (*Builder) Println added in v0.4.12

func (b *Builder) Println(st string) *Builder

Add new content.

Always add line break at the end of the line.

func (*Builder) Printlnf added in v0.4.12

func (b *Builder) Printlnf(st string, args ...any) *Builder

Add new formatted content.

Always add line break at the end of the line.

func (*Builder) StepIn added in v0.4.12

func (b *Builder) StepIn(f func(b *Builder)) *Builder

func (*Builder) WithIndent added in v0.4.12

func (b *Builder) WithIndent(s string, c int) *Builder

func (*Builder) WithIndentCnt added in v0.4.12

func (b *Builder) WithIndentCnt(ind int)

func (*Builder) WithIndentStr added in v0.4.12

func (b *Builder) WithIndentStr(ind string)

func (*Builder) WithLinePrefix added in v0.4.12

func (b *Builder) WithLinePrefix(p string) *Builder

func (*Builder) WithLineSuffix added in v0.4.12

func (b *Builder) WithLineSuffix(p string) *Builder

type IndWritef

type IndWritef = func(indentCnt int, pat string, args ...any)

func NewIndWritef

func NewIndWritef(indentStr string) (*strings.Builder, IndWritef)

Wrap strings.Builder, and returns a Writef func that automatically adds indentation.

type IndentWriter

type IndentWriter struct {
	*strings.Builder
	// contains filtered or unexported fields
}

func NewIndentWriter

func NewIndentWriter(indentStr string) IndentWriter

func (*IndentWriter) DecrIndent

func (i *IndentWriter) DecrIndent()

func (*IndentWriter) IncrIndent

func (i *IndentWriter) IncrIndent()

func (*IndentWriter) NoIndWritef

func (i *IndentWriter) NoIndWritef(pat string, args ...any) *IndentWriter

Writef without indentation and with line break.

func (*IndentWriter) NoLbWritef

func (i *IndentWriter) NoLbWritef(pat string, args ...any) *IndentWriter

Writef with indentation and without line break.

func (*IndentWriter) NoLbWritefWhen

func (i *IndentWriter) NoLbWritefWhen(condition bool, pat string, args ...any) *IndentWriter

NoLbWritef(..) when condition is true else Writef(..).

func (*IndentWriter) SetIndent

func (i *IndentWriter) SetIndent(ind int)

func (*IndentWriter) StepIn

func (i *IndentWriter) StepIn(f func(iw *IndentWriter)) *IndentWriter

func (*IndentWriter) Writef

func (i *IndentWriter) Writef(pat string, args ...any) *IndentWriter

type SLPinter

type SLPinter struct {
	*strings.Builder
	LineSuffix string
	LinePrefix string
}

func (*SLPinter) Printf

func (s *SLPinter) Printf(st string, args ...any)

func (*SLPinter) Println

func (s *SLPinter) Println(st string)

func (*SLPinter) Printlnf

func (s *SLPinter) Printlnf(st string, args ...any)

Jump to

Keyboard shortcuts

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