Giter Club home page Giter Club logo

redel's Introduction

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

redel's People

Contributors

joseluisq avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.