项目作者: joseluisq

项目描述 :
Replace byte occurrences between two byte delimiters.
高级语言: Go
项目地址: git://github.com/joseluisq/redel.git
创建时间: 2019-08-05T05:56:23Z
项目社区:https://github.com/joseluisq/redel

开源协议:Other

下载


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

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/joseluisq/redel/v3"
  6. )
  7. func main() {
  8. // 1. Configure a Reader.
  9. str := "Lorem ipsum dolor START nam risus END magna START suscipit. END varius START sapien END."
  10. reader := strings.NewReader(str)
  11. // 2. Intance Redel using a Reader and an array of byte delimiters.
  12. rd := redel.New(reader, []redel.Delimiter{
  13. // 2.1 Define here the byte delimiters which ones should be applied
  14. {Start: []byte("START"), End: []byte("END")},
  15. // Note that this byte-pair is not present in our example,
  16. // so it will be not applied.
  17. {Start: []byte("BEGIN"), End: []byte("END")},
  18. })
  19. // 3. Finally, define a byte replacement and then replace occurrences.
  20. // Replace supports a closure which will be called for every scan-splitted token.
  21. replacement := []byte("REPLACEMENT")
  22. rd.Replace(replacement, func(data []byte, atEOF bool) {
  23. // print out only for demonstration
  24. fmt.Print(string(data))
  25. })
  26. // RESULT:
  27. // Lorem ipsum dolor REPLACEMENT magna REPLACEMENT varius REPLACEMENT.⏎
  28. }

File replacement

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "github.com/joseluisq/redel/v3"
  7. )
  8. func main() {
  9. // 1. Configure a Reader.
  10. reader, err := os.Open("my_big_file.txt")
  11. if err != nil {
  12. fmt.Println(err)
  13. os.Exit(1)
  14. }
  15. defer reader.Close()
  16. f, err := os.Create("my_big_file_replaced.txt")
  17. if err != nil {
  18. fmt.Println(err)
  19. os.Exit(1)
  20. }
  21. defer f.Close()
  22. var writer = bufio.NewWriter(f)
  23. // 2. Intance Redel using a Reader and an array of byte delimiters.
  24. replacement := []byte("REPLACEMENT")
  25. rd := redel.New(reader, []redel.Delimiter{
  26. // 2.1 Define here the byte delimiters which ones should be applied
  27. {Start: []byte("START"), End: []byte("END")},
  28. {Start: []byte("BEGIN"), End: []byte("END")},
  29. })
  30. // 3. Finally, define a byte replacement, replace occurrences and
  31. // save every scan-splitted token to the file.
  32. rd.Replace(replacement, func(data []byte, atEOF bool) {
  33. _, err := writer.Write(data)
  34. if err != nil {
  35. fmt.Println(err)
  36. os.Exit(1)
  37. }
  38. })
  39. writer.Flush()
  40. }

More API examples can be found in redel_test.go file.

API

New

It creates a new Redel instance.

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

Replace

Replace function replaces every occurrence with a custom replacement token.

  1. func Replace(replacement []byte, mapFunc ReplacementMapFunc)

ReplaceFilter

ReplaceFilter function scans and replaces byte occurrences filtering every replacement value via a bool callback.

  1. 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.

  1. 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