text: github.com/tenntenn/text/transform Index | Examples | Files

package transform

import "github.com/tenntenn/text/transform"

Index

Examples

Package Files

replacer.go

func ReplaceAll Uses

func ReplaceAll(t ReplaceTable) transform.Transformer

ReplaceAll creates transform.Transformer which is chained Replacers. The Replacers replace by replacing rule which is indicated by ReplaceTable.

type ReplaceByteTable Uses

type ReplaceByteTable [][]byte

ReplaceByteTable implements ReplaceTable. i*2 elements represents old, i*2+1 elements new for Replacer.

func (*ReplaceByteTable) Add Uses

func (t *ReplaceByteTable) Add(old, new []byte)

Add adds a new replacing rule.

func (ReplaceByteTable) At Uses

func (t ReplaceByteTable) At(i int) (old, new []byte)

At implements ReplaceTable.At.

func (ReplaceByteTable) Len Uses

func (t ReplaceByteTable) Len() int

Len implements ReplaceTable.Len.

type ReplaceHistory Uses

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

ReplaceHistory represents histories of replacing with Replacer.

func NewReplaceHistory Uses

func NewReplaceHistory() *ReplaceHistory

NewReplaceHistory creates a new ReplaceHistory.

func (*ReplaceHistory) At Uses

func (h *ReplaceHistory) At(index int) (src0, src1, dst0, dst1 int)

At returns a history of given index.

func (*ReplaceHistory) Iterate Uses

func (h *ReplaceHistory) Iterate(f func(src0, src1, dst0, dst1 int) bool)

Iterate iterates histories by replacing order. This method can call with a nil receiver. The arguments of f represent range of replacing, from src[src0:src1] to dst[dst0:dst1]. if f returns false Iterate will stop the iteration.

type ReplaceRuneTable Uses

type ReplaceRuneTable []rune

ReplaceRuneTable implements ReplaceTable. i*2 elements represents old, i*2+1 elements new for Replacer.

func (*ReplaceRuneTable) Add Uses

func (t *ReplaceRuneTable) Add(old, new rune)

Add adds a new replacing rule.

func (ReplaceRuneTable) At Uses

func (t ReplaceRuneTable) At(i int) (old, new []byte)

At implements ReplaceTable.At.

func (ReplaceRuneTable) Len Uses

func (t ReplaceRuneTable) Len() int

Len implements ReplaceTable.Len.

type ReplaceStringTable Uses

type ReplaceStringTable []string

ReplaceStringTable implements ReplaceTable. i*2 elements represents old, i*2+1 elements new for Replacer.

func (*ReplaceStringTable) Add Uses

func (t *ReplaceStringTable) Add(old, new string)

Add adds a new replacing rule.

func (ReplaceStringTable) At Uses

func (t ReplaceStringTable) At(i int) (old, new []byte)

At implements ReplaceTable.At.

func (ReplaceStringTable) Len Uses

func (t ReplaceStringTable) Len() int

Len implements ReplaceTable.Len.

type ReplaceTable Uses

type ReplaceTable interface {
    // At returns i-th replacing rule.
    At(i int) (old, new []byte)
    // Len returns the number of replacing rules.
    Len() int
}

ReplaceTable is used for ReplaceAll.

ExampleReplaceAll is an example of ReplaceAll.

Code:

t := ReplaceStringTable{
    "Hello", "Hi",
    "World", "Gophers",
}
r := transform.NewReader(strings.NewReader("Hello, World"), ReplaceAll(t))
io.Copy(os.Stdout, r)

Output:

Hi, Gophers

type Replacer Uses

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

Replacer replaces a part of byte data which matches given pattern to other pattern. It implements transform.Transformer.

func NewReplacer Uses

func NewReplacer(old, new []byte, history *ReplaceHistory) *Replacer

NewReplacer creates a new Replacer which replaces old to new. old and new are accepted nil and empty bytes ([]byte{}). if old is empty the Replacer does not replace and just copy src to dst.

If history is not nil, Replacer records histories of replacing.

func Replace Uses

func Replace(old, new []byte) *Replacer

Replace returns a Replacer with out history. It is a shorthand for NewReplacer(old, new, nil).

func ReplaceRune Uses

func ReplaceRune(old, new rune) *Replacer

ReplaceRune returns a Replacer which replaces given rune.

func ReplaceString Uses

func ReplaceString(old, new string) *Replacer

ReplaceString returns a Replacer which replaces given string.

func (*Replacer) Reset Uses

func (r *Replacer) Reset()

Reset implements transform.Transformer.Reset.

func (*Replacer) Transform Uses

func (r *Replacer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error)

Transform implements transform.Transformer.Transform. Transform replaces old to new in src and copy to dst.

Because the transforming is taken by part of source data with transform.Reader the Replacer is carefull for boundary of current src buffer and next one. When end of src matches for part of old and atEOF is false the Replacer stops to transform and remain len(src) % len(old) bytes for next transforming. If Replacer remained boundary bytes, nSrc will be less than len(src) and returns transform.ErrShortSrc.

Package transform imports 3 packages (graph). Updated 2017-05-24. Refresh now. Tools for package owners.