Documentation ¶
Index ¶
- func CalculateNumberOfLines(text []string, width int) (int, error)
- func DateStringer(date time.Time) string
- func FindContentIndexes(openingToken, closingToken string, contentTokens []string) (int, int, error)
- func GenerateID(size int) (string, error)
- func GetOrdinalSuffix(number int) string
- func ReplaceSuffix(str, suffix string) (string, error)
- func SplitSentenceIntoFields(sentence string, indentLevel int) ([][]string, error)
- func TimeStringer(time time.Time) string
- type ErrClosingTokenNotFound
- type ErrNeverOpened
- type ErrOpeningTokenNotFound
- type SpltLine
- type TextSplitter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateNumberOfLines ¶
CalculateNumberOfLines is a function that calculates the minimum number of lines needed to fit a given text within a specified width.
Panics with an error of type *ers.ErrInvalidParameter if the text is empty, or if the width is less than or equal to 0.
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.
- bool: True if it is possible to fit the text within the width, and false otherwise.
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.
func DateStringer ¶
DateStringer prints the date in the format "1st January, 2006".
Parameters:
- date: The date to print.
Returns:
- string: The date in the format "1st January, 2006".
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.
Panics with an error of type *ers.ErrInvalidParameter if the opening or closing tokens are empty, or with an error of type *ers.ErrCallFailed if the opening token is not found.
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: An error of type *ErrNeverOpened if the closing token is found without any corresponding opening token, or an error of type *ErrClosingTokenNotFound if 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 of type *ers.ErrInvalidParameter if the size is less than 1. Otherwise, any error returned by the rand.Read function.
The function uses the crypto/rand package to generate a random ID of the specified size.
func GetOrdinalSuffix ¶
GetOrdinalSuffix returns the ordinal suffix for a given integer.
Parameters:
- number: The integer for which to get the ordinal suffix.
Returns:
- string: The ordinal suffix for the number.
For example, for the number 1, the function returns "1st"; for the number 2, it returns "2nd"; and so on.
func ReplaceSuffix ¶
ReplaceSuffix replaces the end of the given string with the provided suffix.
Panics with an error of type *ers.ErrCallFailed if the suffix is longer than the string.
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.
func SplitSentenceIntoFields ¶
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.
Panics with an error of type *ers.ErrInvalidParameter if the indentLevel is negative.
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 if any of the runes in the string is invalid.
func TimeStringer ¶ added in v0.2.11
TimeStringer prints the time in the format "3:04 PM".
Parameters:
- time: The time to print.
Returns:
- string: The time in the format "3:04 PM".
Types ¶
type ErrClosingTokenNotFound ¶
type ErrClosingTokenNotFound struct {
// contains filtered or unexported fields
}
ErrClosingTokenNotFound is a struct that represents an error when a closing token is not found in the content.
func (*ErrClosingTokenNotFound) Error ¶
func (e *ErrClosingTokenNotFound) Error() string
Error is a method of the ErrClosingTokenNotFound type that implements the error interface.
Returns:
- string: The error message.
type ErrNeverOpened ¶
type ErrNeverOpened struct {
// contains filtered or unexported fields
}
ErrNeverOpened is a struct that represents an error when a closing token is found without a corresponding opening token.
func (*ErrNeverOpened) Error ¶
func (e *ErrNeverOpened) Error() string
Error is a method of the ErrNeverOpened type that implements the error interface.
Returns:
- string: The error message.
type ErrOpeningTokenNotFound ¶
type ErrOpeningTokenNotFound struct {
// contains filtered or unexported fields
}
ErrOpeningTokenNotFound is a struct that represents an error when an opening token is not found in the content.
func (*ErrOpeningTokenNotFound) Error ¶
func (e *ErrOpeningTokenNotFound) Error() string
Error is a method of the ErrOpeningTokenNotFound type that implements the error interface.
Returns:
- string: The error message.
type SpltLine ¶
type SpltLine struct { // The Line field is a slice of strings, each representing a word in the line. Line []string // The Len field is an integer representing the total length of the line, // including spaces between words. Len int }
SpltLine is a helper struct used in the SplitTextInEqualSizedLines function. It represents a line of text.
func NewSpltLine ¶
NewSpltLine creates a new SpltLine instance.
Parameters:
- word: The initial word to add to the line.
Returns:
- *SpltLine: A pointer to the created SpltLine instance.
func (*SpltLine) InsertWord ¶
InsertWord is a method of SpltLine that adds a given word to the end of the line.
Parameters:
- word: The word to add to the line.
type TextSplitter ¶
type TextSplitter struct { // The Width represents the maximum length of a line. Width int // The Lines field is a slice of pointers to SpltLine structs, each representing // a line of text. Lines []*SpltLine }
TextSplitter is a helper struct used in the SplitTextInEqualSizedLines function. It holds the width of the lines and a slice of pointers to SpltLine structs.
func SplitTextInEqualSizedLines ¶
func SplitTextInEqualSizedLines(text []string, width int, maxHeight optional.Int) (*TextSplitter, error)
SplitTextInEqualSizedLines is a function that splits a given text into lines of equal width.
Parameters:
- text: The slice of strings representing the text to split.
- width: The width of the lines.
- maxHeight: The maximum height of the text.
Returns:
- *TextSplitter: A pointer to the created TextSplitter 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 (*TextSplitter) CanInsertWord ¶
func (ts *TextSplitter) CanInsertWord(word string, lineIndex int) bool
CanInsertWord is a method of TextSplitter that checks if a given word can be inserted into a specific line without exceeding the width of the TextSplitter.
Parameters:
- word: The word to check.
- lineIndex: The index of the line to check.
Returns:
- bool: True if the word can be inserted into the line at lineIndex without exceeding the width, and false otherwise.
func (*TextSplitter) GetFurthestRightEdge ¶
func (ts *TextSplitter) GetFurthestRightEdge() int
GetFurthestRightEdge is a method of TextSplitter that returns the length of the longest line in the TextSplitter.
Returns:
- int: The length of the longest line.
func (*TextSplitter) InsertWord ¶
func (ts *TextSplitter) InsertWord(word string) bool
InsertWord is a method of TextSplitter that attempts to insert a given word into the TextSplitter.
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 TextSplitter.
func (*TextSplitter) InsertWordAt ¶
func (ts *TextSplitter) InsertWordAt(word string, lineIndex int) error
InsertWordAt is a method of TextSplitter that attempts to insert a given word into a specific line of the TextSplitter.
Panics with an error of type *ers.ErrInvalidParameter if the lineIndex is out of bounds.
Parameters:
- word: The word to insert.
- lineIndex: The index of the line to insert the word into.