stringutil

package
v1.30.3 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base58Encode

func Base58Encode(input []byte) string

Base58Encode encodes a byte slice to base58 string. Uses the Bitcoin alphabet which excludes ambiguous characters (0, O, l, I).

func Base58EncodeSHA256

func Base58EncodeSHA256(input string) string

Base58EncodeSHA256 generates a SHA-256 hash of the input and encodes it as base58. This is useful for creating deterministic, URL-safe identifiers.

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration formats a duration into a human-readable string. It handles negative durations by prepending a "-" sign. Examples:

  • 500ms -> "500ms"
  • 1.5s -> "1.5s"
  • 2m30s -> "2m 30s"
  • 1h30m -> "1h 30m"
  • -500ms -> "-500ms"
  • -2m30s -> "-2m 30s"

func FormatTime

func FormatTime(t time.Time) string

FormatTime returns formatted time.

func IsJSON

func IsJSON(s string) bool

IsJSON checks if the given string is valid JSON. Returns true if the string can be parsed as JSON, false otherwise.

func IsMultiLine

func IsMultiLine(s string) bool

IsMultiLine checks if the given string contains multiple lines. Returns true if the string contains line separators (\n, \r\n, or \r).

func KebabToCamel

func KebabToCamel(s string) string

KebabToCamel converts a kebab-case string to camelCase.

func KeyValuesToMap

func KeyValuesToMap(kvSlice []string) map[string]string

KeyValuesToMap converts a slice of "KEY=VALUE" strings into a map of keys to values. It splits each entry at the first '='; entries without '=' are skipped. Values may be empty (for example, "KEY=" results in map["KEY"] = ""). Empty keys are allowed (for example, "=value" results in map[""] = "value").

func MatchPattern

func MatchPattern(ctx context.Context, content string, patterns []string, opts ...MatchOption) bool

MatchPattern matches content against patterns using either literal or regex matching. For files or large content, use MatchPatternScanner instead.

func MatchPatternScanner

func MatchPatternScanner(ctx context.Context, scanner *bufio.Scanner, patterns []string, opts ...MatchOption) bool

func ParseBool

func ParseBool(value any) (bool, error)

ParseBool parses a boolean value from the given input.

func ParseSeparatedValues

func ParseSeparatedValues(s string) ([]string, error)

ParseSeparatedValues intelligently parses a string into separate values based on detected separator patterns. Handles JSON arrays, various delimiters, quoted strings, and space-separated values.

func ParseTime

func ParseTime(val string) (time.Time, error)

ParseTime parses time string.

func RandomString

func RandomString(n int) string

RandomString generates a random string of length n using letters from letterBytes.

func RemoveQuotes

func RemoveQuotes(s string) string

RemoveQuotes removes leading and trailing double quotes from a string if present, and unescapes the content using strconv.Unquote.

func ScreamingSnakeToCamel added in v1.29.0

func ScreamingSnakeToCamel(s string) string

ScreamingSnakeToCamel converts a SCREAMING_SNAKE_CASE string to camelCase. Example: TOTAL_COUNT -> totalCount, MY_VAR -> myVar, FOO -> foo

func TruncString

func TruncString(val string, max int) string

TruncString returns truncated string.

Types

type KeyValue

type KeyValue string

func NewKeyValue

func NewKeyValue(key, value string) KeyValue

NewKeyValue constructs a KeyValue by concatenating key and value with an '=' separator. The key must not contain '=' characters to ensure round-trip correctness with Key() and Value(). An empty value produces a trailing '=' (e.g., "KEY=").

func (KeyValue) Bool

func (kv KeyValue) Bool() bool

func (KeyValue) Key

func (kv KeyValue) Key() string

func (KeyValue) MarshalJSON

func (kv KeyValue) MarshalJSON() ([]byte, error)

func (KeyValue) String

func (kv KeyValue) String() string

func (*KeyValue) UnmarshalJSON

func (kv *KeyValue) UnmarshalJSON(data []byte) error

func (KeyValue) Value

func (kv KeyValue) Value() string

type MatchOption

type MatchOption func(*matchOptions)

MatchOption represents an option for pattern matching

func WithExactMatch

func WithExactMatch() MatchOption

WithExactMatch configures the matcher to use exact string matching for literal patterns

func WithMaxBufferSize

func WithMaxBufferSize(size int) MatchOption

WithMaxBufferSize configures the maximum buffer size for handling long lines

type SeparatorType

type SeparatorType int

SeparatorType represents different types of separators for parsing text

const (
	SeparatorTypeJSON SeparatorType = iota
	SeparatorTypeNewline
	SeparatorTypeComma
	SeparatorTypeSemicolon
	SeparatorTypePipe
	SeparatorTypeTab
	SeparatorTypeQuoted
	SeparatorTypeSpace
)

func DetectSeparatorType

func DetectSeparatorType(s string) SeparatorType

DetectSeparatorType analyzes a string to determine the most likely separator type

Jump to

Keyboard shortcuts

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