Documentation
¶
Overview ¶
Package stringx provides extended string functions for Go with Unicode support.
This package extends the standard library's strings package with additional functionality for common string operations.
Index ¶
- Constants
- Variables
- func CamelCase(s string) string
- func FromBytes(bytes []byte) string
- func KebabCase(s string) string
- func Len(s string) int
- func PadBoth(s string, length int, padding string) string
- func PadLeft(s string, length int, padding string) string
- func PadRight(s string, length int, padding string) string
- func PascalCase(s string) string
- func Password(length int, opt ...PasswordOption) string
- func Random(length int) string
- func Reverse(s string) string
- func SnakeCase(s string) string
- func ToBytes(s string) []byte
- type PasswordCharKind
- type PasswordFactory
- type PasswordIncludes
- type PasswordOption
- type PasswordOptions
- type PasswordSource
- type RandomFactory
- type RandomOption
- type RandomOptions
Examples ¶
Constants ¶
const ( // DefaultPasswordLetters is the default set of letters used for generating passwords. DefaultPasswordLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // DefaultPasswordNumbers is the default set of numbers used for generating passwords. DefaultPasswordNumbers = "0123456789" // DefaultPasswordSymbols is the default set of symbols used for generating passwords. DefaultPasswordSymbols = "~!@#$%^&*()-_=+[]{}|;:,.<>?/" // DefaultPasswordSpaces is the default set of spaces used for generating passwords. DefaultPasswordSpaces = " " )
const DefaultRandomAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
DefaultRandomAlphabet is the default alphabet used for generating random strings.
Variables ¶
var DefaultRandomFactory = NewRandomFactory()
DefaultRandomFactory is the default RandomFactory used for generating random strings.
Functions ¶
func CamelCase ¶
CamelCase converts a string to camel case.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.CamelCase("hello_world"))
fmt.Println(stringx.CamelCase("hello-world"))
fmt.Println(stringx.CamelCase("hello world"))
fmt.Println(stringx.CamelCase("HelloWorld"))
}
Output: helloWorld helloWorld helloWorld helloWorld
func FromBytes ¶
FromBytes converts a byte slice to a string without copying the data. This is more efficient than using string(bytes) because it avoids unnecessary memory allocation. However, be cautious when using this function, as any modification to the bytes will also modify the resulting string, since they share the same underlying data. Use this function in appropriate scenarios where you can guarantee that the byte slice will not be modified after conversion.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
bytes := []byte("hello world")
s := stringx.FromBytes(bytes)
fmt.Println(s)
}
Output: hello world
func KebabCase ¶
KebabCase converts a string to kebab case.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.KebabCase("helloWorld"))
fmt.Println(stringx.KebabCase("HelloWorld"))
fmt.Println(stringx.KebabCase("hello_world"))
fmt.Println(stringx.KebabCase("HTTPServer"))
}
Output: hello-world hello-world hello-world http-server
func Len ¶
Len returns the actual character length of the string, not the byte length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.Len("hello"))
fmt.Println(stringx.Len("你好"))
fmt.Println(stringx.Len("🎉🎊"))
}
Output: 5 2 2
func PadBoth ¶
PadBoth pads the string on both sides with the specified padding string until it reaches the desired length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.PadBoth("go", 6, "x"))
fmt.Println(stringx.PadBoth("hi", 8, "ab"))
fmt.Println(stringx.PadBoth("test", 10, "="))
}
Output: xxgoxx abahiaba ===test===
func PadLeft ¶
PadLeft pads the string on the left with the specified padding string until it reaches the desired length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.PadLeft("go", 5, "x"))
fmt.Println(stringx.PadLeft("hello", 10, "0"))
fmt.Println(stringx.PadLeft("test", 8, "ab"))
}
Output: xxxgo 00000hello ababtest
func PadRight ¶
PadRight pads the string on the right with the specified padding string until it reaches the desired length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.PadRight("go", 5, "x"))
fmt.Println(stringx.PadRight("hello", 10, "0"))
fmt.Println(stringx.PadRight("test", 8, "ab"))
}
Output: goxxx hello00000 testabab
func PascalCase ¶
PascalCase converts a string to pascal case.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.PascalCase("hello_world"))
fmt.Println(stringx.PascalCase("hello-world"))
fmt.Println(stringx.PascalCase("hello world"))
fmt.Println(stringx.PascalCase("helloWorld"))
}
Output: HelloWorld HelloWorld HelloWorld HelloWorld
func Password ¶ added in v1.1.0
func Password(length int, opt ...PasswordOption) string
Password generates a password of the specified length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
// Generate a secure password of length 16 using the default character sets
// (letters, numbers and symbols).
pwd := stringx.Password(16)
fmt.Printf("Password length: %d\n", len(pwd))
}
Output: Password length: 16
Example (Includes) ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
// Restrict the password to letters and numbers only.
pwd := stringx.Password(12, stringx.PasswordIncludes{
stringx.PasswordLetter,
stringx.PasswordNumber,
})
fmt.Printf("Password length: %d\n", len(pwd))
}
Output: Password length: 12
func Random ¶
Random generates a random string of the specified length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
// Generate a random string of length 10
s := stringx.Random(10)
fmt.Printf("Random string length: %d\n", len(s))
}
Output: Random string length: 10
func Reverse ¶
Reverse reverses the string.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.Reverse("hello"))
fmt.Println(stringx.Reverse("你好世界"))
fmt.Println(stringx.Reverse("abc123"))
}
Output: olleh 界世好你 321cba
func SnakeCase ¶
SnakeCase converts a string to snake case.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.SnakeCase("helloWorld"))
fmt.Println(stringx.SnakeCase("HelloWorld"))
fmt.Println(stringx.SnakeCase("hello-world"))
fmt.Println(stringx.SnakeCase("HTTPServer"))
}
Output: hello_world hello_world hello_world http_server
func ToBytes ¶
ToBytes converts a string to a byte slice without copying the data. This is more efficient than using []byte(s) because it avoids unnecessary memory allocation. However, be cautious when using this function, as any modification to the resulting byte slice will cause a panic, since strings are immutable in Go. Use this function in appropriate scenarios where you can guarantee that the resulting byte slice will not be modified after conversion.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
s := "hello world"
bytes := stringx.ToBytes(s)
fmt.Println(string(bytes))
}
Output: hello world
Types ¶
type PasswordCharKind ¶ added in v1.1.0
type PasswordCharKind int
PasswordCharKind represents the kind of characters to include in the password.
const ( PasswordLetter PasswordCharKind PasswordNumber PasswordSymbol PasswordSpace )
type PasswordFactory ¶ added in v1.1.0
type PasswordFactory interface {
// MakePassword generates a password of the specified length.
MakePassword(length int, opt ...PasswordOption) string
}
PasswordFactory is an interface for generating passwords.
var DefaultPasswordFactory PasswordFactory = NewSecuredPasswordFactory()
DefaultPasswordFactory is the default PasswordFactory used for generating passwords. It uses crypto/rand for secure random number generation, so the top-level Password function is safe for generating real passwords by default.
func NewPasswordFactory ¶ added in v1.1.0
func NewPasswordFactory() PasswordFactory
NewPasswordFactory creates a new PasswordFactory.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
// A non-cryptographic factory is faster and suitable for non-sensitive
// scenarios such as test fixtures.
factory := stringx.NewPasswordFactory()
pwd := factory.MakePassword(20)
fmt.Printf("Password length: %d\n", len(pwd))
}
Output: Password length: 20
func NewPasswordFactoryWithSource ¶ added in v1.1.0
func NewPasswordFactoryWithSource(src PasswordSource) PasswordFactory
NewPasswordFactoryWithSource creates a new PasswordFactory with the given PasswordSource.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
// Provide a custom character source to fully control the alphabet.
src := stringx.PasswordSource{
stringx.PasswordLetter: []byte("abcdef"),
stringx.PasswordNumber: []byte("0123"),
}
factory := stringx.NewPasswordFactoryWithSource(src)
pwd := factory.MakePassword(16, stringx.PasswordIncludes{
stringx.PasswordLetter,
stringx.PasswordNumber,
})
fmt.Printf("Password length: %d\n", len(pwd))
}
Output: Password length: 16
func NewSecuredPasswordFactory ¶ added in v1.1.0
func NewSecuredPasswordFactory() PasswordFactory
NewSecuredPasswordFactory creates a new PasswordFactory that uses crypto/rand for secure random number generation.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
// A cryptographically secure factory backed by crypto/rand.
factory := stringx.NewSecuredPasswordFactory()
pwd := factory.MakePassword(20)
fmt.Printf("Password length: %d\n", len(pwd))
}
Output: Password length: 20
func NewSecuredPasswordFactoryWithSource ¶ added in v1.1.0
func NewSecuredPasswordFactoryWithSource(src PasswordSource) PasswordFactory
NewSecuredPasswordFactoryWithSource creates a new PasswordFactory that uses crypto/rand for secure random number generation with the given PasswordSource.
type PasswordIncludes ¶ added in v1.1.0
type PasswordIncludes []PasswordCharKind
PasswordIncludes is a PasswordOption that specifies which character kinds to include in the password.
func (PasswordIncludes) ApplyPasswordOptions ¶ added in v1.1.0
func (o PasswordIncludes) ApplyPasswordOptions(opts *PasswordOptions)
ApplyPasswordOptions applies the PasswordIncludes option to the given PasswordOptions.
type PasswordOption ¶ added in v1.1.0
type PasswordOption interface {
// ApplyPasswordOptions applies the option to the given PasswordOptions.
ApplyPasswordOptions(opts *PasswordOptions)
}
PasswordOption is an interface for applying password options.
type PasswordOptions ¶ added in v1.1.0
type PasswordOptions struct {
// Includes specifies which character kinds to include in the password.
Includes PasswordIncludes
}
PasswordOptions contains options for generating passwords.
func (*PasswordOptions) Join ¶ added in v1.1.0
func (o *PasswordOptions) Join(opts ...PasswordOption)
Join applies the given PasswordOption to the PasswordOptions.
type PasswordSource ¶ added in v1.1.0
type PasswordSource map[PasswordCharKind][]byte
PasswordSource is a map of character kinds to their corresponding byte slices.
type RandomFactory ¶
type RandomFactory interface {
// MakeRandomString generates a random string of the specified length.
MakeRandomString(length int) string
}
RandomFactory is an interface for generating random strings.
func NewRandomFactory ¶
func NewRandomFactory(opt ...RandomOption) RandomFactory
NewRandomFactory creates a new RandomFactory.
type RandomOption ¶
type RandomOption func(opts *RandomOptions)
RandomOption is a function that modifies RandomOptions.
func RandomAlphabet ¶
func RandomAlphabet(alphabet string) RandomOption
RandomAlphabet sets the alphabet for generating random strings.
type RandomOptions ¶
type RandomOptions struct {
// Alphabet is the set of characters to use for generating random strings.
Alphabet []rune
}
RandomOptions contains options for generating random strings.