replace

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2020 License: MIT Imports: 3 Imported by: 18

README

Streaming text replacement

GoDoc

This package provides a x/text/transform.Transformer implementation that replaces text

Example

package main

import (
	"io"
	"os"
	"regexp"

	"github.com/icholy/replace"
	"golang.org/x/text/transform"
)

func main() {
	f, _ := os.Open("file")
	defer f.Close()

	r := transform.NewReader(f, transform.Chain(
		// simple replace
		replace.String("foo", "bar"),
		replace.Bytes([]byte("thing"), []byte("test")),

		// remove all words that start with baz
		replace.Regexp(regexp.MustCompile(`baz\w*`), nil),

		// increment all numbers
		replace.RegexpStringFunc(regexp.MustCompile(`\d+`), func(match string) string {
			x, _ := strconv.Atoi(match)
			return strconv.Itoa(x+1)
		}),
	))

	_, _ = io.Copy(os.Stdout, r)
}

Notes Regexp* functions

  • The replace functions should not save or modify any []byte parameters they recieve.
  • For better performance, reduce the MaxSourceBuffer size to the largest possible match (Default 64kb).
  • If a match is longer than MaxSourceBuffer it may be skipped.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RegexpTransformer added in v0.2.0

type RegexpTransformer struct {
	// MaxSourceBuffer is the maximum size of the window used to search for the
	// regex match. (Default is 64kb).
	MaxSourceBuffer int
	// contains filtered or unexported fields
}

RegexpTransformer replaces regexp matches in a stream See: http://golang.org/x/text/transform

func Regexp added in v0.2.0

func Regexp(re *regexp.Regexp, new []byte) *RegexpTransformer

Regexp returns a transformer that replaces all matches of re with new

func RegexpFunc added in v0.2.0

func RegexpFunc(re *regexp.Regexp, replace func([]byte) []byte) *RegexpTransformer

RegexpFunc returns a transformer that replaces all matches of re with the result of calling replace with the match. The []byte parameter passed to replace should not be modified and is not guaranteed to be valid after the function returns.

func RegexpIndexFunc added in v0.2.2

func RegexpIndexFunc(re *regexp.Regexp, replace func(src []byte, index []int) []byte) *RegexpTransformer

RegexpIndexFunc returns a transformer that replaces all matches of re with the return value of replace. The replace function recieves the underlying src buffer and indexes into that buffer. The []byte parameter passed to replace should not be modified and is not guaranteed to be valid after the function returns.

func RegexpString added in v0.2.0

func RegexpString(re *regexp.Regexp, new string) *RegexpTransformer

RegexpString returns a transformer that replaces all matches of re with new

func RegexpStringFunc added in v0.2.0

func RegexpStringFunc(re *regexp.Regexp, replace func(string) string) *RegexpTransformer

RegexpStringFunc returns a transformer that replaces all matches of re with the result of calling replace with the match.

func RegexpStringSubmatchFunc added in v0.2.0

func RegexpStringSubmatchFunc(re *regexp.Regexp, replace func([]string) string) *RegexpTransformer

RegexpStringSubmatchFunc returns a transformer that replaces all matches of re with the result of calling replace with the submatch.

func RegexpSubmatchFunc added in v0.2.0

func RegexpSubmatchFunc(re *regexp.Regexp, replace func([][]byte) []byte) *RegexpTransformer

RegexpSubmatchFunc returns a transformer that replaces all matches of re with the result of calling replace with the submatch. The [][]byte parameter passed to replace should not be modified and is not guaranteed to be valid after the function returns.

func (*RegexpTransformer) Reset added in v0.3.1

func (t *RegexpTransformer) Reset()

Reset resets the state and allows a Transformer to be reused.

func (*RegexpTransformer) Transform added in v0.2.0

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

Transform implements golang.org/x/text/transform#Transformer

type Transformer

type Transformer struct {
	transform.NopResetter
	// contains filtered or unexported fields
}

Transformer replaces text in a stream See: http://golang.org/x/text/transform

func Bytes

func Bytes(old, new []byte) Transformer

Bytes returns a transformer that replaces all instances of old with new. Unlike bytes.Replace, empty old values don't match anything.

func String

func String(old, new string) Transformer

String returns a transformer that replaces all instances of old with new. Unlike strings.Replace, empty old values don't match anything.

func (Transformer) Transform

func (t Transformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error)

Transform implements golang.org/x/text/transform#Transformer

Jump to

Keyboard shortcuts

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