StringExt

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateNumberOfLines

func CalculateNumberOfLines(text []string, width int) (int, error)

CalculateNumberOfLines is a function that calculates the minimum number of lines needed to fit a given text within a specified width.

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.

Errors:

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

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 FirstInstanceOfWS added in v0.3.37

func FirstInstanceOfWS(chars []rune, from_idx, to_idx int) int

FirstInstanceOfWS finds the first instance of whitespace in the characters.

Parameters:

  • chars: The characters.
  • from_idx: The starting index. (inclusive)
  • to_idx: The ending index. (exclusive)

Returns:

  • int: The index of the first whitespace character. -1 if not found.

Behaviors:

  • If from_idx < 0, from_idx is set to 0.
  • If to_idx >= len(chars), to_idx is set to len(chars) - 1.
  • If from_idx > to_idx, from_idx and to_idx are swapped.

FIXME: Remove this function once MyGoLib is updated.

func FitString added in v0.2.41

func FitString(s string, width int) string

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.

Behaviors:

  • If the width is less than 0, it is set to 0.
  • 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

func GenerateID(size int) (string, error)

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:

  • *uc.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 LastInstanceOfWS added in v0.3.37

func LastInstanceOfWS(chars []rune, from_idx, to_idx int) int

LastInstanceOfWS finds the last instance of whitespace in the characters.

Parameters:

  • chars: The characters.
  • from_idx: The starting index. (inclusive)
  • to_idx: The ending index. (exclusive)

Returns:

  • int: The index of the last whitespace character. -1 if not found.

Behaviors:

  • If from_idx < 0, from_idx is set to 0.
  • If to_idx >= len(chars), to_idx is set to len(chars) - 1.
  • If from_idx > to_idx, from_idx and to_idx are swapped.

func ReplaceSuffix

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

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 Title added in v0.3.40

func Title(str string) (string, error)

Title capitalizes the first letter of the input string and returns the modified string.

Parameters:

  • str: The input string to be processed.

Returns:

  • string: The modified string with the first letter capitalized.
  • error: An error if the input string is empty or has invalid UTF-8 encoding.

Types

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 implements the error interface.

Message: "number of lines ({NumberOfLines}) is greater than the number of words ({NumberOfWords})"

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 implements the error interface.

Message: "suffix {Suffix} is longer than the string {Str}"

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 implements the error interface.

Message: "no candidate found"

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

func NewTextSplit(maxWidth, maxHeight int) (*TextSplit, error)

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 *uc.ErrInvalidParameter if the maxWidth or maxHeight is less than 0.

func SplitInEqualSizedLines added in v0.2.43

func SplitInEqualSizedLines(text []string, width, height int) (*TextSplit, error)

SplitInEqualSizedLines is a function that splits a given text into lines of equal width.

Errors:

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

func (ts *TextSplit) Copy() uc.Copier

Copy implements the common.Copier interface.

func (*TextSplit) GetFirstLine added in v0.2.43

func (ts *TextSplit) GetFirstLine() []string

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

func (ts *TextSplit) GetFurthestRightEdge() (int, bool)

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

func (ts *TextSplit) GetHeight() int

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

func (ts *TextSplit) GetLines() []string

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

func (ts *TextSplit) GetRunes() [][]rune

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

func (ts *TextSplit) InsertWord(word string) bool

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

func (ts *TextSplit) InsertWords(words []string) int

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.

Jump to

Keyboard shortcuts

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