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.
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.
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 ¶
Replace returns a Replacer with out history. It is a shorthand for NewReplacer(old, new, nil).
func ReplaceRune ¶
ReplaceRune returns a Replacer which replaces given rune.
func ReplaceString ¶
ReplaceString returns a Replacer which replaces given string.
func (*Replacer) Transform ¶
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.