Documentation ¶
Index ¶
- func CopyWithFrames(src io.Reader, dst io.Writer, buf []byte, overlapSize int, ...) (written int64, err error)
- func InPlaceReplace(s, old, new []byte, n int) (err error)
- func InPlaceReplaceAll(s, old, new []byte) (err error)
- func ReplaceBytes(src io.Reader, dst io.Writer, old []byte, new []byte) (written int64, err error)
- func ReplaceStringsPrefix(source io.Reader, output io.Writer, values []string, old string, new string) (replacements int, matches map[string]struct{}, err error)
- type ErrNotSameLength
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 InPlaceReplaceAll ¶
func ReplaceBytes ¶
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
Click to show internal directories.
Click to hide internal directories.