https://github.com/joseluisq/redel
Replace byte occurrences between two byte delimiters.
https://github.com/joseluisq/redel
byte-delimiters golang-package replacer
Last synced: over 1 year ago
JSON representation
Replace byte occurrences between two byte delimiters.
- Host: GitHub
- URL: https://github.com/joseluisq/redel
- Owner: joseluisq
- License: apache-2.0
- Created: 2019-08-05T05:56:23.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-10-17T21:24:13.000Z (over 6 years ago)
- Last Synced: 2024-04-22T14:23:23.810Z (about 2 years ago)
- Topics: byte-delimiters, golang-package, replacer
- Language: Go
- Homepage:
- Size: 53.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Redel [](https://travis-ci.com/joseluisq/redel) [](https://codecov.io/gh/joseluisq/redel) [](https://goreportcard.com/report/github.com/joseluisq/redel) [](https://godoc.org/github.com/joseluisq/redel)
> Replace byte occurrences between two byte delimiters.
__Redel__ provides a small interface around [bufio.Scanner](https://golang.org/pkg/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.
## Supported Go versions
- 1.10.3+
- 1.11+
💡 For older versions, please use the latest `v2` tag.
## Usage
### String replacement
```go
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.⏎
}
```
### File replacement
```go
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](./redel_test.go) file.
## API
### New
It creates a new `Redel` instance.
```go
func New(reader io.Reader, delimiters []Delimiter) *Redel
```
### Replace
`Replace` function replaces every occurrence with a custom replacement token.
```go
func Replace(replacement []byte, mapFunc ReplacementMapFunc)
```
### ReplaceFilter
`ReplaceFilter` function scans and replaces byte occurrences filtering every replacement value via a bool callback.
```go
func ReplaceFilter(replacement []byte, mapFunc ReplacementMapFunc, filterFunc FilterValueFunc, preserveDelimiters bool)
```
### ReplaceFilterWith
`ReplaceFilterWith` function scans and replaces byte occurrences filtering every matched replacement value and supporting a value callback in order to customize those values.
```go
func ReplaceFilterWith(mapFunc ReplacementMapFunc, filterReplaceFunc FilterValueReplaceFunc, preserveDelimiters bool)
```
## Contributions
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](https://github.com/joseluisq/redel/pulls) or [issue](https://github.com/joseluisq/redel/issues).
## License
This work is primarily distributed under the terms of both the [MIT license](LICENSE-MIT) and the [Apache License (Version 2.0)](LICENSE-APACHE).
© 2017-present [Jose Quintana](http://git.io/joseluisq)