Documentation ¶
Overview ¶
Package StringExt provides a set of functions that extend the functionality of the built-in string type.
Index ¶
- func AdvancedFieldsSplitter(sentence string, indentLevel int) ([][]string, error)
- func FindContentIndexes(openingToken, closingToken string, contentTokens []string) (result [2]int, err error)
- func GenerateID(size int) (string, error)
- func ReplaceSuffix(str, suffix string) (string, error)
- func ToUTF8Runes(s string) ([]rune, error)
- type ErrInvalidUTF8Encoding
- type ErrLongerSuffix
- type ErrNeverOpened
- type ErrTokenNotFound
- type TokenType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdvancedFieldsSplitter ¶ added in v0.2.33
SplitSentenceIntoFields splits the string into fields, where each field is a substring separated by one or more whitespace charactue.
Parameters:
- sentence: The string to split into fields.
- indentLevel: The number of spaces that a tab character is replaced with.
Returns:
- [][]string: A two-dimensional slice of strings, where each inner slice represents the fields of a line from the input string.
- error: An error of type *ue.ErrInvalidRuneAt if an invalid rune is found in the sentence.
Behaviors:
- Negative indentLevel values are converted to positive values.
- Empty sentences return a nil slice with no errors.
- The function handles the following whitespace characters: space, tab, vertical tab, carriage return, line feed, and form feed.
- The function returns a partial result if an invalid rune is found where the result are the fields found up to that point.
func FindContentIndexes ¶
func FindContentIndexes(openingToken, closingToken string, contentTokens []string) (result [2]int, err error)
FindContentIndexes searches for the positions of opening and closing tokens in a slice of strings.
Parameters:
- openingToken: The string that marks the beginning of the content.
- closingToken: The string that marks the end of the content.
- contentTokens: The slice of strings in which to search for the tokens.
Returns:
- result: An array of two integers representing the start and end indexes of the content.
- err: Any error that occurred while searching for the tokens.
Errors:
- *ue.ErrInvalidParameter: If the openingToken or closingToken is an empty string.
- *ErrTokenNotFound: If the opening or closing token is not found in the content.
- *ErrNeverOpened: If the closing token is found without any corresponding opening token.
Behaviors:
- The first index of the content is inclusive, while the second index is exclusive.
- This function returns a partial result when errors occur. ([-1, -1] if errors occur before finding the opening token, [index, 0] if the opening token is found but the closing token is not found.
func GenerateID ¶ added in v0.2.14
GenerateID generates a random ID of the specified size (in bytes).
Parameters:
- size: The size of the ID to generate (in bytes).
Returns:
- string: The generated ID.
- error: An error if the ID cannot be generated.
Errors:
- *ue.ErrInvalidParameter: If the size is less than 1.
- Any error returned by the rand.Read function.
Behaviors:
- The function uses the crypto/rand package to generate a random ID of the specified size.
- The ID is returned as a hexadecimal string.
func ReplaceSuffix ¶
ReplaceSuffix replaces the end of the given string with the provided suffix.
Parameters:
- str: The original string.
- suffix: The suffix to replace the end of the string.
Returns:
- string: The resulting string after replacing the end with the suffix.
- error: An error of type *ErrLongerSuffix if the suffix is longer than the string.
Examples:
const ( str string = "hello world" suffix string = "Bob" ) result, err := ReplaceSuffix(str, suffix) if err != nil { fmt.Println(err) } else { fmt.Println(result) // Output: hello woBob }
func ToUTF8Runes ¶ added in v0.2.33
ToUTF8Runes converts a string to a slice of runes.
Parameters:
- s: The string to convert.
Returns:
- []rune: The slice of runes
- error: An error of type *ue,ErrAtIndex if the string contains invalid UTF-8 encoding.
Behaviors:
- An empty string returns a nil slice with no errors.
- The function stops at the first invalid UTF-8 encoding; returning an error and the runes found up to that point.
Types ¶
type ErrInvalidUTF8Encoding ¶ added in v0.2.33
type ErrInvalidUTF8Encoding struct{}
ErrInvalidUTF8Encoding is an error type for invalid UTF-8 encoding.
func NewErrInvalidUTF8Encoding ¶ added in v0.2.33
func NewErrInvalidUTF8Encoding() *ErrInvalidUTF8Encoding
NewErrInvalidUTF8Encoding creates a new ErrInvalidUTF8Encoding error.
Returns:
- *ErrInvalidUTF8Encoding: A pointer to the newly created error.
func (*ErrInvalidUTF8Encoding) Error ¶ added in v0.2.33
func (e *ErrInvalidUTF8Encoding) Error() string
Error is a method of the error interface that returns the error message.
Returns:
- string: The error message.
type ErrLongerSuffix ¶ added in v0.2.32
type ErrLongerSuffix struct { // Str is the string that is shorter than the suffix. Str string // Suffix is the Suffix that is longer than the string. Suffix string }
ErrLongerSuffix is a struct that represents an error when the suffix is longer than the string.
func NewErrLongerSuffix ¶ added in v0.2.32
func NewErrLongerSuffix(str, suffix string) *ErrLongerSuffix
NewErrLongerSuffix is a constructor of ErrLongerSuffix.
Parameters:
- str: The string that is shorter than the suffix.
- suffix: The suffix that is longer than the string.
Returns:
- *ErrLongerSuffix: A pointer to the newly created error.
func (*ErrLongerSuffix) Error ¶ added in v0.2.32
func (e *ErrLongerSuffix) Error() string
Error is a method of error interface that returns the error message.
Returns:
- string: The error message.
type ErrNeverOpened ¶
type ErrNeverOpened struct {
// OpeningToken and ClosingToken are the opening and closing tokens,
// respectively.
OpeningToken, ClosingToken string
}
ErrNeverOpened is a struct that represents an error when a closing token is found without a corresponding opening token.
func NewErrNeverOpened ¶ added in v0.2.25
func NewErrNeverOpened(openingToken, closingToken string) *ErrNeverOpened
NewErrNeverOpened is a constructor of ErrNeverOpened.
Parameters:
- openingToken: The opening token that was never closed.
- closingToken: The closing token that was found without a corresponding opening token.
Returns:
- *ErrNeverOpened: A pointer to the newly created error.
func (*ErrNeverOpened) Error ¶
func (e *ErrNeverOpened) Error() string
Error is a method of the error interface that returns the error message.
Returns:
- string: The error message.
type ErrTokenNotFound ¶ added in v0.2.32
type ErrTokenNotFound struct { // Token is the token that was not found in the content. Token string // Type is the type of the token (opening or closing). Type TokenType }
ErrTokenNotFound is a struct that represents an error when a token is not found in the content.
func NewErrTokenNotFound ¶ added in v0.2.32
func NewErrTokenNotFound(token string, tokenType TokenType) *ErrTokenNotFound
NewErrTokenNotFound is a constructor of ErrTokenNotFound.
Parameters:
- token: The token that was not found in the content.
- tokenType: The type of the token (opening or closing).
Returns:
- *ErrTokenNotFound: A pointer to the newly created error.
func (*ErrTokenNotFound) Error ¶ added in v0.2.32
func (e *ErrTokenNotFound) Error() string
Error is a method of the error interface that returns the error message.
Returns:
- string: The error message.