runes

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: MIT Imports: 3 Imported by: 1

README

This is a simplistic package for replicating some of the functionalities of the strings package while operation explicitly with slices of runes. In fact, the internals utilize the strings package quite a bit. 

The strings package operates on strings and the indexing is difficult to reason about when dealing with non-ASCII chracters because the index value becomes difficult to interpret for a human. 


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(s, substr Runes) bool

Contains returns true if substr occurs in s.

func ContainsAny

func ContainsAny(s, substr Runes) bool

ContainsAny returns true if any rune in substr occurs in s.

func Equal

func Equal(s1, s2 Runes) bool

Equal returns true if s1 and s2 are identical.

func HasPrefix

func HasPrefix(r Runes, pre Runes) bool

HasPrefix returns true if r starts with pre.

func HasSuffix

func HasSuffix(s, suffix Runes) bool

HasSuffix returns true if s has suffix.

func Index

func Index(r Runes, s Runes) int

Index returns the starting index of s within r. Returns -1 if s is not contained in r.

func IndexAny

func IndexAny(r Runes, s Runes) int

IndexAny returns the first index of any of the runes appearing in s.

func IndexFunc

func IndexFunc(r Runes, f func(rune) bool) int

IndexFunc returns the index of first rune in r that satisfies condition given by f.

func LastIndex

func LastIndex(r, s Runes) int

LastIndex returns the idenx of the last instance of s within r.

func LastIndexAny

func LastIndexAny(r Runes, s Runes) int

LastIndexAny returns the last index of any of the runes appearing in s.

func LastIndexFunc

func LastIndexFunc(r Runes, f func(rune) bool) int

LastIndexFunc returns the index of first rune in r that satisfies condition given by f.

Types

type Builder

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

Builder is for building Runes.

func NewBuilder

func NewBuilder() *Builder

NewBuilder returns a pointer to a new builder.

func (*Builder) Bytes

func (b *Builder) Bytes() []byte

Bytes returns the content of b in bytes.

func (*Builder) Reset

func (b *Builder) Reset()

Reset resets b.

func (*Builder) Runes

func (b *Builder) Runes() Runes

Runes reads out the accumulated runes.

func (*Builder) String

func (b *Builder) String() string

String implements the Stringer interface.

func (*Builder) Write

func (b *Builder) Write(p []byte) (n int, err error)

Write implements the io.Writer interface

func (*Builder) WriteRunes

func (b *Builder) WriteRunes(r Runes) (n int, err error)

WriteRunes appends r to b. Returns the length of n and error (currently always nil).

func (*Builder) WriteString

func (b *Builder) WriteString(s string) (n int, err error)

WriteString appends s to b by first converting it to Runes. Returns the number of runes written and error (always nil at the moment).

type Reader

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

Reader is an io.Reader reading from s.

func NewReader

func NewReader(s Runes) *Reader

NewReader returns a new Reader that reads from s.

func (*Reader) Read

func (r *Reader) Read(b []byte) (n int, err error)

Read implements io.Reader interface. It reads one rune at a time so don't expect to be able to read a sequence of single bytes. Most of the time, you probaly want ReadRune.

func (*Reader) ReadRune

func (r *Reader) ReadRune() (ch rune, size int, err error)

ReadRune implements io.RuneReader interface. For compatibility only. Most of the time, you probaly want ReadRune.

func (*Reader) ReadRuneAt

func (r *Reader) ReadRuneAt(off int) (ch rune, size int, err error)

ReadRuneAt returns rune at r[off]. If off is outside the range of r, returns appropriate error.

func (*Reader) Seek

func (r *Reader) Seek(offset int, whence int) (int, error)

Seek implements the io.Seeker interface.

func (*Reader) Size

func (r *Reader) Size() int64

Size returns the size of the underlying Runes.

type Runes

type Runes []rune

Runes is an alias for []rune.

func Chunk

func Chunk(r Runes, n int) (out []Runes)

Chunk splits r into a Runes of length at most n and returns the result as []Runes

func Fields

func Fields(src Runes) (out []Runes)

Fields splits src into fields where fields are separated by white spaces.

func Join

func Join(slice []Runes, sep Runes) (out Runes)

Join joins the runes in slice using sep as separator. Note that sep is a string.

func Map

func Map(mapping func(rune) rune, s Runes) Runes

Map transforms s according to mapping.

func Replace

func Replace(src, matchString, replaceString Runes, n int) (out Runes)

Replace replaces the first n instances of matchString in src with replaceString. If n<0, it replaces all. Returns src if n=0.

func ReplaceAll

func ReplaceAll(src, matchString, replaceString Runes) (out Runes)

ReplaceAll replaces all instances of matchString in src with replaceString.

func ReplaceBlocks

func ReplaceBlocks(src, first, last, replaceString Runes, n int) (out Runes)

ReplaceBlocks replaces the first n blocks defined by first-last pair by replaceString. If n=-1, all are replaced. If n=0, returns src.

func ReplaceBlocksAll

func ReplaceBlocksAll(src, first, last, replaceString Runes) (out Runes)

ReplaceBlocksAll replaces all blocks.

func ReplaceBlocksFunc

func ReplaceBlocksFunc(src, first, last Runes, handler func(Runes) Runes, n int) (out Runes)

ReplaceBlocksFunc replaces the content of blocks defined by first and last with the output of handler.

func ReplaceBlocksFuncAll

func ReplaceBlocksFuncAll(src, first, last Runes, handler func(Runes) Runes) (out Runes)

ReplaceBlocksFuncAll replaces all blocks with output of handler.

func Split

func Split(r Runes, sep Runes) (out []Runes)

Split splits r into a sub-slices using sep as the separator.

func SplitIntoBlocks

func SplitIntoBlocks(src, first, last Runes) (out []Runes)

SplitIntoBlocks splits src into two types of chunks: those that match the block as defined by first and last, and those that do not. The result is returned as a slice of Runes. A block will always terminate at the first instance of last so none of the block related functions in this package will work with nested blocks.

func TrimFunc

func TrimFunc(s Runes, f func(rune) bool) Runes

TrimFunc trims s according to f.

func TrimSpace

func TrimSpace(r Runes) Runes

TrimSpace removes all leading and trailing white spaces.

func (Runes) Bytes

func (r Runes) Bytes() []byte

Bytes returns r as slice of bytes.

func (Runes) String

func (r Runes) String() string

String returns the content of r as a string. Implements stringer interface.

Jump to

Keyboard shortcuts

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