bytes

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AndString

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

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

Parameters:

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

Returns:

  • string: The string representation of the values.

func EitherOrString

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

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

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(bytes.Fields([]byte("a b c"}, false) // "a, b or c"

func FindByte

func FindByte(data []byte, from int, sep byte) int

FindByte searches for the first occurrence of a byte in a byte slice starting from a given index.

Parameters:

  • data: the byte slice to search in.
  • from: the index to start the search from. If negative, it is treated as 0.
  • sep: the byte to search for.

Returns:

  • int: the index of the first occurrence of the byte in the byte slice, or -1 if not found.

func FindByteReversed

func FindByteReversed(data []byte, from int, sep byte) int

FindByteReversed searches for the first occurrence of a byte in a byte slice starting from a given index in reverse order.

Parameters:

  • data: the byte slice to search in.
  • from: the index to start the search from. If greater than or equal to the length of the byte slice, it is treated as the length of the byte slice minus 1.
  • sep: the byte to search for.

Returns:

  • int: the index of the first occurrence of the byte in the byte slice in reverse order, or -1 if not found.

func FindContentIndexes

func FindContentIndexes(op_token, cl_token []byte, tokens [][]byte) (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:

  • *luc.ErrInvalidParameter: If the 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 FirstIndex

func FirstIndex(tokens [][]byte, target []byte) int

FirstIndex returns the first index of the target in the tokens.

Parameters:

  • tokens: The slice of tokens in which to search for the target.
  • target: The target to search for.

Returns:

  • int: The index of the target. -1 if the target is not found.

If either tokens or the target are empty, it returns -1.

func ForwardSearch

func ForwardSearch(data []byte, from int, sep []byte) int

ForwardSearch searches for the first occurrence of a byte in a byte slice.

Parameters:

  • data: the byte slice to search in.
  • from: the index to start the search from. If negative, it is treated as 0.
  • sep: the byte to search for.

Returns:

  • int: the index of the first occurrence of the byte in the byte slice, or -1 if 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 OrString

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

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

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(bytes.Fields([]byte("a b c"), false, true) // "a, b, nor c"

func ReverseSearch

func ReverseSearch(data []byte, from int, sep []byte) int

ReverseSearch searches for the last occurrence of a byte in a byte slice.

Parameters:

  • data: the byte slice to search in.
  • from: the index to start the search from. If greater than or equal to the length of the byte slice, it is treated as the length of the byte slice minus 1.
  • sep: the byte to search for.

Returns:

  • int: the index of the last occurrence of the byte in the byte slice, or -1 if not found.

func TrimEmpty

func TrimEmpty(values [][]byte) [][]byte

TrimEmpty removes empty bytes from a slice of bytes; including any empty spaces at the beginning and end of the bytes.

Parameters:

  • values: The values to trim.

Returns:

  • [][]byte: The trimmed values.

Types

type ErrNeverOpened

type ErrNeverOpened struct {
	// OpeningToken is the opening token that was never closed.
	OpeningToken []byte

	// ClosingToken is the closing token that was found without a corresponding
	// opening token.
	ClosingToken []byte
}

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 []byte) *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 ErrTokenNotFound

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

	// 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 []byte, 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"

Jump to

Keyboard shortcuts

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