StringExt

package
v0.2.33 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package StringExt provides a set of functions that extend the functionality of the built-in string type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdvancedFieldsSplitter added in v0.2.33

func AdvancedFieldsSplitter(sentence string, indentLevel int) ([][]string, error)

SplitSentenceIntoFields splits the string into fields, where each field is a substring separated by one or more whitespace characters. The function also handles special characters such as tabs, vertical tabs, carriage returns, line feeds, and form feeds.

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 *ers.ErrInvalidRuneAt if an invalid rune is found in the sentence.

Notes:

  • Negative indentLevel values are converted to positive values.
  • Empty sentences return an empty slice with no errors.

func ByteSplitter added in v0.2.28

func ByteSplitter(data []byte, sep byte) [][]byte

ByteSplitter splits a byte slice into multiple slices based on a separator byte. The separator byte is not included in the resulting slices.

If the input slice is empty, the function returns nil.

Parameters:

  • data: The byte slice to split.
  • sep: The separator byte.

Returns:

  • [][]byte: A slice of byte slices.

func FindContentIndexes

func FindContentIndexes(openingToken, closingToken string, contentTokens []string) (int, int, 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:

  • int: The start index of the content (inclusive).
  • int: The end index of the content (exclusive).
  • error: Any error that occurred while searching for the tokens.

Errors:

  • *ers.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.

func GenerateID added in v0.2.14

func GenerateID(size int) (string, error)

GenerateID generates a random ID of the specified size (in bytes).

The function uses the crypto/rand package to generate a random ID of the specified size.

Errors:

  • *ers.ErrInvalidParameter: If the size is less than 1.
  • Any error returned by the rand.Read function.

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.

func JoinBytes added in v0.2.28

func JoinBytes(slices [][]byte, sep byte) string

JoinBytes joins multiple byte slices into a single string using a separator byte.

If the input slice is empty, the function returns an empty string.

Parameters:

  • slices: A slice of byte slices to join.
  • sep: The separator byte.

Returns:

  • string: The joined string.

func ReplaceSuffix

func ReplaceSuffix(str, suffix string) (string, error)

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

func ToUTF8Runes(s string) ([]rune, error)

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 *ErrAtIndex if the string contains invalid UTF-8 encoding.

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.

type TokenType added in v0.2.32

type TokenType int8

TokenType is an enum that represents the type of token in a string.

const (
	// OpToken represents an opening token.
	OpToken TokenType = iota

	// ClToken represents a closing token.
	ClToken
)

func (TokenType) String added in v0.2.32

func (t TokenType) String() string

String is a method of fmt.Stringer interface that returns the string representation of the token type.

Returns:

  • string: the string representation of the token type.

Jump to

Keyboard shortcuts

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