strings

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NoCandidateFound is an error that is returned when no candidate is found. Readers
	// must return this error as is and not wrap it as callers are expected to check
	// for this error using ==.
	NoCandidateFound error
)

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

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

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

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

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

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

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

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. Never returns nil.

func (ErrLinesGreaterThanWords) Error

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

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

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. Never returns nil.

func (ErrLongerSuffix) Error

func (e ErrLongerSuffix) Error() string

Error implements the error interface.

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

type TextSplit

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

TextSplit represents a split text with a maximum width and height.

func NewTextSplit

func NewTextSplit(max_width, max_height int) (*TextSplit, error)

NewTextSplit creates a new TextSplit with the given maximum width and height.

Parameters:

  • max_width: The maximum length of a line.
  • max_height: The maximum number of lines.

Returns:

  • *TextSplit: A pointer to the newly created TextSplit.
  • error: An error of type *errors.ErrInvalidParameter if the maxWidth or maxHeight is less than 0.

func SplitInEqualSizedLines

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

func (ts TextSplit) Copy() *TextSplit

Copy is a method that creates a copy of the TextSplit.

Returns:

  • *TextSplit: A copy of the TextSplit. Never returns nil.

func (TextSplit) FirstLine

func (ts TextSplit) FirstLine() []string

FirstLine 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) FurthestRightEdge

func (ts TextSplit) FurthestRightEdge() (int, bool)

FurthestRightEdge 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) Height

func (ts TextSplit) Height() int

Height is a method that returns the height of the TextSplit.

Returns:

  • int: The height of the TextSplit.

func (*TextSplit) InsertWord

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

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.

func (TextSplit) Lines

func (ts TextSplit) Lines() []string

Lines is a method that returns the lines of the TextSplit.

Returns:

  • []string: The lines of the TextSplit.

func (TextSplit) Runes

func (ts TextSplit) Runes() [][]rune

Runes 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.

Jump to

Keyboard shortcuts

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