Documentation ¶
Index ¶
- func AnyToLines[I com.Runer, O any](elem I, f func(rune) (O, error)) ([][]O, error)
- func CalculateNumberOfLines(text []string, width int) (int, error)
- func Fields(str string) []string
- func FindContentIndexes(openingToken, closingToken string, contentTokens []string) (result [2]int, err error)
- func FitString(s string, width int) (string, error)
- func GenerateID(size int) (string, error)
- func ReplaceSuffix(str, suffix string) (string, bool)
- func StringToLines(str string) []string
- func ToUTF8Runes(s string) ([]rune, error)
- type ErrInvalidUTF8Encoding
- type ErrLinesGreaterThanWords
- type ErrLongerSuffix
- type ErrNeverOpened
- type ErrNoCandidateFound
- type ErrTokenNotFound
- type TextSplit
- func (ts *TextSplit) Copy() intf.Copier
- func (ts *TextSplit) GetFirstLine() []string
- func (ts *TextSplit) GetFurthestRightEdge() (int, bool)
- func (ts *TextSplit) GetHeight() int
- func (ts *TextSplit) GetLines() []string
- func (ts *TextSplit) GetRunes() [][]rune
- func (ts *TextSplit) InsertWord(word string) bool
- func (ts *TextSplit) InsertWords(words []string) int
- type TokenType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnyToLines ¶ added in v0.3.2
StringToLines splits a string into lines.
Parameters:
- elem: The element to split into lines.
- f: The function to execute on each rune to convert it to the desired type.
Returns:
- [][]O: The lines of the elements.
- error: An error if it occurs during the conversion.
func CalculateNumberOfLines ¶
CalculateNumberOfLines is a function that calculates the minimum number of lines needed to fit a given text within a specified width.
Errors:
- *ue.ErrInvalidParameter: If the width is less than or equal to 0.
- *ErrLinesGreaterThanWords: If the calculated number of lines is greater than the number of words in the text.
Parameters:
- text: The slice of strings representing the text to calculate the number of lines for.
- width: The width to fit the text within.
Returns:
- int: The calculated number of lines needed to fit the text within the width.
- error: An error if it occurs during the calculation.
The function calculates the total length of the text (Tl) and uses a mathematical formula to estimate the minimum number of lines needed to fit the text within the given width. The formula is explained in detail in the comments within the function.
It also returns the calculated number of lines when it errors out
func Fields ¶ added in v0.2.43
Fields splits a string into fields.
Returns:
- []*String: The fields of the string.
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 FitString ¶ added in v0.2.41
FitString fits a string to the specified width by adding spaces to the end of the string until the width is reached.
Parameters:
- width: The width to fit the string to.
Returns:
- string: The string with spaces added to the end to fit the width.
- error: An error if the width is less than 0.
Behaviors:
- If the width is less than the length of the string, the string is truncated to fit the width.
- If the width is greater than the length of the string, spaces are added to the end of the string until the width is reached.
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.
- bool: A boolean indicating if the operation was successful. (i.e., if the suffix is shorter than the string).
Behaviors:
- For quick error, use the *ErrLongerSuffix error type of this package.
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 StringToLines ¶ added in v0.3.2
StringToLines splits a string into lines.
Parameters:
- str: The string to split into lines.
Returns:
- []string: The lines of the string.
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 *ErrInvalidUTF8Encoding 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 ErrLinesGreaterThanWords ¶ added in v0.2.43
type ErrLinesGreaterThanWords struct { // NumberOfLines is the number of lines in the text. NumberOfLines int // NumberOfWords is the number of words in the text. NumberOfWords int }
ErrLinesGreaterThanWords is an error type that is returned when the number of lines in a text is greater than the number of words.
func NewErrLinesGreaterThanWords ¶ added in v0.2.43
func NewErrLinesGreaterThanWords(numberOfLines, numberOfWords int) *ErrLinesGreaterThanWords
NewErrLinesGreaterThanWords is a constructor of ErrLinesGreaterThanWords.
Parameters:
- numberOfLines: The number of lines in the text.
- numberOfWords: The number of words in the text.
Returns:
- *ErrLinesGreaterThanWords: A pointer to the newly created error.
func (*ErrLinesGreaterThanWords) Error ¶ added in v0.2.43
func (e *ErrLinesGreaterThanWords) 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 ErrNoCandidateFound ¶ added in v0.2.43
type ErrNoCandidateFound struct{}
ErrNoCandidateFound is an error type that is returned when no candidate is found.
func NewErrNoCandidateFound ¶ added in v0.2.43
func NewErrNoCandidateFound() *ErrNoCandidateFound
NewErrNoCandidateFound is a constructor of ErrNoCandidateFound.
Returns:
- *ErrNoCandidateFound: A pointer to the newly created error.
func (*ErrNoCandidateFound) Error ¶ added in v0.2.43
func (e *ErrNoCandidateFound) 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 TextSplit ¶ added in v0.2.43
type TextSplit struct {
// contains filtered or unexported fields
}
TextSplit represents a split text with a maximum width and height.
func NewTextSplit ¶ added in v0.2.43
NewTextSplit creates a new TextSplit with the given maximum width and height.
Parameters:
- maxWidth: The maximum length of a line.
- maxHeight: The maximum number of lines.
Returns:
- *TextSplit: A pointer to the newly created TextSplit.
- error: An error of type *ers.ErrInvalidParameter if the maxWidth or maxHeight is less than 0.
func SplitInEqualSizedLines ¶ added in v0.2.43
SplitInEqualSizedLines is a function that splits a given text into lines of equal width.
Errors:
- *ue.ErrInvalidParameter: If the input text is empty or the width is less than or equal to 0.
- *ErrLinesGreaterThanWords: If the number of lines needed to fit the text within the width is greater than the number of words in the text.
- *ErrNoCandidateFound: If no candidate is found during the optimization process.
Parameters:
- text: The slice of strings representing the text to split.
Returns:
- *TextSplit: A pointer to the created TextSplit instance.
- error: An error of type *ErrEmptyText if the input text is empty, or an error of type *ErrWidthTooSmall if the width is less than or equal to 0.
The function calculates the minimum number of lines needed to fit the text within the width using the CalculateNumberOfLines function. Furthermore, it uses the Sum of Squared Mean (SQM) to find the optimal solution for splitting the text into lines of equal width.
If maxHeight is not provided, the function calculates the number of lines needed to fit the text within the width using the CalculateNumberOfLines function.
func (*TextSplit) Copy ¶ added in v0.2.43
Copy is a method of intf.Copier that creates a shallow copy of the TextSplit.
Returns:
- intf.Copier: A shallow copy of the TextSplit.
func (*TextSplit) GetFirstLine ¶ added in v0.2.43
GetFirstLine is a method that returns the first line of the TextSplit.
Returns:
- []string: The first line of the TextSplit, or nil if the TextSplit is empty.
Behaviors:
- If the TextSplit is empty, the method returns nil.
func (*TextSplit) GetFurthestRightEdge ¶ added in v0.2.43
GetFurthestRightEdge is a method that returns the number of characters in the longest line of the TextSplit.
Returns:
- int: The number of characters in the longest line.
- bool: True if the TextSplit is not empty, and false otherwise.
func (*TextSplit) GetHeight ¶ added in v0.2.43
GetHeight is a method that returns the height of the TextSplit.
Returns:
- int: The height of the TextSplit.
func (*TextSplit) GetLines ¶ added in v0.2.43
GetLines is a method that returns the lines of the TextSplit.
Returns:
- []*SpltLine: The lines of the TextSplit.
func (*TextSplit) GetRunes ¶ added in v0.2.43
GetRunes is a method of TextSplit that returns the runes of the TextSplit.
Returns:
- [][]rune: A slice of runes representing the words in the TextSplit.
Behaviors:
- It is always a slice of runes with one line.
func (*TextSplit) InsertWord ¶ added in v0.2.43
InsertWord is a method that attempts to insert a given word into the TextSplit.
Parameters:
- word: The word to insert.
Returns:
- bool: True if the word was successfully inserted, and false if the word is too long to fit within the width of the TextSplit.
func (*TextSplit) InsertWords ¶ added in v0.2.43
InsertWords is a method that attempts to insert multiple words into the TextSplit.
Parameters:
- words: The words to insert.
Returns:
- int: The index of the first word that could not be inserted, or -1 if all words were inserted.