transform

package module
v0.0.0-...-ba83649 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: MIT Imports: 3 Imported by: 1

README

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReplaceAll

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.

Types

type ReplaceByteTable

type ReplaceByteTable [][]byte

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

func (*ReplaceByteTable) Add

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

Add adds a new replacing rule.

func (ReplaceByteTable) At

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

At implements ReplaceTable.At.

func (ReplaceByteTable) Len

func (t ReplaceByteTable) Len() int

Len implements ReplaceTable.Len.

type ReplaceHistory

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

ReplaceHistory represents histories of replacing with Replacer.

func NewReplaceHistory

func NewReplaceHistory() *ReplaceHistory

NewReplaceHistory creates a new ReplaceHistory.

func (*ReplaceHistory) At

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

At returns a history of given index.

func (*ReplaceHistory) Iterate

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

type ReplaceRuneTable []rune

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

func (*ReplaceRuneTable) Add

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

Add adds a new replacing rule.

func (ReplaceRuneTable) At

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

At implements ReplaceTable.At.

func (ReplaceRuneTable) Len

func (t ReplaceRuneTable) Len() int

Len implements ReplaceTable.Len.

type ReplaceStringTable

type ReplaceStringTable []string

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

func (*ReplaceStringTable) Add

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

Add adds a new replacing rule.

func (ReplaceStringTable) At

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

At implements ReplaceTable.At.

func (ReplaceStringTable) Len

func (t ReplaceStringTable) Len() int

Len implements ReplaceTable.Len.

type ReplaceTable

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.

Example

ExampleReplaceAll is an example of ReplaceAll.

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

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

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

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

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

func ReplaceRune

func ReplaceRune(old, new rune) *Replacer

ReplaceRune returns a Replacer which replaces given rune.

func ReplaceString

func ReplaceString(old, new string) *Replacer

ReplaceString returns a Replacer which replaces given string.

func (*Replacer) Reset

func (r *Replacer) Reset()

Reset implements transform.Transformer.Reset.

func (*Replacer) Transform

func (r *Replacer) Transform(dst, src []byte, atEOF bool) (int, int, 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 the matched bytes for next transforming. If Replacer remained boundary bytes, nSrc will be less than len(src) and returns transform.ErrShortSrc.

Jump to

Keyboard shortcuts

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