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)
- func Vars(input string, replacements map[string]string) (expanded string)
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.
func StringPreserveFile ¶
func StringPreserveFile(search, replace, target string) (original, modified string, count int, delta *diff.Diff, err error)
StringPreserveFile uses StringPreserve to ProcessFile
func Vars ¶ added in v1.2.0
Vars searches through `input` for variables in the form of `$Name` or `${Name}` and replaces them with the corresponding `replacements` value. Missing keys are replaced with empty strings
Vars follows the POSIX 3.235 Name definition:
a word consisting solely of underscores, digits, and alphabetics from the portable character set. The first character of a name is not a digit
Types ¶
This section is empty.