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."
}
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.
Documentation
¶
Overview ¶
Package replace provides text replacement utilities
Index ¶
- func ProcessFile(target string, fn func(original string) (modified string, count int)) (original, modified string, count int, delta *diff.Diff, err error)
- func Regex(search *regexp.Regexp, replace, contents string) (modified string, count int)
- func RegexFile(search *regexp.Regexp, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
- func RegexLines(search *regexp.Regexp, replace, contents string) (modified string, count int)
- func RegexLinesFile(search *regexp.Regexp, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
- func RegexPreserve(search *regexp.Regexp, replace, contents string) (modified string, count int)
- func RegexPreserveFile(search *regexp.Regexp, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
- func String(search, replace, contents string) (modified string, count int)
- func StringFile(search, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
- func StringInsensitive(search, replace, contents string) (modified string, count int)
- func StringInsensitiveFile(search, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
- func StringPreserve(search, replace, contents string) (modified string, count int)
- func StringPreserveFile(search, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ProcessFile ¶
func ProcessFile(target string, fn func(original string) (modified string, count int)) (original, modified string, count int, delta *diff.Diff, err error)
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
func Regex ¶
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`
func RegexFile ¶
func RegexFile(search *regexp.Regexp, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
RegexFile uses Regex to ProcessFile
func RegexLines ¶ added in v1.1.0
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
func RegexLinesFile ¶ added in v1.1.0
func RegexLinesFile(search *regexp.Regexp, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
RegexLinesFile uses RegexLines to ProcessFile
func RegexPreserve ¶
RegexPreserve is similar to StringPreserve except that it works with regular expressions to perform the search and replacement process.
While StringPreserve can easily detect un-case-detectable inputs, due to the variable nature of regular expressions it is assumed that the developer using RegexPreserve is confident that the `search` and `replace` arguments result in case-detectable string replacements.
func RegexPreserveFile ¶
func RegexPreserveFile(search *regexp.Regexp, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
RegexPreserveFile uses StringPreserve to ProcessFile
func String ¶
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`
func StringFile ¶
func StringFile(search, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
StringFile uses String to ProcessFile
func StringInsensitive ¶
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`
func StringInsensitiveFile ¶
func StringInsensitiveFile(search, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
StringInsensitiveFile uses StringInsensitive to ProcessFile
func StringPreserve ¶
StringPreserve attempts to preserve the case of per-replacement matches. Not all strings can have their cases easily detected and StringPreserve uses CanPreserve to check if the `search` and `replace` arguments are simple enough for DetectCase to discern the case patterns. If StringPreserve can't preserve, it defaults to returning the results of calling String with the same arguments given to StringPreserve.
To preserve the per-instance cases, each instance has DetectCase run and uses Case.Apply to derive the `replace` value actually used.
See the Case constants for the list of string cases supported.
Types ¶
This section is empty.