strings

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package strings provides high-performance, zero-copy string utilities with pooling for Nebula

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildLargeString

func BuildLargeString(fn func(*Builder)) string

BuildLargeString provides a way to build large strings

func BuildMediumString

func BuildMediumString(fn func(*Builder)) string

BuildMediumString provides a way to build medium-sized strings

func BuildString

func BuildString(fn func(*Builder)) string

BuildString provides a simple way to build strings with a function

func BuildWith

func BuildWith(size BuilderSize, fn func(*Builder)) string

BuildWith provides a functional approach to string building

func BytesToString

func BytesToString(b []byte) string

BytesToString converts byte slice to string without allocation WARNING: The returned string shares memory with the byte slice. Do not modify the byte slice after calling this function.

func Clone

func Clone(s string) string

Clone creates a copy of a string (useful when you need to own the memory)

func Compare

func Compare(a, b string) int

Compare compares two strings without allocation

func Concat

func Concat(strings ...string) string

Concat efficiently concatenates strings using pooled builder

func Contains

func Contains(s, substr string) bool

Contains checks if string contains substring without allocation

func HasPrefix

func HasPrefix(s, prefix string) bool

HasPrefix checks if string has prefix without allocation

func HasSuffix

func HasSuffix(s, suffix string) bool

HasSuffix checks if string has suffix without allocation

func Index

func Index(s, substr string) int

Index finds the index of substring in string without allocation

func Join

func Join(strings []string, delimiter string) string

Join joins strings using a delimiter with minimal allocations

func JoinPooled

func JoinPooled(strings []string, delimiter string) string

JoinPooled efficiently joins strings using pooled builder

func PutBuilder

func PutBuilder(builder *Builder, size BuilderSize)

PutBuilder returns a builder to the appropriate pool

func Split

func Split(s, delimiter string) []string

Split splits string by delimiter without allocating intermediate strings Returns a slice of string views into the original string

func Sprintf

func Sprintf(format string, args ...interface{}) string

Sprintf provides a pooled alternative to fmt.Sprintf

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes converts string to byte slice without allocation WARNING: The returned byte slice shares memory with the string. Do not modify the returned slice.

func TrimSpace

func TrimSpace(s string) string

TrimSpace removes leading and trailing whitespace

func ValueToString

func ValueToString(value interface{}) string

ValueToString efficiently converts interface{} values to strings This replaces fmt.Sprintf("%v", value) in hot paths like CSV processing

Types

type Builder

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

Builder provides efficient string building with zero-copy operations

func GetBuilder

func GetBuilder(size BuilderSize) *Builder

GetBuilder retrieves a pooled builder of the specified size

func NewBuilder

func NewBuilder(capacity int) *Builder

NewBuilder creates a new string builder

func (*Builder) Bytes

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

Bytes returns the underlying byte slice

func (*Builder) Cap

func (b *Builder) Cap() int

Cap returns the capacity of the underlying buffer

func (*Builder) Grow

func (b *Builder) Grow(n int)

Grow grows the buffer capacity

func (*Builder) Len

func (b *Builder) Len() int

Len returns the length of the built string

func (*Builder) Reset

func (b *Builder) Reset()

Reset resets the builder for reuse

func (*Builder) String

func (b *Builder) String() string

String returns the built string using zero-copy conversion

func (*Builder) Write

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

Write implements io.Writer interface

func (*Builder) WriteByte

func (b *Builder) WriteByte(c byte)

WriteByte appends a single byte

func (*Builder) WriteBytes

func (b *Builder) WriteBytes(data []byte)

WriteBytes appends bytes to the builder

func (*Builder) WriteString

func (b *Builder) WriteString(s string)

WriteString appends a string to the builder

type BuilderSize

type BuilderSize int

BuilderSize represents different builder sizes

const (
	Small  BuilderSize = iota // < 1KB
	Medium                    // 1KB - 16KB
	Large                     // 16KB+
)

type CSVBuilder

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

CSVBuilder provides optimized CSV string building

func NewCSVBuilder

func NewCSVBuilder(estimatedRows, estimatedCols int) *CSVBuilder

NewCSVBuilder creates a new CSV builder

func (*CSVBuilder) Close

func (cb *CSVBuilder) Close()

Close releases the builder back to the pool

func (*CSVBuilder) String

func (cb *CSVBuilder) String() string

String returns the built CSV string

func (*CSVBuilder) WriteHeader

func (cb *CSVBuilder) WriteHeader(headers []string)

WriteHeader writes CSV header

func (*CSVBuilder) WriteRow

func (cb *CSVBuilder) WriteRow(fields []string)

WriteRow writes a CSV row

type Intern

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

Intern provides string interning to reduce memory usage

func NewIntern

func NewIntern() *Intern

NewIntern creates a new string interner

func (*Intern) Clear

func (intern *Intern) Clear()

Clear removes all interned strings

func (*Intern) Get

func (intern *Intern) Get(s string) string

Get returns an interned version of the string

func (*Intern) Size

func (intern *Intern) Size() int

Size returns the number of interned strings

type Pool

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

Pool manages a pool of string builders

func NewPool

func NewPool(poolSize, builderCapacity int) *Pool

NewPool creates a new builder pool

func (*Pool) Get

func (p *Pool) Get() *Builder

Get retrieves a builder from the pool

func (*Pool) Put

func (p *Pool) Put(builder *Builder)

Put returns a builder to the pool

type SQLBuilder

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

SQLBuilder provides optimized SQL query building

func NewSQLBuilder

func NewSQLBuilder(estimatedLength int) *SQLBuilder

NewSQLBuilder creates a new SQL builder

func (*SQLBuilder) Close

func (sb *SQLBuilder) Close()

Close releases the builder back to the pool

func (*SQLBuilder) String

func (sb *SQLBuilder) String() string

String returns the built SQL query

func (*SQLBuilder) WriteIdentifier

func (sb *SQLBuilder) WriteIdentifier(name string) *SQLBuilder

WriteIdentifier writes a quoted identifier

func (*SQLBuilder) WriteInt

func (sb *SQLBuilder) WriteInt(value int64) *SQLBuilder

WriteInt writes an integer value

func (*SQLBuilder) WriteQuery

func (sb *SQLBuilder) WriteQuery(query string) *SQLBuilder

WriteQuery writes a SQL query part

func (*SQLBuilder) WriteSpace

func (sb *SQLBuilder) WriteSpace() *SQLBuilder

WriteSpace adds a space

func (*SQLBuilder) WriteStringLiteral

func (sb *SQLBuilder) WriteStringLiteral(value string) *SQLBuilder

WriteStringLiteral writes a quoted string literal

type URLBuilder

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

URLBuilder provides optimized URL building

func NewFormBuilder

func NewFormBuilder() *URLBuilder

NewFormBuilder creates a new form builder (for application/x-www-form-urlencoded) This is essentially a URLBuilder without a base URL

func NewURLBuilder

func NewURLBuilder(baseURL string) *URLBuilder

NewURLBuilder creates a new URL builder

func (*URLBuilder) AddParam

func (ub *URLBuilder) AddParam(key, value string) *URLBuilder

AddParam adds a URL parameter (with proper encoding)

func (*URLBuilder) AddParamBool

func (ub *URLBuilder) AddParamBool(key string, value bool) *URLBuilder

AddParamBool adds a boolean parameter

func (*URLBuilder) AddParamInt

func (ub *URLBuilder) AddParamInt(key string, value int) *URLBuilder

AddParamInt adds an integer parameter

func (*URLBuilder) AddParams

func (ub *URLBuilder) AddParams(params map[string]string) *URLBuilder

AddParams adds multiple parameters

func (*URLBuilder) AddPath

func (ub *URLBuilder) AddPath(segments ...string) *URLBuilder

AddPath adds path segments to the URL

func (*URLBuilder) Close

func (ub *URLBuilder) Close()

Close releases the builder back to the pool

func (*URLBuilder) Query

func (ub *URLBuilder) Query() string

Query returns just the query string (without the base URL) This is useful for form-encoded POST bodies

func (*URLBuilder) String

func (ub *URLBuilder) String() string

String returns the built URL

Jump to

Keyboard shortcuts

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