Documentation
¶
Overview ¶
Package strings provides small string helpers and a curated set of aliases for the Go standard library strings package.
The intent of this package is to:
- Keep go-service code depending on go-service packages consistently, while still delegating to the standard library for core string operations.
- Provide a few convenience helpers that are frequently needed across the repository (for example emptiness checks and small splitting helpers).
- Expose carefully documented unsafe utilities (see Bytes) for performance- sensitive paths where avoiding allocations is important.
Most functions in this package are thin wrappers around the corresponding functions in the standard library strings package (for example Contains, HasPrefix, ReplaceAll, TrimSpace) or the strcase package (for example ToSnake, ToLowerCamel, ToDelimited). These wrappers do not change semantics.
Convenience helpers ¶
This package also provides helpers not present in the standard library:
IsEmpty / IsAnyEmpty: common emptiness checks. IsAnyEmpty returns false when called without values.
Join and Concat: Join takes the separator first and then variadic strings, so callers can avoid allocating a slice at the call site. With no strings it returns Empty. Concat concatenates without a separator.
CutColon: a small helper that splits on the first ":" using strings.Cut, returning the part before, the part after, and whether ":" was found. If ":" is not present, "before" is the original input and "after" is empty.
ToSnake / ToLowerCamel / ToDelimited: common string case conversion helpers delegated to strcase.
Constants ¶
Empty and Space are provided as named constants for readability and reuse.
Unsafe conversions ¶
Bytes converts a string to a byte slice without allocation by using unsafe.
Important: the returned []byte aliases the same memory as the input string.
Treat the returned slice as read-only. Writing to it results in undefined behavior.
Do not retain the returned slice beyond the lifetime of the original string value. In particular, do not store it in long-lived structures or return it when the string was derived from a transient buffer.
For safe conversions, use the built-in conversion []byte(s), which allocates a new byte slice.
Index ¶
- Constants
- func Bytes(s string) []byte
- func Concat(ss ...string) string
- func Contains(s, substr string) bool
- func Count(s, substr string) int
- func Cut(s, sep string) (string, string, bool)
- func CutAfter(s, sep string) string
- func CutColon(s string) (string, string, bool)
- func HasPrefix(s, prefix string) bool
- func IsAnyEmpty(ss ...string) bool
- func IsEmpty(s string) bool
- func Join(sep string, ss ...string) string
- func LastIndex(s, substr string) int
- func Repeat(s string, count int) string
- func ReplaceAll(s, o, n string) string
- func ToDelimited(s string, delimiter uint8) string
- func ToLower(s string) string
- func ToLowerCamel(s string) string
- func ToSnake(s string) string
- func Trim(s, cutset string) string
- func TrimSpace(s string) string
- type Reader
Constants ¶
const Empty = ""
Empty is the empty string constant.
It is provided as a named constant for readability and reuse.
const Space = " "
Space is the single ASCII space character (" ").
It is provided as a named constant for readability and reuse.
Variables ¶
This section is empty.
Functions ¶
func Bytes ¶
Bytes converts s to a []byte without allocating.
This function uses unsafe to create a byte slice view over the string's backing storage. As a result:
- The returned slice aliases the same memory as s.
- The returned slice must be treated as read-only. Writing to it results in undefined behavior.
- Do not retain the returned slice beyond the lifetime of s. In particular, do not store it in long-lived structures or return it when s was derived from a transient buffer.
For a safe conversion that does not alias the input string, use []byte(s), which allocates a new slice.
func Contains ¶
Contains reports whether substr is within s.
This is a thin wrapper around strings.Contains and does not change semantics.
func Count ¶ added in v2.194.0
Count counts the number of non-overlapping instances of substr in s.
This is a thin wrapper around strings.Count and does not change semantics.
func Cut ¶
Cut slices s around the first instance of sep, returning the text before and after sep.
The returned boolean reports whether sep was found.
This is a thin wrapper around strings.Cut and does not change semantics.
func CutAfter ¶ added in v2.643.0
CutAfter slices s around the first instance of sep and returns the text after sep.
If sep is not present, it returns Empty.
func CutColon ¶ added in v2.12.0
CutColon splits s on the first ":" and returns the parts before and after.
If ":" is not present, before is the original input and after is Empty. The returned boolean reports whether ":" was found.
This helper is commonly used by go-service "source string" conventions where a value is prefixed with a kind such as "env:NAME" or "file:/path".
func HasPrefix ¶
HasPrefix reports whether s begins with prefix.
This is a thin wrapper around strings.HasPrefix and does not change semantics.
func IsAnyEmpty ¶ added in v2.24.0
IsAnyEmpty reports whether any of ss are empty.
It returns true when at least one element of ss has length 0. With no values, it returns false.
func IsEmpty ¶
IsEmpty reports whether s is empty.
This is equivalent to len(s) == 0, but provided for readability at call sites.
func Join ¶
Join joins ss with sep, taking the separator before the values to join.
This is equivalent to strings.Join(ss, sep), but it accepts variadic input so callers can avoid allocating a slice at the call site when they already have discrete string values. With no values, it returns Empty.
func LastIndex ¶ added in v2.303.7
LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
This is a thin wrapper around strings.LastIndex and does not change semantics.
func Repeat ¶
Repeat returns a new string consisting of count copies of s.
This is a thin wrapper around strings.Repeat and does not change semantics.
func ReplaceAll ¶ added in v2.93.0
ReplaceAll returns a copy of s with all non-overlapping instances of o replaced by n.
This is a thin wrapper around strings.ReplaceAll and does not change semantics.
func ToDelimited ¶ added in v2.412.0
ToDelimited converts s to a delimiter-separated string.
This is a thin wrapper around strcase.ToDelimited and does not change semantics.
func ToLower ¶
ToLower returns s with all Unicode letters mapped to their lower case.
This is a thin wrapper around strings.ToLower and does not change semantics.
func ToLowerCamel ¶ added in v2.412.0
ToLowerCamel converts s to lower camel case.
This is a thin wrapper around strcase.ToLowerCamel and does not change semantics.
func ToSnake ¶ added in v2.412.0
ToSnake converts s to snake case.
This is a thin wrapper around strcase.ToSnake and does not change semantics.
func Trim ¶ added in v2.303.7
Trim returns a slice of s with all leading and trailing Unicode code points contained in cutset removed.
This is a thin wrapper around strings.Trim and does not change semantics.
func TrimSpace ¶
TrimSpace returns s with all leading and trailing white space removed.
White space is defined by Unicode.
This is a thin wrapper around strings.TrimSpace and does not change semantics.
Types ¶
type Reader ¶ added in v2.331.0
Reader is an alias of strings.Reader.
It is provided so go-service code can depend on a consistent import path while preserving standard library semantics.
func NewReader ¶ added in v2.331.0
NewReader returns a new Reader reading from s.
This is a thin wrapper around strings.NewReader and does not change semantics.