transform

package
v0.11.17 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2015 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package transform provides reader and writer wrappers that transform the bytes passing through as well as various transformations. Example transformations provided by other packages include normalization and conversion between character sets.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrShortDst means that the destination buffer was too short to
	// receive all of the transformed bytes.
	ErrShortDst = errors.New("transform: short destination buffer")

	// ErrShortSrc means that the source buffer has insufficient data to
	// complete the transformation.
	ErrShortSrc = errors.New("transform: short source buffer")
)

Functions

func Bytes

func Bytes(t Transformer, b []byte) (result []byte, n int, err error)

Bytes returns a new byte slice with the result of converting b[:n] using t, where n <= len(b). If err == nil, n will be len(b). It calls Reset on t.

func String

func String(t Transformer, s string) (result string, n int, err error)

String returns a string with the result of converting s[:n] using t, where n <= len(s). If err == nil, n will be len(s). It calls Reset on t.

Types

type NopResetter

type NopResetter struct{}

NopResetter can be embedded by implementations of Transformer to add a nop Reset method.

func (NopResetter) Reset

func (NopResetter) Reset()

Reset implements the Reset method of the Transformer interface.

type Reader

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

Reader wraps another io.Reader by transforming the bytes read.

func NewReader

func NewReader(r io.Reader, t Transformer) *Reader

NewReader returns a new Reader that wraps r by transforming the bytes read via t. It calls Reset on t.

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

Read implements the io.Reader interface.

type Transformer

type Transformer interface {
	// Transform writes to dst the transformed bytes read from src, and
	// returns the number of dst bytes written and src bytes read. The
	// atEOF argument tells whether src represents the last bytes of the
	// input.
	//
	// Callers should always process the nDst bytes produced and account
	// for the nSrc bytes consumed before considering the error err.
	//
	// A nil error means that all of the transformed bytes (whether freshly
	// transformed from src or left over from previous Transform calls)
	// were written to dst. A nil error can be returned regardless of
	// whether atEOF is true. If err is nil then nSrc must equal len(src);
	// the converse is not necessarily true.
	//
	// ErrShortDst means that dst was too short to receive all of the
	// transformed bytes. ErrShortSrc means that src had insufficient data
	// to complete the transformation. If both conditions apply, then
	// either error may be returned. Other than the error conditions listed
	// here, implementations are free to report other errors that arise.
	Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error)

	// Reset resets the state and allows a Transformer to be reused.
	Reset()
}

Transformer transforms bytes.

var (
	// Discard is a Transformer for which all Transform calls succeed
	// by consuming all bytes and writing nothing.
	Discard Transformer = discard{}

	// Nop is a Transformer that copies src to dst.
	Nop Transformer = nop{}
)

func Chain

func Chain(t ...Transformer) Transformer

Chain returns a Transformer that applies t in sequence.

func RemoveFunc

func RemoveFunc(f func(r rune) bool) Transformer

RemoveFunc returns a Transformer that removes from the input all runes r for which f(r) is true. Illegal bytes in the input are replaced by RuneError.

Example
package main

import (
	"fmt"
	"unicode"

	"golang.org/x/text/transform"
	"golang.org/x/text/unicode/norm"
)

func main() {
	input := []byte(`tschüß; до свидания`)

	b := make([]byte, len(input))

	t := transform.RemoveFunc(unicode.IsSpace)
	n, _, _ := t.Transform(b, input, true)
	fmt.Println(string(b[:n]))

	t = transform.RemoveFunc(func(r rune) bool {
		return !unicode.Is(unicode.Latin, r)
	})
	n, _, _ = t.Transform(b, input, true)
	fmt.Println(string(b[:n]))

	n, _, _ = t.Transform(b, norm.NFD.Bytes(input), true)
	fmt.Println(string(b[:n]))

}
Output:

tschüß;досвидания
tschüß
tschuß

type Writer

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

Writer wraps another io.Writer by transforming the bytes read. The user needs to call Close to flush unwritten bytes that may be buffered.

func NewWriter

func NewWriter(w io.Writer, t Transformer) *Writer

NewWriter returns a new Writer that wraps w by transforming the bytes written via t. It calls Reset on t.

func (*Writer) Close

func (w *Writer) Close() error

Close implements the io.Closer interface.

func (*Writer) Write

func (w *Writer) Write(data []byte) (n int, err error)

Write implements the io.Writer interface. If there are not enough bytes available to complete a Transform, the bytes will be buffered for the next write. Call Close to convert the remaining bytes.

Jump to

Keyboard shortcuts

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