Replace byte occurrences between two byte delimiters.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Jose Quintana 636d5bdada
feat: add Go v1.13 support
3 weeks ago
.vscode chore: add .vscode 3 months ago
.gitignore feat: add go modules support 3 months ago
.travis.yml feat: add Go v1.13 support 3 weeks ago
LICENSE-APACHE feat: dual license support 3 weeks ago
LICENSE-MIT feat: dual license support 3 weeks ago
Makefile chore: update coverage task 3 months ago
README.md feat: dual license support 3 weeks ago
go.mod feat: add Go v1.13 support 3 weeks ago
redel.go feat: add random eof token 4 months ago
redel_test.go refactor: improve test to support an array of delimiters 4 months ago

README.md

Redel Build Status codecov Go Report Card GoDoc

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.

Supported Go versions

  • 1.10.3+
  • 1.11+

💡 For older versions, please use the latest v2 tag.

Usage

String replacement

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

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.

API

New

It creates a new Redel instance.

func New(reader io.Reader, delimiters []Delimiter) *Redel

Replace

Replace function replaces every occurrence with a custom replacement token.

func Replace(replacement []byte, mapFunc ReplacementMapFunc)

ReplaceFilter

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

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)

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 or issue.

License

This work is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

© 2017-present Jose Quintana