{"id":16038026,"url":"https://github.com/joseluisq/redel","last_synced_at":"2025-03-24T10:16:48.013Z","repository":{"id":57498608,"uuid":"200591203","full_name":"joseluisq/redel","owner":"joseluisq","description":"Replace byte occurrences between two byte delimiters.","archived":false,"fork":false,"pushed_at":"2019-10-17T21:24:13.000Z","size":55,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-22T14:23:23.810Z","etag":null,"topics":["byte-delimiters","golang-package","replacer"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joseluisq.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-05T05:56:23.000Z","updated_at":"2020-10-27T08:03:15.000Z","dependencies_parsed_at":"2022-09-06T17:10:57.266Z","dependency_job_id":null,"html_url":"https://github.com/joseluisq/redel","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseluisq%2Fredel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseluisq%2Fredel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseluisq%2Fredel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joseluisq%2Fredel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joseluisq","download_url":"https://codeload.github.com/joseluisq/redel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245196955,"owners_count":20576110,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["byte-delimiters","golang-package","replacer"],"created_at":"2024-10-08T22:23:06.100Z","updated_at":"2025-03-24T10:16:47.960Z","avatar_url":"https://github.com/joseluisq.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Redel [![Build Status](https://travis-ci.com/joseluisq/redel.svg?branch=master)](https://travis-ci.com/joseluisq/redel) [![codecov](https://codecov.io/gh/joseluisq/redel/branch/master/graph/badge.svg)](https://codecov.io/gh/joseluisq/redel) [![Go Report Card](https://goreportcard.com/badge/github.com/joseluisq/redel)](https://goreportcard.com/report/github.com/joseluisq/redel) [![GoDoc](https://godoc.org/github.com/joseluisq/redel?status.svg)](https://godoc.org/github.com/joseluisq/redel)\n\n\u003e Replace byte occurrences between two byte delimiters.\n\n__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.\n\n## Supported Go versions\n\n- 1.10.3+\n- 1.11+\n\n💡 For older versions, please use the latest `v2` tag.\n\n## Usage\n\n### String replacement\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\n\t\"github.com/joseluisq/redel/v3\"\n)\n\nfunc main() {\n\t// 1. Configure a Reader.\n\tstr := \"Lorem ipsum dolor START nam risus END magna START suscipit. END varius START sapien END.\"\n\treader := strings.NewReader(str)\n\n\t// 2. Intance Redel using a Reader and an array of byte delimiters.\n\trd := redel.New(reader, []redel.Delimiter{\n\t\t// 2.1 Define here the byte delimiters which ones should be applied\n\t\t{Start: []byte(\"START\"), End: []byte(\"END\")},\n\n\t\t// Note that this byte-pair is not present in our example,\n\t\t// so it will be not applied.\n\t\t{Start: []byte(\"BEGIN\"), End: []byte(\"END\")},\n\t})\n\n\t// 3. Finally, define a byte replacement and then replace occurrences.\n\t//    Replace supports a closure which will be called for every scan-splitted token.\n\treplacement := []byte(\"REPLACEMENT\")\n\trd.Replace(replacement, func(data []byte, atEOF bool) {\n\t\t// print out only for demonstration\n\t\tfmt.Print(string(data))\n\t})\n\n\t// RESULT:\n\t// Lorem ipsum dolor REPLACEMENT magna REPLACEMENT varius REPLACEMENT.⏎\n}\n```\n\n### File replacement\n\n```go\npackage main\n\nimport (\n\t\"bufio\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/joseluisq/redel/v3\"\n)\n\nfunc main() {\n\t// 1. Configure a Reader.\n\treader, err := os.Open(\"my_big_file.txt\")\n\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\tos.Exit(1)\n\t}\n\n\tdefer reader.Close()\n\n\tf, err := os.Create(\"my_big_file_replaced.txt\")\n\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\tos.Exit(1)\n\t}\n\n\tdefer f.Close()\n\n\tvar writer = bufio.NewWriter(f)\n\n\t// 2. Intance Redel using a Reader and an array of byte delimiters.\n\treplacement := []byte(\"REPLACEMENT\")\n\trd := redel.New(reader, []redel.Delimiter{\n\t\t// 2.1 Define here the byte delimiters which ones should be applied\n\t\t{Start: []byte(\"START\"), End: []byte(\"END\")},\n\t\t{Start: []byte(\"BEGIN\"), End: []byte(\"END\")},\n\t})\n\n\t// 3. Finally, define a byte replacement, replace occurrences and\n\t//    save every scan-splitted token to the file.\n\trd.Replace(replacement, func(data []byte, atEOF bool) {\n\t\t_, err := writer.Write(data)\n\n\t\tif err != nil {\n\t\t\tfmt.Println(err)\n\t\t\tos.Exit(1)\n\t\t}\n\t})\n\n\twriter.Flush()\n}\n```\n\nMore API examples can be found in [redel_test.go](./redel_test.go) file.\n\n## API\n\n### New\n\nIt creates a new `Redel` instance.\n\n```go\nfunc New(reader io.Reader, delimiters []Delimiter) *Redel\n```\n\n### Replace\n\n`Replace` function replaces every occurrence with a custom replacement token.\n\n```go\nfunc Replace(replacement []byte, mapFunc ReplacementMapFunc)\n```\n\n### ReplaceFilter\n\n`ReplaceFilter` function scans and replaces byte occurrences filtering every replacement value via a bool callback.\n\n```go\nfunc ReplaceFilter(replacement []byte, mapFunc ReplacementMapFunc, filterFunc FilterValueFunc, preserveDelimiters bool)\n```\n\n### ReplaceFilterWith\n\n`ReplaceFilterWith` function scans and replaces byte occurrences filtering every matched replacement value and supporting a value callback in order to customize those values.\n\n```go\nfunc ReplaceFilterWith(mapFunc ReplacementMapFunc, filterReplaceFunc FilterValueReplaceFunc, preserveDelimiters bool)\n```\n\n## Contributions\n\nUnless 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.\n\nFeel free to send some [Pull request](https://github.com/joseluisq/redel/pulls) or [issue](https://github.com/joseluisq/redel/issues).\n\n## License\n\nThis work is primarily distributed under the terms of both the [MIT license](LICENSE-MIT) and the [Apache License (Version 2.0)](LICENSE-APACHE).\n\n© 2017-present [Jose Quintana](http://git.io/joseluisq)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoseluisq%2Fredel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoseluisq%2Fredel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoseluisq%2Fredel/lists"}