Documentation ¶
Index ¶
- func CalculateNumberOfLines(text []string, width int) (int, error)
- func FindContentIndexes(openingToken, closingToken string, contentTokens []string) (int, int, error)
- func GetOrdinalSuffix(number int) string
- func ReplaceSuffix(str, suffix string) (string, error)
- func SplitSentenceIntoFields(sentence string, indentLevel int) ([][]string, error)
- type ErrClosingTokenEmpty
- type ErrEmptyText
- type ErrHeightTooSmall
- type ErrOpeningTokenEmpty
- type ErrOpeningTokenNotFound
- type ErrSuffixTooLong
- type ErrWidthTooSmall
- type ErrWordTooLong
- type SpltLine
- type TextSplitter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateNumberOfLines ¶ added in v0.2.4
CalculateNumberOfLines is a function that calculates the minimum number of lines needed to fit a given text within a specified width. It takes a slice of strings (text) and an integer (width) as input. The function returns the calculated number of lines and an error.
If the input text is empty, the function returns 0 and an ErrEmptyText error. If the width is less than or equal to 0, the function returns 0 and an ErrWidthTooSmall error.
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.
If the calculated number of lines is greater than the number of words in the text, the function returns 0 and an ErrWidthTooSmall error, indicating that the width is too small.
Otherwise, the function returns the calculated number of lines and no error.
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. It returns the start and end indexes of the content between the tokens, and an error if any.
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:
- The start index of the content (inclusive).
- The end index of the content (exclusive).
- An error if the opening or closing tokens are empty, or if they are not found in the correct order in the contentTokens.
If the openingToken is found but the closingToken is not, the function will return an error. If the closingToken is found before the openingToken, the function will return an error. If the closingToken is a newline ("\n") and it is not found, the function will return the length of the contentTokens as the end index.
Errors returned can be of type ErrOpeningTokenEmpty, ErrClosingTokenEmpty, ErrOpeningTokenNotFound, or a generic error with a message about the closing token.
func GetOrdinalSuffix ¶
GetOrdinalSuffix returns the ordinal suffix for a given integer.
Parameters:
- number: The integer for which to get the ordinal suffix.
The function returns a string that represents the number with its ordinal suffix.
For example, for the number 1, the function returns "1st"; for the number 2, it returns "2nd"; and so on. For numbers ending in 11, 12, or 13, the function returns the number with the suffix "th" (e.g., "11th", "12th", "13th"). For negative numbers, the function also returns the number with the suffix "th".
If the last digit of the number is 0 or greater than 3 (and the last two digits are not 11, 12, or 13), the function returns the number with the suffix "th". If the last digit of the number is 1, 2, or 3 (and the last two digits are not 11, 12, or 13), the function returns the number with the corresponding ordinal suffix ("st", "nd", or "rd").
func ReplaceSuffix ¶
ReplaceSuffix replaces the end of the given string with the provided suffix. The function checks the lengths of the string and the suffix to determine the appropriate action:
- If the length of the suffix is greater than the length of the string, the function returns an empty string and an ErrSuffixTooLong error.
- If the length of the suffix is equal to the length of the string, the function returns the suffix.
- If the length of the suffix is zero, the function returns the original string.
- Otherwise, the function replaces the end of the string with the suffix and returns the result.
Parameters:
- str: The original string.
- suffix: The suffix to replace the end of the string.
Returns:
- The modified string, or an error if the suffix is too long.
func SplitSentenceIntoFields ¶ added in v0.2.7
SplitSentenceIntoFields takes a string and an indent level as input. It 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. The indent level determines the number of spaces that a tab character is replaced with.
The function returns a two-dimensional slice of strings. Each inner slice represents the fields of a line from the input string. Lines that do not contain any fields (i.e., they are empty or consist only of whitespace) are not included in the output.
If the input string is empty, the function returns an empty two-dimensional slice. If the indent level is negative, the function returns an ErrInvalidParameter error. If the input string contains an invalid rune, the function returns an error.
Types ¶
type ErrClosingTokenEmpty ¶
type ErrClosingTokenEmpty struct{}
ErrClosingTokenEmpty is a struct that represents an error when a closing token is empty. It does not have any fields as the error condition is solely based on the absence of a closing token.
func (*ErrClosingTokenEmpty) Error ¶
func (e *ErrClosingTokenEmpty) Error() string
Error is a method of the ErrClosingTokenEmpty type that implements the error interface. It returns a string representation of the error, that is, the string "closing token is empty".
type ErrEmptyText ¶ added in v0.2.4
type ErrEmptyText struct{}
ErrEmptyText represents an error when a text input is empty.
func (*ErrEmptyText) Error ¶ added in v0.2.4
func (e *ErrEmptyText) Error() string
Error generates the error message for the ErrEmptyText error. The message indicates that the text input cannot be empty.
type ErrHeightTooSmall ¶ added in v0.2.4
type ErrHeightTooSmall struct{}
ErrHeightTooSmall represents an error when a height value is less than 1.
func (*ErrHeightTooSmall) Error ¶ added in v0.2.4
func (e *ErrHeightTooSmall) Error() string
Error generates the error message for the ErrHeightTooSmall error. The message indicates that the height value must be at least 1.
type ErrOpeningTokenEmpty ¶
type ErrOpeningTokenEmpty struct{}
ErrOpeningTokenEmpty is a struct that represents an error when an opening token is empty. It does not have any fields as the error condition is solely based on the absence of an opening token.
func (*ErrOpeningTokenEmpty) Error ¶
func (e *ErrOpeningTokenEmpty) Error() string
Error is a method of the ErrOpeningTokenEmpty type that implements the error interface. It returns a string representation of the error, that is, the string "opening token is empty".
type ErrOpeningTokenNotFound ¶
type ErrOpeningTokenNotFound struct{}
ErrOpeningTokenNotFound is a struct that represents an error when an opening token is not found in the content. It does not have any fields as the error condition is solely based on the absence of an opening token in the content.
func (*ErrOpeningTokenNotFound) Error ¶
func (e *ErrOpeningTokenNotFound) Error() string
Error is a method of the ErrOpeningTokenNotFound type that implements the error interface. It returns a string representation of the error, that is, the string "opening token not found in content".
type ErrSuffixTooLong ¶
type ErrSuffixTooLong struct{}
ErrSuffixTooLong is a struct that represents an error when a suffix is too long. It does not have any fields as the error condition is solely based on the length of the suffix.
func (*ErrSuffixTooLong) Error ¶
func (e *ErrSuffixTooLong) Error() string
Error is a method of the ErrSuffixTooLong type that implements the error interface. It returns a string representation of the error, that is, the string "suffix is too long".
type ErrWidthTooSmall ¶ added in v0.2.4
type ErrWidthTooSmall struct{}
ErrWidthTooSmall is an error that occurs when the width is too small to fit the text.
func (*ErrWidthTooSmall) Error ¶ added in v0.2.4
func (e *ErrWidthTooSmall) Error() string
Error method for ErrWidthTooSmall. It returns a string indicating that the width was too small to fit the text.
type ErrWordTooLong ¶ added in v0.2.4
type ErrWordTooLong struct {
// contains filtered or unexported fields
}
ErrWordTooLong is an error that occurs when a word is too long to fit within a certain width.
func (*ErrWordTooLong) Error ¶ added in v0.2.4
func (e *ErrWordTooLong) Error() string
Error method for ErrWordTooLong. It returns a formatted string indicating the word that was too long.
type SpltLine ¶ added in v0.2.4
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 ¶ added in v0.2.4
NewSpltLine creates a new SpltLine instance. The initial word is passed as an argument. The length of the line is calculated based on the initial word. It returns a pointer to the created SpltLine instance.
func (*SpltLine) InsertWord ¶ added in v0.2.4
InsertWord is a method on the SpltLine struct. It adds a given word to the end of the Line slice and increases the Len field by the length of the word plus one (for the space). The method does not return any value.
type TextSplitter ¶ added in v0.2.4
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 ¶ added in v0.2.4
func SplitTextInEqualSizedLines(text []string, width, height int) (*TextSplitter, error)
SplitTextInEqualSizedLines is a function that splits a given text into lines of equal width. It takes a slice of strings (text) and two integers (width, height) as input. The function returns a pointer to a TextSplitter struct and an error.
If the input text is empty, the function returns nil and an ErrEmptyText error. If the width is less than or equal to 0, the function returns nil and an ErrWidthTooSmall error.
If the height is less than 1, the function calculates the height itself using the CalculateNumberOfLines function. If CalculateNumberOfLines returns an error, SplitTextInEqualSizedLines returns nil and the same error.
The function then creates a new TextSplitter struct and starts inserting words into it. If a word cannot be inserted, the function returns an error.
After all words have been inserted, the function calculates the Sum of Squared Mean (SQM) for the current solution and starts looking for an optimal solution by moving words from the last line to the above line.
The function keeps track of all candidates for the optimal solution and their corresponding SQM. The candidate with the lowest SQM is considered the optimal solution.
If there are multiple candidates with the same lowest SQM, the function simply returns the first one.
The function finally returns the first line of the optimal solution and no error.
func (*TextSplitter) CanInsertWord ¶ added in v0.2.4
func (ts *TextSplitter) CanInsertWord(word string, lineIndex int) bool
CanInsertWord is a method on the TextSplitter struct. It checks if a given word can be inserted into a specific line without exceeding the width of the TextSplitter. The method takes a string (word) and an integer (lineIndex) as input. It returns true if the word can be inserted into the line at lineIndex without exceeding the width, and false otherwise.
func (*TextSplitter) GetFurthestRightEdge ¶ added in v0.2.4
func (ts *TextSplitter) GetFurthestRightEdge() int
GetFurthestRightEdge is a method on the TextSplitter struct. It iterates over all lines in the TextSplitter and finds the length of the longest line. If no lines exist, it returns the width of the TextSplitter. Otherwise, it returns the length of the longest line.
func (*TextSplitter) InsertWord ¶ added in v0.2.4
func (ts *TextSplitter) InsertWord(word string) error
InsertWord is a method on the TextSplitter struct. It attempts to insert a given word into the TextSplitter. If the length of the word is greater than the width of the TextSplitter, it returns an ErrWordTooLong error. If the word can be inserted without exceeding the width, it is added to the Lines slice and the method returns nil. If adding the word to the last line would exceed the width, the words of the last line are shifted to the left until the word can be inserted or there are no more lines.
func (*TextSplitter) InsertWordAt ¶ added in v0.2.4
func (ts *TextSplitter) InsertWordAt(word string, lineIndex int) error