strings

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 6 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// BASE_10 is the base 10
	BASE_10 int = 10
)
View Source
const (
	// MAX_STRING_SIZE is the maximum size that a string can have.
	// However, this is not a strict limit but a suggestion.
	MAX_STRING_SIZE uint64 = 0xFFFFFFFFFFFFFFFF
)

Variables

This section is empty.

Functions

func And added in v0.1.5

func And(values []string) string

And formats a slice of strings as a string using the "and" conjunction according to the rules of the English language. The provided values should be unique and not empty.

Parameters:

  • values: The slice of strings to format.

Returns:

  • string: The formatted string.

func Concat added in v0.1.5

func Concat(strs ...string) string

Concat is a convenience function that does the same as `Join(strs, "")`. Empty strings are ignored.

Parameters:

  • strs: The slice of strings to join.

Returns:

  • string: The joined string.

func Count added in v0.1.5

func Count(str, substr string) (uint32, bool)

Count returns the number of times a substring occurs in a string. If the substring is empty, it returns the length of the string.

Parameters:

  • str: The string to count the substring in.
  • substr: The substring to count.

Returns:

  • uint32: The number of times the substring occurs in the string. It is never greater than common.MAX_SLICE_SIZE.
  • bool: `true` if the number of times the substring occurs in the string is less than or equal to common.MAX_SLICE_SIZE, `false` otherwise.

func EitherOr added in v0.1.5

func EitherOr(values []string) string

EitherOr formats a slice of strings as a string using the "either/or" conjunction according to the rules of the English language. The provided values should be unique and not empty.

Parameters:

  • values: The slice of strings to format.

Returns:

  • string: The formatted string.

func Fields added in v0.1.5

func Fields(dst *[]string, str string) (uint32, error)

Fields splits the string s around each instance of one or more consecutive white space characters, returning an array of substrings of s or an empty slice if s contains only white space. The result is stored back into the provided memory location without any side effects.

Parameters:

  • dst: The memory location to store the result.
  • str: The string to split.

Returns:

  • uint32: The number of substrings in the result.
  • error: An error if splitting the string fails.

Errors:

  • errors.ErrBadParam: If dst is nil.
  • errors.ErrSizeExceeded: If the result has been truncated due to the number of substrings being too many.

func FormatFloat added in v0.1.9

func FormatFloat[E ~float32 | float64](num E) string

FormatFloat returns the string representation of the float in base 10.

Parameters:

  • num: The float to format.

Returns:

  • string: The string representation of the float in base 10.

func FormatInt added in v0.1.9

func FormatInt[E ~int | int8 | int16 | int32 | int64](num E) string

FormatInt returns the string representation of the int in base 10.

Parameters:

  • num: The int to format.

Returns:

  • string: The string representation of the int in base 10.

func FormatUint added in v0.1.5

func FormatUint[E ~uint | uint8 | uint16 | uint32 | uint64](num E) string

FormatUint returns the string representation of the uint in base 10.

Parameters:

  • num: The uint to format.

Returns:

  • string: The string representation of the uint in base 10.

func IsLonger added in v0.1.5

func IsLonger(s string) bool

IsLonger reports whether the string is longer than MAX_STRING_SIZE.

Parameters:

  • s: The string to check.

Returns:

  • bool: `true` if the string is too long, `false` otherwise.

func IsShorter added in v0.1.5

func IsShorter(str string) bool

IsShorter reports whether the length of the string is shorter than MAX_STRING_SIZE.

Parameters:

  • str: the string to check.

Returns:

  • bool: `true` if the length of the string is valid, `false` otherwise.

func Join added in v0.1.5

func Join(strs []string, sep string) string

Join joins a slice of strings with a separator.

Parameters:

  • strs: The slice of strings to join.
  • sep: The separator.

Returns:

  • string: The joined string.

func Len

func Len(str string) (uint64, bool)

Len returns the length of the string, reporting whether it is shorter than or equal to MAX_STRING_SIZE.

Parameters:

  • str: The string to get the length of.

Returns:

  • uint64: The length of the string.
  • bool: `true` if the string is shorter than or equal to MAX_STRING_SIZE, `false` otherwise.

func Or added in v0.1.5

func Or(values []string) string

Or formats a slice of strings as a string using the "or" conjunction according to the rules of the English language. The provided values should be unique and not empty.

Parameters:

  • values: The slice of strings to format.

Returns:

  • string: The formatted string.

func Ordinal added in v0.1.5

func Ordinal(num uint64) string

Ordinal returns the ordinal suffix of the number.

Parameters:

  • num: The number to get the ordinal suffix of.

Returns:

  • string: The ordinal suffix of the number.

func Quote added in v0.1.3

func Quote(str string) string

Quote quotes a string.

Parameters:

  • str: The string to quote.

Returns:

  • string: The quoted string.

func QuoteIf added in v0.1.5

func QuoteIf(str string) string

QuoteIf quotes a string if it is not empty.

Parameters:

  • str: The string to quote.

Returns:

  • string: The quoted string.

func RejectEmpty added in v0.1.5

func RejectEmpty[S ~[]string](s *S) (uint32, error)

RejectEmpty returns a slice with the strings that are not empty. These strings are defined to be "valid".

The result of RejectEmpty is stored back in the provided variable without any side effect and without modifying the original slice.

Parameters:

  • s: The slice to reject the empty strings from. The result is stored back in this variable.

Returns:

  • uint32: The number of valid strings.
  • error: An error if rejecting the strings fails.

Errors:

  • errors.ErrBadParam: If s is nil.
  • errors.ErrSizeExceeded: If the number of valid strings is greater than [MaxSliceSize].

Example:

s := []string{"", "", ""}

n, err := RejectEmpty(&s)
if err != nil {
	panic(err)
}

// Do something with `result`

func Repeat added in v0.1.5

func Repeat(str string, count uint64) string

Repeat returns a string that is repeated count times.

Parameters:

  • str: The string to repeat.
  • count: The number of times to repeat the string.

Returns:

  • string: The repeated string.

func ReplacePrefix added in v0.1.5

func ReplacePrefix(str, old, new string) (string, bool)

ReplacePrefix replaces the prefix of a string with another string, if the prefix exists. If `old` is empty, the prefix is added.

Parameters:

  • str: The string to replace the prefix of.
  • old: The prefix to replace.
  • new: The string to replace the prefix with.

Returns:

  • string: The string with the prefix replaced.
  • bool: `true` if the prefix was replaced, `false` otherwise.

func ReplaceSuffix added in v0.1.5

func ReplaceSuffix(str, old, new string) (string, bool)

ReplaceSuffix replaces the suffix of a string with another string, if the suffix exists. If `old` is empty, the suffix is added.

Parameters:

  • str: The string to replace the suffix of.
  • old: The suffix to replace.
  • new: The string to replace the suffix with.

Returns:

  • string: The string with the suffix replaced.
  • bool: `true` if the suffix was replaced, `false` otherwise.

func Rune added in v0.1.5

func Rune(str string) iter.Seq[rune]

Rune returns an iterator that yields the runes in a string.

The iterator is not single-use only.

Parameters:

  • str: The string to yield the runes from.

Returns:

  • iter.Seq[rune]: An iterator that yields the runes in a string. It is never nil.

func Split

func Split(dst *[]string, str, sep string) (uint32, error)

Split splits a string into a list of substrings, storing the result back into the provided memory location without any side-effects.

Parameters:

  • dst: The memory location to store the result.
  • str: The string to split.
  • sep: The separator to use.

Returns:

  • uint32: The number of substrings in the result.
  • error: An error if splitting the string fails.

Errors:

  • errors.ErrBadParam: If dst is nil.
  • errors.ErrSizeExceeded: If the result has been truncated due to the number of substrings being too many.

func SplitN added in v0.1.5

func SplitN(dst *[]string, str *string, sep string, n uint32) (uint32, error)

SplitN splits a string into a slice of strings using a separator and a maximum number of splits. The remaining string is stored in the provided variable without any side effect. Likewise, the result is stored back into the provided memory location without any side effects.

If the maximum number of splits is greater than common.MAX_SLICE_SIZE, it is truncated.

Parameters:

  • dst: The memory location to store the result.
  • str: The string to split. The remaining string is stored back in this variable.
  • sep: The separator to use.
  • n: The maximum number of splits.

Returns:

  • uint32: The number of splits performed.
  • error: An error if splitting the string fails.

Errors:

  • errors.ErrBadParam: If dst or str are nil.

func Trim added in v0.1.5

func Trim(str, sep string) string

Trim trims leading and trailing separator characters from the string.

Parameters:

  • str: The string to trim.
  • sep: The separator.

Returns:

  • string: The trimmed string.

func TrimSpace added in v0.1.5

func TrimSpace(str string) string

TrimSpace trims leading and trailing white space from a string.

Parameters:

  • str: The string to trim.

Returns:

  • string: The trimmed string.

func TrimSuffix added in v0.1.5

func TrimSuffix(str, suffix string) string

TrimSuffix trims the suffix from a string.

Parameters:

  • str: The string to trim.
  • suffix: The suffix to trim.

Returns:

  • string: The trimmed string.

Types

type ErrBadFormat added in v0.1.5

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

ErrBadFormat is the error type for when formats are invalid during parsing, especially from strings to enums.

func NewErrBadFormat added in v0.1.5

func NewErrBadFormat(msg string) *ErrBadFormat

NewErrBadFormat returns a new ErrBadFormat with the given message.

Parameters:

  • msg: The message of the error.

Returns:

  • *ErrBadFormat: The newly created error. It is never `nil`.

Format:

"bad format: <msg>"

Where, <msg> is the message of the error. Defaults to errors.DEFAULT_ERR_MSG if empty.

func (ErrBadFormat) Error added in v0.1.5

func (e ErrBadFormat) Error() string

Error implements error.

Jump to

Keyboard shortcuts

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