replace

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2024 License: Apache-2.0 Imports: 6 Imported by: 7

README

godoc codecov Go Report Card

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

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 ProfileCase

func ProfileCase(input string) (lower, upper, space, kebab, underscore bool)

ProfileCase scans through each rune in the given input and checks for a number of interesting hints about the string case of the input

func Regex

func Regex(search *regexp.Regexp, replace, contents string) (modified string, count int)

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 RegexPreserve

func RegexPreserve(search *regexp.Regexp, replace, contents string) (modified string, count int)

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

func String(search, replace, contents string) (modified string, count int)

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

func StringInsensitive(search, replace, contents string) (modified string, count int)

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

func StringPreserve(search, replace, contents string) (modified string, count int)

StringPreserve is like StringInsensitive except that StringPreserve attempts to preserve the per-instance case when doing replacements. If `search` or `replace` have any spaces, StringPreserve is wraps String.

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

Types

type Case

type Case uint8

Case is a simple type for indicating a detected string case

const (
	UnknownCase Case = iota
	LowerCase
	UpperCase
	CamelCase
	LowerCamelCase
	KebabCase
	ScreamingKebabCase
	SnakeCase
	ScreamingSnakeCase
)

func DetectCase

func DetectCase(input string) (detected Case)

DetectCase uses ProfileCase and some extra efforts to reliably discern the obvious string case of the given input

func (Case) Apply

func (c Case) Apply(input string) (modified string)

Apply returns the input text with the detected Case applied. For example:

CamelCase.Apply("kebab-thing") == "KebabThing"
KebabCase.Apply("CamelCase") == "camel-case"

func (Case) String

func (c Case) String() (name string)

String returns the name of the string case, in that string cases' case

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL