Strings

package
v0.2.42 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AndString

func AndString(vals ...string) string

AndString concatenates a list of strings using commas and the word "and" before the last string.

Parameters:

  • vals: The list of strings to concatenate.

Returns:

  • string: The concatenated string.

func CalculateNumberOfLines added in v0.2.32

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.

Errors:

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

func DateStringer

func DateStringer(date time.Time) string

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 GetOrdinalSuffix

func GetOrdinalSuffix(number int) string

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 OrString

func OrString(vals ...string) string

OrString concatenates a list of strings using commas and the word "or" before the last string.

Parameters:

  • vals: The list of strings to concatenate.

Returns:

  • string: The concatenated string.

func StringsJoiner added in v0.2.36

func StringsJoiner[T fmt.Stringer](values []T, sep string) string

StringsJoiner joins a list of fmt.Stringer values using a separator.

Parameters:

  • values: The list of fmt.Stringer values to join.
  • sep: The separator to use when joining the strings.

Returns:

  • string: The string representation of the values.

func TimeStringer

func TimeStringer(time time.Time) string

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 ErrLinesGreaterThanWords added in v0.2.32

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

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

func (e *ErrLinesGreaterThanWords) 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.32

type ErrNoCandidateFound struct{}

ErrNoCandidateFound is an error type that is returned when no candidate is found.

func NewErrNoCandidateFound added in v0.2.32

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

func (e *ErrNoCandidateFound) Error() string

Error is a method of the error interface that returns the error message.

Returns:

  • string: The error message.

type SpltLine added in v0.2.32

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

func NewSpltLine(word string) *SpltLine

NewSpltLine creates a new SpltLine with the given initial word.

Parameters:

  • word: The initial word to add to the line.

Returns:

  • *SpltLine: A pointer to the newly created SpltLine.

func (*SpltLine) Copy added in v0.2.32

func (sl *SpltLine) Copy() intf.Copier

Copy is a method of intf.Copier that creates a shallow copy of the SpltLine.

Returns:

  • intf.Copier: A shallow copy of the SpltLine.

func (*SpltLine) InsertWord added in v0.2.32

func (sl *SpltLine) InsertWord(word string)

InsertWord is a method of SpltLine that adds a given word to the end of the line.

If the word is an empty string, it is ignored.

Parameters:

  • word: The word to add to the line.

func (*SpltLine) String added in v0.2.32

func (sl *SpltLine) String() string

String is a method of fmt.Stringer that returns the string representation of the SpltLine.

Returns:

  • string: The resulting string.

type TextSplit added in v0.2.32

type TextSplit 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
}

TextSplit is a helper struct used in the SplitTextInEqualSizedLines function.

func NewTextSplit added in v0.2.41

func NewTextSplit(width, height int) (*TextSplit, error)

NewTextSplit creates a new TextSplit with the given width.

Parameters:

  • width: The maximum length of a line.
  • height: The maximum number of lines.

Returns:

  • *TextSplit: A pointer to the newly created TextSplit.
  • error: An error of type *ers.ErrInvalidParameter if the width or height is less than 0.

func (*TextSplit) CanInsertWord added in v0.2.32

func (ts *TextSplit) CanInsertWord(word string, lineIndex int) bool

CanInsertWord is a method that checks if a given word can be inserted into a specific line without exceeding the width of the TextSplit.

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 (*TextSplit) Copy added in v0.2.32

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

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

func (ts *TextSplit) GetFirstLine() *SpltLine

func (*TextSplit) GetFurthestRightEdge added in v0.2.32

func (ts *TextSplit) GetFurthestRightEdge() int

GetFurthestRightEdge is a method that returns the length of the longest line in the TextSplit.

Returns:

  • int: The length of the longest line.

func (*TextSplit) InsertWord added in v0.2.32

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) InsertWordAt added in v0.2.32

func (ts *TextSplit) InsertWordAt(word string, lineIndex int) error

InsertWordAt is a method that attempts to insert a given word into a specific line of the TextSplit.

Parameters:

  • word: The word to insert.
  • lineIndex: The index of the line to insert the word into.

Returns:

  • error: An error of type *ers.ErrInvalidParameter if the lineIndex is out of bounds.

type TextSplitter added in v0.2.32

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

func NewTextSplitter added in v0.2.32

func NewTextSplitter(width int) (*TextSplitter, error)

func (*TextSplitter) GetSolution added in v0.2.32

func (tsr *TextSplitter) GetSolution() (*TextSplit, error)

GetSolution returns the solution of the TextSplitter.

Returns:

  • *TextSplit: The solution of the TextSplitter.
  • error: An error of type *ErrNoCandidateFound if no candidate is found.

func (*TextSplitter) SetHeight added in v0.2.32

func (ts *TextSplitter) SetHeight(height int) error

func (*TextSplitter) SplitInEqualSizedLines added in v0.2.32

func (tsr *TextSplitter) SplitInEqualSizedLines(text []string) error

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

Errors:

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

Jump to

Keyboard shortcuts

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