runes

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: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AndString added in v0.4.5

func AndString(values []rune, quote bool) string

AndString is a function that returns a string representation of a slice of runes.

Parameters:

  • values: The values to convert to a string.
  • quote: Whether to quote the values.

Returns:

  • string: The string representation of the values.

func BytesToUtf8

func BytesToUtf8(data []byte) ([]rune, error)

BytesToUtf8 is a function that converts bytes to runes.

Parameters:

  • data: The bytes to convert.

Returns:

  • []rune: The runes.
  • error: An error of type *ErrInvalidUTF8Encoding if the bytes are not valid UTF-8.

This function also converts '\r\n' to '\n'. Plus, whenever an error occurs, it returns the runes decoded so far and the index of the error rune.

func EitherOrString

func EitherOrString(values []rune, quote bool) string

EitherOrString is a function that returns a string representation of a slice of runes.

Parameters:

  • values: The values to convert to a string.
  • quote: True if the values should be quoted, false otherwise.

Returns:

  • string: The string representation.

Example:

EitherOrString([]string{'a', 'b', 'c'}, false) // "a, b or c"

func FindContentIndexes

func FindContentIndexes(op_token, cl_token rune, tokens []rune) (result [2]int, err error)

FindContentIndexes searches for the positions of opening and closing tokens in a slice of strings.

Parameters:

  • op_token: The string that marks the beginning of the content.
  • cl_token: The string that marks the end of the content.
  • tokens: The slice of strings in which to search for the tokens.

Returns:

  • result: An array of two integers representing the start and end indexes of the content.
  • err: Any error that occurred while searching for the tokens.

Errors:

  • *uc.ErrInvalidParameter: If the openingToken or closingToken is an empty string.
  • *ErrTokenNotFound: If the opening or closing token is not found in the content.
  • *ErrNeverOpened: If the closing token is found without any corresponding opening token.

Behaviors:

  • The first index of the content is inclusive, while the second index is exclusive.
  • This function returns a partial result when errors occur. ([-1, -1] if errors occur before finding the opening token, [index, 0] if the opening token is found but the closing token is not found.

func IndicesOf

func IndicesOf(data []byte, sep []byte, exclude_sep bool) []int

Indices returns the indices of the separator in the data.

Parameters:

  • data: The data.
  • sep: The separator.
  • exclude_sep: Whether the separator is inclusive. If set to true, the indices will point to the character right after the separator. Otherwise, the indices will point to the character right before the separator.

Returns:

  • []int: The indices.

func MultiMatcher

func MultiMatcher(chars []rune, stream CharStream) (string, error)

MultiMatcher kinda works like WordMatcher but, unlike WordMatcher, it only matches a specific set of characters.

Parameters:

  • chars: The characters to match.
  • stream: The CharStream to use.

Returns:

  • string: The matched string.
  • error: An error if the matching process failed.

Errors:

  • *common.ErrInvalidParameter: If the input stream is nil or the input characters are empty.

func OrString

func OrString(values []rune, quote, is_negative bool) string

OrString is a function that returns a string representation of a slice of runes.

Parameters:

  • values: The values to convert to a string.
  • quote: True if the values should be quoted, false otherwise.
  • is_negative: True if the string should use "nor" instead of "or", false otherwise.

Returns:

  • string: The string representation.

Example:

OrString([]string{'a', 'b', 'c'}, false, true) // "a, b, nor c"

func StringToUtf8

func StringToUtf8(str string) ([]rune, error)

StringToUtf8 converts a string to a slice of runes.

Parameters:

  • str: The string to convert.

Returns:

  • runes: The slice of runes.
  • error: An error of type *ErrInvalidUTF8Encoding if the string is not valid UTF-8.

Behaviors:

  • An empty string returns a nil slice with no errors.
  • The function stops at the first invalid UTF-8 encoding; returning an error and the runes found up to that point.
  • The function converts '\r\n' to '\n'.

Types

type BoxBorderType

type BoxBorderType int

BoxBorderType is the type of the box border.

const (
	// BtNormal is the normal box border type.
	BtNormal BoxBorderType = iota

	// BtTriple is the triple box border type.
	BtTriple

	// BtQuadruple is the quadruple box border type.
	BtQuadruple

	// BtDouble is the double box border type.
	BtDouble

	// BtRounded is like BtNormal but with rounded corners.
	BtRounded
)

type BoxStyle

type BoxStyle struct {
	// LineType is the type of the line.
	LineType BoxBorderType

	// IsHeavy is whether the line is heavy or not.
	// Only applicable to BtNormal, BtTriple, and BtQuadruple.
	IsHeavy bool

	// Padding is the padding of the box.
	// [Top, Right, Bottom, Left]
	Padding [4]int
}

BoxStyle is the style of the box.

var (
	// DefaultBoxStyle is the default box style.
	DefaultBoxStyle *BoxStyle
)

func NewBoxStyle

func NewBoxStyle(line_type BoxBorderType, is_heavy bool, padding [4]int) *BoxStyle

NewBoxStyle creates a new box style.

Negative padding are set to 0.

Parameters:

  • line_type: The line type.
  • is_heavy: Whether the line is heavy or not.
  • padding: The padding of the box. [Top, Right, Bottom, Left]

Returns:

  • *BoxStyle: The new box style.

func (*BoxStyle) ApplyStrings

func (bs *BoxStyle) ApplyStrings(content []string) (*RuneTable, error)

DrawBox draws a box around the content.

Format: If the content is ["Hello", "World"], the box will be:

┏━━━━━━━┓
┃ Hello ┃
┃ World ┃
┗━━━━━━━┛

Parameters:

  • content: The content.

Returns:

  • *RuneTable: The content in a box.
  • error: An error if the content could not be processed.

Behaviors:

  • If the box style is nil, the default box style will be used.

func (*BoxStyle) GetCorners

func (bs *BoxStyle) GetCorners() [4]rune

GetCorners gets the corners of the box.

Returns:

  • [4]rune: The corners. [TopLeft, TopRight, BottomLeft, BottomRight]

func (*BoxStyle) GetSideBorder

func (bs *BoxStyle) GetSideBorder() rune

GetSideBorder gets the side border of the box.

It also applies to the left border as they are the same.

Returns:

  • string: The side border.

func (*BoxStyle) GetTopBorder

func (bs *BoxStyle) GetTopBorder() rune

GetTopBorder gets the top border of the box.

It also applies to the bottom border as they are the same.

Returns:

  • string: The top border.

type CharStream

type CharStream interface {
	// IsDone checks whether the stream is done.
	//
	// Returns:
	//   - bool: True if the stream is done. False otherwise.
	IsDone() bool

	// Next returns the next character in the stream while advancing the position.
	//
	// Returns:
	//   - rune: The next character in the stream. utf8.RuneError if the stream is done.
	//   - bool: True if the stream has more characters. False otherwise.
	Next() (rune, bool)

	// Peek returns the next character in the stream without advancing the position.
	//
	// Returns:
	//   - rune: The next character in the stream. utf8.RuneError if the stream is done.
	//   - bool: True if the stream has more characters. False otherwise.
	Peek() (rune, bool)

	// Refuse undoes the last Next operation.
	//
	// Returns:
	//   - bool: True if the last Next operation was undone. False otherwise.
	Refuse() bool

	// RefuseMany undoes any Next operation since the last Accept operation.
	// This is useful when you want to abandon the current word and start a new one.
	RefuseMany()

	// Accept accepts the next character in the stream. This is useful for signifying
	// valid sequences that should not be undone.
	Accept()
}

CharStream is an interface for a character stream.

type ErrInvalidUTF8Encoding

type ErrInvalidUTF8Encoding struct {
	// At is the index of the invalid UTF-8 encoding.
	At int
}

ErrInvalidUTF8Encoding is an error type for invalid UTF-8 encoding.

func NewErrInvalidUTF8Encoding

func NewErrInvalidUTF8Encoding(at int) *ErrInvalidUTF8Encoding

NewErrInvalidUTF8Encoding creates a new ErrInvalidUTF8Encoding error.

Parameters:

  • at: The index of the invalid UTF-8 encoding.

Returns:

  • *ErrInvalidUTF8Encoding: A pointer to the newly created error.

func (*ErrInvalidUTF8Encoding) Error

func (e *ErrInvalidUTF8Encoding) Error() string

Error implements the error interface.

Message: "invalid UTF-8 encoding"

type ErrNeverOpened

type ErrNeverOpened struct {
	// OpeningToken is the opening token that was never closed.
	OpeningToken rune

	// ClosingToken is the closing token that was found without a corresponding
	// opening token.
	ClosingToken rune
}

ErrNeverOpened is a struct that represents an error when a closing token is found without a corresponding opening token.

func NewErrNeverOpened

func NewErrNeverOpened(openingToken, closingToken rune) *ErrNeverOpened

NewErrNeverOpened is a constructor of ErrNeverOpened.

Parameters:

  • openingToken: The opening token that was never closed.
  • closingToken: The closing token that was found without a corresponding opening token.

Returns:

  • *ErrNeverOpened: A pointer to the newly created error.

func (*ErrNeverOpened) Error

func (e *ErrNeverOpened) Error() string

Error implements the error interface.

Message:

  • "closing token {ClosingToken} found without a corresponding opening token {OpeningToken}".

type ErrNoClosestWordFound

type ErrNoClosestWordFound struct{}

ErrNoClosestWordFound is an error when no closest word is found.

func NewErrNoClosestWordFound

func NewErrNoClosestWordFound() *ErrNoClosestWordFound

NewErrNoClosestWordFound creates a new ErrNoClosestWordFound.

Returns:

  • *ErrNoClosestWordFound: The new ErrNoClosestWordFound.

func (*ErrNoClosestWordFound) Error

func (e *ErrNoClosestWordFound) Error() string

Error implements the error interface.

Message: "no closest word was found"

type ErrTokenNotFound

type ErrTokenNotFound struct {
	// Token is the token that was not found in the content.
	Token rune

	// IsOpening is the type of the token (opening or closing).
	IsOpening bool
}

ErrTokenNotFound is a struct that represents an error when a token is not found in the content.

func NewErrTokenNotFound

func NewErrTokenNotFound(token rune, is_opening bool) *ErrTokenNotFound

NewErrTokenNotFound is a constructor of ErrTokenNotFound.

Parameters:

  • token: The token that was not found in the content.
  • is_opening: The type of the token (opening or closing).

Returns:

  • *ErrTokenNotFound: A pointer to the newly created error.

func (*ErrTokenNotFound) Error

func (e *ErrTokenNotFound) Error() string

Error implements the error interface.

Message: "{Type} token {Token} is not in the content"

type LavenshteinTable

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

LevenshteinTable is a table of words for the Levenshtein distance.

func NewLevenshteinTable

func NewLevenshteinTable(words ...string) (*LavenshteinTable, error)

NewLevenshteinTable creates a new Levenshtein table with the given words.

Parameters:

  • words: The words to add to the table.

Returns:

  • *LevenshteinTable: The new Levenshtein table.
  • error: An error if any of the words cannot be added to the table.

Errors:

  • *common.ErrAt: Whenever a word is not valid UTF-8.

It is the same as creating an empty table and then adding the words to it.

func (*LavenshteinTable) AddWord

func (lt *LavenshteinTable) AddWord(word string) error

AddWord adds a word to the table.

Parameters:

  • word: The word to add.

Returns:

  • error: An error of type *ErrInvalidUTF8Encoding if the word is not valid UTF-8.

func (*LavenshteinTable) GetClosest

func (lt *LavenshteinTable) GetClosest(target []rune) (string, error)

GetClosest gets the closest word to a target.

Parameters:

  • target: The target.

Returns:

  • string: The closest word.
  • error: The error if any occurs.

Errors:

  • *common.ErrInvalidParameter: If the target is empty.
  • *ErrNoClosestWordFound: If no closest word is found.

type RuneTable

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

RuneTable is a table of runes.

func NewRuneTable

func NewRuneTable(lines []string) (*RuneTable, error)

NewRuneTable creates a new RuneTable with the given lines.

Parameters:

  • lines: The lines to add to the table.

Returns:

  • *RuneTable: The new RuneTable.
  • error: An error if any.

func (*RuneTable) AlignRightEdge

func (rt *RuneTable) AlignRightEdge() int

AlignRightEdge aligns the right edge of the table.

Returns:

  • int: The right most edge.

func (*RuneTable) AppendBottomRow

func (rt *RuneTable) AppendBottomRow(row []rune)

AppendBottomRow appends a row to the bottom of the table.

Parameters:

  • row: The row to append.

func (*RuneTable) GetRightMostEdge

func (rt *RuneTable) GetRightMostEdge() int

GetRightMostEdge gets the right most edge of the content.

Parameters:

  • content: The content.

Returns:

  • int: The right most edge.

func (*RuneTable) PrefixEachRow

func (rt *RuneTable) PrefixEachRow(prefix []rune)

PrefixEachRow prefixes each row with the given prefix.

Parameters:

  • prefix: The prefix to add to each row.

func (*RuneTable) PrependTopRow

func (rt *RuneTable) PrependTopRow(row []rune)

PrependTopRow prepends a row to the top of the table.

Parameters:

  • row: The row to prepend.

func (*RuneTable) String

func (rt *RuneTable) String() string

String implements the fmt.Stringer interface.

func (*RuneTable) SuffixEachRow

func (rt *RuneTable) SuffixEachRow(suffix []rune)

SuffixEachRow suffixes each row with the given suffix.

Parameters:

  • suffix: The suffix to add to each row.

type Stream

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

Stream is a character stream.

func NewStream

func NewStream(b []rune) *Stream

NewStream creates a new stream with the given runes.

Parameters:

  • b: The rune slice of the stream.

Returns:

  • *Stream: The new stream. Never nil.

func (*Stream) Accept

func (s *Stream) Accept()

Accept implements the CharStream interface.

func (*Stream) IsDone

func (s *Stream) IsDone() bool

IsDone implements the CharStream interface.

func (*Stream) Next

func (s *Stream) Next() (rune, bool)

Next implements the CharStream interface.

func (*Stream) Peek

func (s *Stream) Peek() (rune, bool)

Peek implements the CharStream interface.

func (*Stream) Refuse

func (s *Stream) Refuse() bool

Refuse implements the CharStream interface.

func (*Stream) RefuseMany

func (s *Stream) RefuseMany()

RefuseMany implements the CharStream interface.

type WordMatcher

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

WordMatcher is the word matcher.

func NewWordMatcher

func NewWordMatcher() *WordMatcher

NewWordMatcher returns a new WordMatcher.

Returns:

  • *WordMatcher: The new WordMatcher. Never nil.

func (*WordMatcher) AddWord

func (wm *WordMatcher) AddWord(word string) error

AddWord adds a word to the matcher. It ignores empty or duplicated words.

Parameters:

  • word: The word to add.

Returns:

  • error: An error if the word is invalid.

Errors:

  • *common.ErrAt: When the word is not a valid UTF-8 string.

func (*WordMatcher) Match

func (wm *WordMatcher) Match(is CharStream) (string, error)

Match matches the input stream.

Parameters:

  • is: The input stream to match.

Returns:

  • string: The matched word.
  • error: An error if the stream could not be matched.

Errors:

  • *common.ErrAt: If the input stream is not a valid UTF-8 stream.

Jump to

Keyboard shortcuts

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