# README
replace - text replacement utilities
Installation
> go get github.com/go-corelibs/replace@latest
Examples
String, StringInsensitive, StringPreserve
var contents := `First line of text says something.
Text on the second line says more.`
func main() {
// replace "text" (case-sensitively) with "this"
modified, count := replace.String("text", "this", contents)
// count == 1
// modified == "First line of this says something.\nText on the second line says more."
// replace "text" (case-insensitively) with "this"
modified, count := replace.StringInsensitive("text", "this", contents)
// count == 2
// modified == "First line of this says something.\nthis on the second line says more."
// replace "text" (case-preserved) with "this"
modified, count := replace.StringPreserve("text", "this", contents)
// count == 2
// modified == "First line of this says something.\nThis on the second line says more."
}
Vars
func main() {
expanded := replace.Vars("hello ${name}", map[string]string{
"name": "strange new worlds",
})
// expanded == "hello strange new worlds"
}
Go-CoreLibs
Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.
License
Copyright 2024 The Go-CoreLibs Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
# Functions
FindAllIncluded walks the given target paths, looking for unique IsIncluded files.
FindAllMatcher uses FindAllIncluded to derive a list of `files` and a list of `matches` (using the `matcher` func), returning ErrTooManyFiles if the total number of files exceeds the MaxFileCount.
FindAllMatchingRegexp is a wrapper around FindAllMatcher with a custom matcher func which uses `search.Match` to filter the `matches` list.
FindAllMatchingRegexpLines is a wrapper around FindAllMatcher with a custom matcher func which uses `search.Match` on each line of each file to filter the `matches` list.
FindAllMatchingString is a wrapper around FindAllMatcher with a custom matcher func which uses [strings.Contains] to filter the `matches` list.
FindAllMatchingStringInsensitive is a wrapper around FindAllMatcher with a custom matcher func which uses [strings.Contains], in a case-insensitive way, to filter the `matches` list.
IsIncluded returns true if the given `input` string is included or is not explicitly excluded, or if `include` and `exclude` are nil or empty.
MakeRegexp constructs a new *regexp.Regexp instance, prefixing the search pattern with `(?msi)` depending on the boolean arguments given.
ProcessFile is a convenience function which calls os.ReadFile on the target and runs the given `fn` with the string contents and creates a Diff of the changes between the original and the modified output.
Regex counts the number of matches and runs ReplaceAllString on the given contents, if there are no matches, `modified` will be the same as `contents`.
RegexFile uses Regex to ProcessFile.
RegexLines is like Regex with the exception that the contents are split into a list of lines and `search` is applied to each line individually.
RegexLinesFile uses RegexLines to ProcessFile.
RegexPreserve is similar to StringPreserve except that it works with regular expressions to perform the search and replacement process.
RegexPreserveFile uses StringPreserve to ProcessFile.
String counts the number of matches and runs strings.ReplaceAll on the given contents, if there are no matches, `modified` will be the same as `contents`.
StringFile uses String to ProcessFile.
StringInsensitive counts the number of case-insensitive matches and replaces each instance with the `replace` value, if there are no matches `modified` will be the same as `contents`.
StringInsensitiveFile uses StringInsensitive to ProcessFile.
StringPreserve attempts to preserve the case of per-replacement matches.
StringPreserveFile uses StringPreserve to ProcessFile.
Vars searches through `input` for variables in the form of `$Name` or `${Name}` and replaces them with the corresponding `replacements` value.
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Type aliases
FindAllMatcherFn is the function signature for custom matching of content.
FindAllMatchingFn is the function signature for custom tracking of the finding process.