stringx

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: MIT Imports: 9 Imported by: 10

Documentation

Overview

Package stringx provides string manipulation functionality specific to protobuf.

Index

Constants

View Source
const (
	// DefaultBuilderSize is the default size of the Builder in bytes.
	DefaultBuilderSize = 1024

	// MaxBuilderSize is the maximum size of the Builder in bytes.
	MaxBuilderSize = 4096
)

Variables

This section is empty.

Functions

func AsciiEqualFold

func AsciiEqualFold(s, t []byte) bool

AsciiEqualFold is a specialization of bytes.EqualFold for use when s is all ASCII (but may contain non-letters) and contains no special-folding letters. See comments on FoldFunc.

func EnumValueName added in v0.1.1

func EnumValueName(s string) string

EnumValueName derives the camel-cased enum value name. See protoc v3.8.0: src/google/protobuf/descriptor.cc:297-313

func EqualFoldRight

func EqualFoldRight(s, t []byte) bool

EqualFoldRight is a specialization of bytes.EqualFold when s is known to be all ASCII (including punctuation), but contains an 's', 'S', 'k', or 'K', requiring a Unicode fold on the bytes in t. See comments on FoldFunc.

func FoldFunc

func FoldFunc(s []byte) func(s, t []byte) bool

FoldFunc returns one of four different case folding equivalence functions, from most general (and slow) to fastest:

1) bytes.EqualFold, if the key s contains any non-ASCII UTF-8 2) EqualFoldRight, if s contains special folding ASCII ('k', 'K', 's', 'S') 3) AsciiEqualFold, no special, but includes non-letters (including _) 4) SimpleLetterEqualFold, no specials, no non-letters.

The letters S and K are special because they map to 3 runes, not just 2:

  • S maps to s and to U+017F 'ſ' Latin small letter long s
  • k maps to K and to U+212A 'K' Kelvin sign

See https://play.golang.org/p/tTxjOc0OGo

The returned function is specialized for matching against s and should only be given s. It's not curried for performance reasons.

func GetBuilder added in v0.1.12

func GetBuilder() *strings.Builder

GetBuilder retrieves a strings.Builder from the pool.

func GoCamelCase added in v0.1.1

func GoCamelCase(s string) string

GoCamelCase camel-cases a protobuf name for use as a Go identifier.

If there is an interior underscore followed by a lower case letter, drop the underscore and convert the letter to upper case.

func GoSanitized added in v0.1.1

func GoSanitized(s string) string

GoSanitized converts a string to a valid Go identifier.

func Indices

func Indices(str, substr string) []int

func IsAllBlank

func IsAllBlank[S ~string](ss ...S) bool

IsAllBlank Checks if all of the CharSequences are empty ("") or whitespace only.

func IsAllEmpty

func IsAllEmpty[S ~string](ss ...S) bool

IsAllEmpty Checks if all of the strings are empty ("")

func IsAnyBlank

func IsAnyBlank[S ~string](ss ...S) bool

IsAnyBlank Checks if any of the string are empty ("") or whitespace only.

func IsAnyEmpty

func IsAnyEmpty[S ~string](ss ...S) bool

IsAnyEmpty Checks if any of the strings are empty ("")

func IsBlank

func IsBlank[S ~string](s S) bool

IsBlank Checks if a string is empty ("") or whitespace only.

func IsEmpty

func IsEmpty[S ~string](s S) bool

IsEmpty checks if a string is empty ("")

func IsNotBlank

func IsNotBlank[S ~string](s S) bool

IsNotBlank Checks if a string is not empty ("") and not whitespace only.

func IsNotEmpty

func IsNotEmpty[S ~string](s S) bool

IsNotEmpty Checks if a string is not empty ("")

func IsValidNumber

func IsValidNumber(s string) bool

IsValidNumber reports whether s is a valid number.

func JSONCamelCase added in v0.1.1

func JSONCamelCase(s string) string

JSONCamelCase converts a snake_case identifier to a camelCase identifier, according to the protobuf JSON specification.

func JSONSnakeCase added in v0.1.1

func JSONSnakeCase(s string) string

JSONSnakeCase converts a camelCase identifier to a snake_case identifier, according to the protobuf JSON specification.

func JoinBool added in v0.1.13

func JoinBool[E ~bool](es []E, sep string) string

func JoinFloat added in v0.1.13

func JoinFloat[E constraints.Float](es []E, sep string) string

func JoinInt added in v0.1.13

func JoinInt[E constraints.Signed](es []E, sep string) string

func JoinUint added in v0.1.13

func JoinUint[E constraints.Unsigned](es []E, sep string) string

func MapEntryName added in v0.1.1

func MapEntryName(s string) string

MapEntryName derives the name of the map entry message given the field name. See protoc v3.8.0: src/google/protobuf/descriptor.cc:254-276,6057

func Match

func Match(str string, pattern string) []string

func Max

func Max[S ~string](a, b S) S

func Min

func Min[S ~string](a, b S) S

func PutBuilder added in v0.1.12

func PutBuilder(buf *strings.Builder)

PutBuilder returns a strings.Builder to the pool.

func Remove

func Remove(s string, chars string) string

Remove takes a string candidate and a string of chars to remove from the candidate.

func ReplaceFunc

func ReplaceFunc(str string, f func(rune) string) string

func SimpleLetterEqualFold

func SimpleLetterEqualFold(s, t []byte) bool

SimpleLetterEqualFold is a specialization of bytes.EqualFold for use when s is all ASCII letters (no underscores, etc) and also doesn't contain 'k', 'K', 's', or 'S'. See comments on FoldFunc.

func TrimEnumPrefix added in v0.1.1

func TrimEnumPrefix(s, prefix string) string

TrimEnumPrefix trims the enum name prefix from an enum value name, where the prefix is all lowercase without underscores. See protoc v3.8.0: src/google/protobuf/descriptor.cc:330-375

Types

type Builder

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

func NewBuilder

func NewBuilder() *Builder

func NewBuilderBuilder

func NewBuilderBuilder(b *strings.Builder) *Builder

func (*Builder) Cap

func (b *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) Grow

func (b *Builder) Grow(n 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. If n is negative, Grow panics.

func (*Builder) Len

func (b *Builder) Len() int

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

func (*Builder) Reset

func (b *Builder) Reset()

Reset resets the Builder to be empty.

func (*Builder) String

func (b *Builder) String() string

String returns the accumulated string.

func (*Builder) Write

func (b *Builder) Write(p []byte) (int, error)

Write appends the contents of p to b's buffer. Write always returns len(p), nil.

func (*Builder) WriteBool

func (b *Builder) WriteBool(bl bool) error

WriteBool appends "true" or "false".

func (*Builder) WriteByte

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

WriteByte appends the byte c to b's buffer. The returned error is always nil.

func (*Builder) WriteFloat

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

WriteFloat appends the string form of the floating-point number f.

func (*Builder) WriteInt

func (b *Builder) WriteInt(i int64, base int) error

WriteInt appends the string form of the integer i.

func (*Builder) WriteQuote

func (b *Builder) WriteQuote(s string) error

WriteQuote appends a double-quoted Go string literal representing s.

func (*Builder) WriteQuoteRune

func (b *Builder) WriteQuoteRune(r rune) error

WriteQuoteRune appends a single-quoted Go character literal representing the rune.

func (*Builder) WriteQuoteRuneToASCII

func (b *Builder) WriteQuoteRuneToASCII(r rune) error

WriteQuoteRuneToASCII appends a single-quoted Go character literal representing the rune.

func (*Builder) WriteQuoteRuneToGraphic

func (b *Builder) WriteQuoteRuneToGraphic(r rune) error

WriteQuoteRuneToGraphic appends a single-quoted Go character literal representing the rune.

func (*Builder) WriteQuoteToASCII

func (b *Builder) WriteQuoteToASCII(s string) error

WriteQuoteToASCII appends a double-quoted Go string literal representing s.

func (*Builder) WriteQuoteToGraphic

func (b *Builder) WriteQuoteToGraphic(s string) error

WriteQuoteToGraphic appends a double-quoted Go string literal representing s.

func (*Builder) WriteRune

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

WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer. It returns the length of r and a nil error.

func (*Builder) WriteString

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

WriteString appends the contents of s to b's buffer. It returns the length of s and a nil error.

func (*Builder) WriteUint

func (b *Builder) WriteUint(i uint64, base int) error

WriteUint appends the string form of the unsigned integer i.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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