runes

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: BSD-3-Clause Imports: 4 Imported by: 1

README

godoc codecov Go Report Card

The runes package contains modified versions of bytes.Reader and strings.Reader. This package also includes a new runes.Reader type, based on the bytes.Reader code, and modified to operate on a rune slice instead of a byte slice.

Google Inc has no affiliation with, and does not promote anything related to any of Go-CoreLibs, Go-Curses or Go-Enjin projects.

runes.RuneReader

runes.RuneReader includes the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker, io.ByteScanner and io.RuneScanner interfaces and the following additional methods:

  • Standard methods not present in any of the io interfaces:
    • Len() int
    • Size() int64
  • Methods not present in the Go standard library:
    • ReadRuneAt(index int64) (ch rune, size int, err error) seeks to the index given and reads the rune at that position
    • ReadPrevRuneFrom(index int64) (ch rune, size int, err error) seeks to the index given and reads the rune previous to that position
    • ReadNextRuneFrom(index int64) (ch rune, size int, err error) seeks to the index given and reads the rune after to that position
    • ReadRuneSlice(index, count int64) (slice []rune, size int, err error) seeks to the index given, and starts accumulating runes, up to the count requested, returning a slice and the total size. For byte and string readers, size is the number of bytes in the rune slice. For the rune reader, the size is always 1, which works because the underlying data type is just a slice of runes (no decoding of multibyte sizes needed)

runes.Reader

This implementation is a modified version of the bytes.Reader type, using a []rune slice instead of a []byte slice and supporting the runes.RuneReader additional methods.

runes.BytesReader and runes.StringReader

These implementations are copies of the standard bytes.Reader and strings.Reader standard library types, included here so that the additional methods of the runes.RuneReader interface could be implemented.

Benchmarks

goos: linux
goarch: arm64
pkg: github.com/go-corelibs/runes
                  │       bytes       │                   string                    │                runes                │
                  │      sec/op       │      sec/op        vs base                  │      sec/op        vs base          │
ReadRuneAt          0.000019400n ± 0%   0.000019400n ± 1%    0.00% (p=0.000 n=1000)   0.000009300n ± 0%  -52.06% (n=1000)
ReadPrevRuneFrom    0.000019900n ± 0%   0.000019900n ± 0%    0.00% (p=0.000 n=1000)   0.000009700n ± 0%  -51.26% (n=1000)
ReadNextRuneFrom    0.000021000n ± 0%   0.000021000n ± 0%        ~ (p=0.264 n=1000)   0.000009300n ± 0%  -55.71% (n=1000)
ReadSliceRuneFrom    0.00005900n ± 1%    0.00006540n ± 1%  +10.85% (p=0.000 n=1000)    0.00005720n ± 1%   -3.05% (n=1000)
geomean              0.00002630n         0.00002698n        +2.61%                     0.00001480n       -43.72%

The columns are comparing the runes.BytesReader, runes.StringReader and runes.Reader types. All benchmark data is included in the testdata/bench subdirectory and can be re-run using make benchmark and then make benchstats to get the report.

Installation

> go get github.com/go-corelibs/runes@latest

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

Licenses

Google does not endorse, promote or sponsor Go-Corelibs, Go-Curses or Go-Enjin in any way. The source code present in this repository complies with all licensing requirements

runes.Reader and runes.BytesReader

Copyright 2012 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE_GO_BYTES
file.

runes.StringReader

Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE_GO_STRINGS
file.

runes.RuneReader and all methods added to the reader implementations

Copyright 2024 The Go-CoreLibs Authors. All rights reserved.
Use of this source code is governed by a BSD-style license
that can be found in the LICENSE file.

Documentation

Overview

Package runes provides rune related utilities

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BytesReader

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

A BytesReader implements the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker, io.ByteScanner, and io.RuneScanner interfaces by reading from a byte slice. Unlike a [Buffer], a BytesReader is read-only and supports seeking. The zero value for BytesReader operates like a BytesReader of an empty slice.

func NewBytesReader

func NewBytesReader(b []byte) *BytesReader

NewBytesReader returns a new [BytesReader.BytesReader] reading from b.

func (*BytesReader) Len

func (r *BytesReader) Len() int

Len returns the number of bytes of the unread portion of the slice.

func (*BytesReader) Read

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

Read implements the io.Reader interface.

func (*BytesReader) ReadAt

func (r *BytesReader) ReadAt(b []byte, off int64) (n int, err error)

ReadAt implements the io.ReaderAt interface.

func (*BytesReader) ReadByte

func (r *BytesReader) ReadByte() (byte, error)

ReadByte implements the io.ByteReader interface.

func (*BytesReader) ReadByteSlice added in v1.1.0

func (r *BytesReader) ReadByteSlice(index, count int64) (slice []byte, err error)

func (*BytesReader) ReadNextRuneFrom

func (r *BytesReader) ReadNextRuneFrom(index int64) (ch rune, size int, err error)

ReadNextRuneFrom is a convenience method combining Seek and ReadRune into one operation. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart)

ReadNextRuneFrom was added by go-corelibs

func (*BytesReader) ReadPrevRuneFrom

func (r *BytesReader) ReadPrevRuneFrom(index int64) (ch rune, size int, err error)

ReadPrevRuneFrom is a convenience method combining Seek and ReadRune into one operation. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart)

ReadPrevRuneFrom was added by go-corelibs

func (*BytesReader) ReadRune

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

ReadRune implements the io.RuneReader interface.

func (*BytesReader) ReadRuneAt

func (r *BytesReader) ReadRuneAt(index int64) (ch rune, size int, err error)

ReadRuneAt is a convenience method combining Seek and ReadRune into one operation. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart)

ReadRuneAt was added by go-corelibs

func (*BytesReader) ReadRuneSlice

func (r *BytesReader) ReadRuneSlice(index, count int64) (slice []rune, size int, err error)

ReadRuneSlice is a convenience method combining Seek and then ReadRune operations accumulating the requested count of runes, starting at the index given. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart). The count argument is exclusive, meaning start at the index and stop at index+count, equivalent to the slice index syntax of bytes[index:index+count]

ReadRuneSlice was added by go-corelibs

func (*BytesReader) ReadString added in v1.1.0

func (r *BytesReader) ReadString(index, count int64) (slice string, err error)

func (*BytesReader) Reset

func (r *BytesReader) Reset(b []byte)

Reset resets the [BytesReader.BytesReader] to be reading from b.

func (*BytesReader) Seek

func (r *BytesReader) Seek(offset int64, whence int) (int64, error)

Seek implements the io.Seeker interface.

func (*BytesReader) Size

func (r *BytesReader) Size() int64

Size returns the original length of the underlying byte slice. Size is the number of bytes available for reading via BytesReader.ReadAt. The result is unaffected by any method calls except BytesReader.Reset.

func (*BytesReader) UnreadByte

func (r *BytesReader) UnreadByte() error

UnreadByte complements BytesReader.ReadByte in implementing the io.ByteScanner interface.

func (*BytesReader) UnreadRune

func (r *BytesReader) UnreadRune() error

UnreadRune complements BytesReader.ReadRune in implementing the io.RuneScanner interface.

func (*BytesReader) WriteTo

func (r *BytesReader) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements the io.WriterTo interface.

type Reader

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

A Reader implements the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker, io.ByteScanner, and io.RuneScanner interfaces by reading from a rune slice. Unlike a [Buffer], a Reader is read-only and supports seeking. The zero value for Reader operates like a Reader of an empty slice.

func NewRunesReader

func NewRunesReader(runes []rune) *Reader

NewRunesReader returns a new [Reader.Reader] reading from b.

func (*Reader) Len

func (r *Reader) Len() int

Len returns the number of bytes of the unread portion of the slice.

func (*Reader) Read

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

Read implements the io.Reader interface.

func (*Reader) ReadAt

func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)

ReadAt implements the io.ReaderAt interface.

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

ReadByte implements the io.ByteReader interface.

func (*Reader) ReadByteSlice added in v1.1.0

func (r *Reader) ReadByteSlice(index, count int64) (slice []byte, err error)

func (*Reader) ReadNextRuneFrom

func (r *Reader) ReadNextRuneFrom(index int64) (ch rune, size int, err error)

ReadNextRuneFrom is a convenience method combining Seek and ReadRune into one operation. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart)

ReadNextRuneFrom was added by go-corelibs

func (*Reader) ReadPrevRuneFrom

func (r *Reader) ReadPrevRuneFrom(index int64) (ch rune, size int, err error)

ReadPrevRuneFrom is a convenience method combining Seek and ReadRune into one operation. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart)

ReadPrevRuneFrom was added by go-corelibs

func (*Reader) ReadRune

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

ReadRune implements the io.RuneReader interface.

func (*Reader) ReadRuneAt

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

ReadRuneAt is a convenience method combining Seek and ReadRune into one operation. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart)

ReadRuneAt was added by go-corelibs

func (*Reader) ReadRuneSlice

func (r *Reader) ReadRuneSlice(index, count int64) (slice []rune, size int, err error)

ReadRuneSlice is a convenience method combining Seek and then ReadRune operations accumulating the requested count of runes, starting at the index given. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart). The count argument is exclusive, meaning start at the index and stop at index+count, equivalent to the slice index syntax of bytes[index:index+count]

The size returned is the number of runes returned, not the number of bytes.

ReadRuneSlice was added by go-corelibs

func (*Reader) ReadString added in v1.1.0

func (r *Reader) ReadString(index, count int64) (slice string, err error)

func (*Reader) Reset

func (r *Reader) Reset(runes []rune)

Reset resets the Reader to be reading from b.

func (*Reader) Seek

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

Seek implements the io.Seeker interface.

func (*Reader) Size

func (r *Reader) Size() int64

Size returns the original length of the underlying byte slice. Size is the number of bytes available for reading via Reader.ReadAt. The result is unaffected by any method calls except Reader.Reset.

func (*Reader) UnreadByte

func (r *Reader) UnreadByte() error

UnreadByte complements Reader.ReadByte in implementing the io.ByteScanner interface.

func (*Reader) UnreadRune

func (r *Reader) UnreadRune() error

UnreadRune complements Reader.ReadRune in implementing the io.RuneScanner interface.

func (*Reader) WriteTo

func (r *Reader) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements the io.WriterTo interface.

type RuneReader

type RuneReader interface {
	io.Reader
	io.ReaderAt
	io.WriterTo
	io.Seeker
	io.ByteScanner
	io.RuneScanner

	// Len returns the byte length of the underlying memory
	Len() int

	// Size returns the length of the underlying slice
	Size() int64

	// ReadRuneAt seeks to the index given and reads the rune at that position
	ReadRuneAt(index int64) (ch rune, size int, err error)

	// ReadPrevRuneFrom seeks to the index given and reads the rune previous to
	// that position
	ReadPrevRuneFrom(index int64) (ch rune, size int, err error)

	// ReadNextRuneFrom seeks to the index given and reads the next rune after
	// that position
	ReadNextRuneFrom(index int64) (ch rune, size int, err error)

	// ReadRuneSlice seeks to the index given, and starts accumulating runes,
	// up to the count requested, returning a slice and the total size
	//
	// For byte and string readers, size is the number of bytes in the rune
	// slice. For the rune reader, the size is always 1, which works because the
	// underlying data type is just a slice of runes (no decoding of multi-bytes
	// needed)
	ReadRuneSlice(index, count int64) (slice []rune, size int, err error)

	// ReadByteSlice is like ReadRuneSlice, but for byte slices
	ReadByteSlice(index, count int64) (slice []byte, err error)

	// ReadString is like ReadRuneSlice, but for a string
	ReadString(index, count int64) (slice string, err error)
}

RuneReader defines the interface for additional rune-specific features when reading data from a string, bytes or rune slices

func NewRuneReader

func NewRuneReader[V []rune | []byte | string](input V) (rb RuneReader)

NewRuneReader is a generic wrapper around constructing a NewBytesReader, NewStringReader or NewRunesReader depending on the input type given and returned as a RuneReader

type StringReader

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

A StringReader implements the io.Reader, io.ReaderAt, io.ByteReader, io.ByteScanner, io.RuneReader, io.RuneScanner, io.Seeker, and io.WriterTo interfaces by reading from a string. The zero value for StringReader operates like a StringReader of an empty string.

func NewStringReader

func NewStringReader(s string) *StringReader

NewStringReader returns a new StringReader reading from s. It is similar to bytes.NewBufferString but more efficient and non-writable.

func (*StringReader) Len

func (r *StringReader) Len() int

Len returns the number of bytes of the unread portion of the string.

func (*StringReader) Read

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

Read implements the io.Reader interface.

func (*StringReader) ReadAt

func (r *StringReader) ReadAt(b []byte, off int64) (n int, err error)

ReadAt implements the io.ReaderAt interface.

func (*StringReader) ReadByte

func (r *StringReader) ReadByte() (byte, error)

ReadByte implements the io.ByteReader interface.

func (*StringReader) ReadByteSlice added in v1.1.0

func (r *StringReader) ReadByteSlice(index, count int64) (slice []byte, err error)

func (*StringReader) ReadNextRuneFrom

func (r *StringReader) ReadNextRuneFrom(index int64) (ch rune, size int, err error)

ReadNextRuneFrom is a convenience method combining Seek and ReadRune into one operation. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart)

ReadNextRuneFrom was added by go-corelibs

func (*StringReader) ReadPrevRuneFrom

func (r *StringReader) ReadPrevRuneFrom(index int64) (ch rune, size int, err error)

ReadPrevRuneFrom is a convenience method combining Seek and ReadRune into one operation. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart)

ReadPrevRuneFrom was added by go-corelibs

func (*StringReader) ReadRune

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

ReadRune implements the io.RuneReader interface.

func (*StringReader) ReadRuneAt

func (r *StringReader) ReadRuneAt(index int64) (ch rune, size int, err error)

ReadRuneAt is a convenience method combining Seek and ReadRune into one operation. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart)

ReadRuneAt was added by go-corelibs

func (*StringReader) ReadRuneSlice

func (r *StringReader) ReadRuneSlice(index, count int64) (slice []rune, size int, err error)

ReadRuneSlice is a convenience method combining Seek and then ReadRune operations accumulating the requested count of runes, starting at the index given. The index argument is always relative to the start of the slice, equivalent to Seek(index, io.SeekStart). The count argument is exclusive, meaning start at the index and stop at index+count, equivalent to the slice index syntax of bytes[index:index+count]

ReadRuneSlice was added by go-corelibs

func (*StringReader) ReadString added in v1.1.0

func (r *StringReader) ReadString(index, count int64) (slice string, err error)

func (*StringReader) Reset

func (r *StringReader) Reset(s string)

Reset resets the StringReader to be reading from s.

func (*StringReader) Seek

func (r *StringReader) Seek(offset int64, whence int) (int64, error)

Seek implements the io.Seeker interface.

func (*StringReader) Size

func (r *StringReader) Size() int64

Size returns the original length of the underlying string. Size is the number of bytes available for reading via StringReader.ReadAt. The returned value is always the same and is not affected by calls to any other method.

func (*StringReader) UnreadByte

func (r *StringReader) UnreadByte() error

UnreadByte implements the io.ByteScanner interface.

func (*StringReader) UnreadRune

func (r *StringReader) UnreadRune() error

UnreadRune implements the io.RuneScanner interface.

func (*StringReader) WriteTo

func (r *StringReader) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements the io.WriterTo interface.

Jump to

Keyboard shortcuts

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