https://github.com/jakebailey/secreplace
Go library for replacing sections of strings
https://github.com/jakebailey/secreplace
go golang
Last synced: 7 months ago
JSON representation
Go library for replacing sections of strings
- Host: GitHub
- URL: https://github.com/jakebailey/secreplace
- Owner: jakebailey
- License: mit
- Created: 2018-10-07T00:17:54.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-07T19:19:36.000Z (over 7 years ago)
- Last Synced: 2025-03-28T04:23:10.515Z (about 1 year ago)
- Topics: go, golang
- Language: Go
- Size: 15.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# secreplace
[](http://godoc.org/github.com/jakebailey/secreplace) [](https://goreportcard.com/report/github.com/jakebailey/secreplace) [](https://travis-ci.com/jakebailey/secreplace) [](https://coveralls.io/github/jakebailey/secreplace?branch=master)
`secreplace` is a library which aids in the string replacement of "sections".
A section is defined as text enclosed by open and close terminators.
For example, take the following string:
```
Hi, my name is (_NAME_)!
```
`(_` and `_)` are the open and close terminators, respectively. We'd like
to replace `(_NAME_)` with something else. Let's pick a function and run
`ReplaceOne`:
```go
replacer := func(s string) (string, error) {
return "COOL-"+s, nil
}
out, changed, err := ReplaceOne("Hi, my name is (_NAME_)!", "(_", "_)", replacer)
// out == "Hi, my name is COOL-NAME!"
// changed == true
// err == nil
```
The `ReplaceAll` function will repeatedly call `ReplaceOne` until no more
replacements can occur. This allows for nesting, for example:
```go
replacer := func(s string) (string, error) {
return "COOL"+s, nil
}
out, changed, err := ReplaceAll("Hi, my name is (_(_A_)-(_B_)_)!", "(_", "_)", replacer)
// out == "Hi, my name is COOL-COOL-A-COOL-B!"
// changed == true
// err == nil
```
Any errors returned by the replacement function will be returned by
`ReplaceOne` and `ReplaceAll`. Any unmatched terminators will be caught and
also cause errors to be returned.