textreplace

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2021 License: MIT Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyWithFrames

func CopyWithFrames(src io.Reader, dst io.Writer, buf []byte, overlapSize int, transform func(b []byte) error) (
	written int64, err error)

CopyWithFrames copies data between two sources. As data is copied it is loaded into a byte buffer that overlaps with the previous buffer. This ensures that bytes of a certain width will not be split over the boundary of a frame.

Example
// We'd like to replace "dolor" with "fishy" in this text
lorem := []byte("Lorem ipsum dolor sit amet")

// With a buffer size of 15 we would split the input into
// []byte("Lorem ipsum dol") and []byte("or sit amet")
// and miss the opportunity to replace "dolor"
bufferSize := 15
expectedAnswer := bytes.ReplaceAll(lorem, []byte("dolor"), []byte("fishy"))
source := bytes.NewBuffer(lorem)

out := bytes.Buffer{}

// if we set an overlap size of 5 we'll ensure we see all length 5 segments of text
_, _ = CopyWithFrames(source, &out, make([]byte, bufferSize), 5, func(b []byte) error {
	fmt.Println(string(b))
	copy(b, bytes.ReplaceAll(b, []byte("dolor"), []byte("fishy")))
	return nil
})
fmt.Println(bytes.Equal(out.Bytes(), expectedAnswer))
Output:
Lorem ipsu
 ipsum dolor si
hy sit amety si
true

func InPlaceReplace

func InPlaceReplace(s, old, new []byte, n int) (err error)

func InPlaceReplaceAll

func InPlaceReplaceAll(s, old, new []byte) (err error)

func ReplaceBytes

func ReplaceBytes(src io.Reader, dst io.Writer, old []byte, new []byte) (written int64, err error)

func ReplaceStringsPrefix

func ReplaceStringsPrefix(source io.Reader, output io.Writer, values []string, old string, new string) (
	replacements int, matches map[string]struct{}, err error)

ReplaceStringsPrefix replaces the prefix of matching strings in a byte stream. All values to be replaced must have the same prefix and that prefix must be the same length as the new value.

Example
var output bytes.Buffer
_, _, _ = ReplaceStringsPrefix(
	bytes.NewBuffer([]byte(
		"something/nix/store/zziylsdvcqgwwwhbspf1agbz0vldxjr3-perl5.30.2-JSON-4.02something"),
	),
	&output,
	[]string{"/nix/store/zziylsdvcqgwwwhbspf1agbz0vldxjr3-perl5.30.2-JSON-4.02"},
	"/nix/store/",
	"/tmp/wings/",
)

fmt.Println(output.String())
Output:
something/tmp/wings/zziylsdvcqgwwwhbspf1agbz0vldxjr3-perl5.30.2-JSON-4.02something

Types

type ErrNotSameLength

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

func (ErrNotSameLength) Error

func (e ErrNotSameLength) Error() string

Jump to

Keyboard shortcuts

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