Replace byte occurrences between two byte delimiters.
Replace byte occurrences between two byte delimiters.
Redel provides a small interface around bufio.Scanner for replace and filter byte occurrences between two byte delimiters. It supports an array of byte-pair replacements with a map and filter closures in order to control every replacement and their values.
💡 For older versions, please use the latest v2
tag.
package main
import (
"fmt"
"strings"
"github.com/joseluisq/redel/v3"
)
func main() {
// 1. Configure a Reader.
str := "Lorem ipsum dolor START nam risus END magna START suscipit. END varius START sapien END."
reader := strings.NewReader(str)
// 2. Intance Redel using a Reader and an array of byte delimiters.
rd := redel.New(reader, []redel.Delimiter{
// 2.1 Define here the byte delimiters which ones should be applied
{Start: []byte("START"), End: []byte("END")},
// Note that this byte-pair is not present in our example,
// so it will be not applied.
{Start: []byte("BEGIN"), End: []byte("END")},
})
// 3. Finally, define a byte replacement and then replace occurrences.
// Replace supports a closure which will be called for every scan-splitted token.
replacement := []byte("REPLACEMENT")
rd.Replace(replacement, func(data []byte, atEOF bool) {
// print out only for demonstration
fmt.Print(string(data))
})
// RESULT:
// Lorem ipsum dolor REPLACEMENT magna REPLACEMENT varius REPLACEMENT.⏎
}
package main
import (
"bufio"
"fmt"
"os"
"github.com/joseluisq/redel/v3"
)
func main() {
// 1. Configure a Reader.
reader, err := os.Open("my_big_file.txt")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer reader.Close()
f, err := os.Create("my_big_file_replaced.txt")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer f.Close()
var writer = bufio.NewWriter(f)
// 2. Intance Redel using a Reader and an array of byte delimiters.
replacement := []byte("REPLACEMENT")
rd := redel.New(reader, []redel.Delimiter{
// 2.1 Define here the byte delimiters which ones should be applied
{Start: []byte("START"), End: []byte("END")},
{Start: []byte("BEGIN"), End: []byte("END")},
})
// 3. Finally, define a byte replacement, replace occurrences and
// save every scan-splitted token to the file.
rd.Replace(replacement, func(data []byte, atEOF bool) {
_, err := writer.Write(data)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
})
writer.Flush()
}
More API examples can be found in redel_test.go file.
It creates a new Redel
instance.
func New(reader io.Reader, delimiters []Delimiter) *Redel
Replace
function replaces every occurrence with a custom replacement token.
func Replace(replacement []byte, mapFunc ReplacementMapFunc)
ReplaceFilter
function scans and replaces byte occurrences filtering every replacement value via a bool callback.
func ReplaceFilter(replacement []byte, mapFunc ReplacementMapFunc, filterFunc FilterValueFunc, preserveDelimiters bool)
ReplaceFilterWith
function scans and replaces byte occurrences filtering every matched replacement value and supporting a value callback in order to customize those values.
func ReplaceFilterWith(mapFunc ReplacementMapFunc, filterReplaceFunc FilterValueReplaceFunc, preserveDelimiters bool)
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in current work by you, as defined in the Apache-2.0 license, shall be dual licensed as described below, without any additional terms or conditions.
Feel free to send some Pull request or issue.
This work is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).
© 2017-present Jose Quintana