replacer

package
v0.0.54 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package replacer provides a string replacement mechanism with custom replace functions and keywords.

Package replacer provides a string replacement mechanism with custom replace functions and keywords.

Index

Constants

View Source
const (
	// DefaultStartKeyword defines the default keyword.
	DefaultStartKeyword      = "${"
	DefaultHostStartKeyword  = "@{"
	DefaultMatchStartKeyword = "@"

	DefaultEndKeyword      = "}"
	DefaultHostEndKeyword  = ":"
	DefaultMatchEndKeyword = DefaultHostEndKeyword
)

Variables

This section is empty.

Functions

func FileReplacer

func FileReplacer(path string, replacements map[string]string) ([]byte, error)

FileReplacer opens a file, replaces occurrences of ${name} with values from the map, and returns the result as []byte.

Types

type MatchSetting

type MatchSetting = func(*matcher)

func WithMatchEnd

func WithMatchEnd(end string) MatchSetting

WithMatchEnd returns a new Matcher instance with replacer end keyword.

func WithMatchFold

func WithMatchFold(fold bool) MatchSetting

WithMatchFold returns a new Matcher instance with case-insensitive matching.

func WithMatchHostMap

func WithMatchHostMap(hosts map[string]string) MatchSetting

WithMatchHostMap Parse the name and return the corresponding IP address

func WithMatchHosts

func WithMatchHosts(list []string, sep string) MatchSetting

WithMatchHosts Parse the name and return the corresponding IP address

func WithMatchSta

func WithMatchSta(sta string) MatchSetting

WithMatchSta returns a new Matcher instance with replacer start keyword.

type Matcher

type Matcher interface {
	Match(content string) (string, bool)
	Replace(content string) string
}

Matcher interface defines methods for matching and replacing strings.

func NewMatch

func NewMatch(replacements map[string]string, ss ...MatchSetting) Matcher

NewMatch creates a new matcher with the provided replacements.

type ReplaceFunc

type ReplaceFunc func(src, key, value string, fold bool) (string, bool)

ReplaceFunc is a function type that accepts a string and returns a replaced string.

type Replacer

type Replacer interface {
	Matcher(replacements map[string]string) Matcher
	Replace(content []byte, replacements map[string]string) []byte // Replaces substrings based on provided key-value pairs.
	ReplaceString(content string, replacements map[string]string) string
}

Replacer interface defines methods for setting keywords and performing replacements.

func New

func New(ss ...Setting) Replacer

New returns a new Replacer instance with default settings.

func NewHost

func NewHost(ss ...Setting) Replacer

type Setting

type Setting = func(*replace)

Setting is the setting of replacer.

func WithEnd

func WithEnd(keyword string) Setting

WithEnd returns a new Replacer instance with replacer end keyword.

func WithFold

func WithFold() Setting

WithFold returns a new Replacer instance with string case folding.

func WithHook

func WithHook(hook ReplaceFunc) Setting

WithHook returns a new Replacer instance with the specified hooks.

func WithKeyword

func WithKeyword(keyword string) Setting

WithKeyword returns a new Replacer instance with the specified keyword.

func WithStart

func WithStart(keyword string) Setting

WithStart returns a new Replacer instance with replacer start keyword.

Jump to

Keyboard shortcuts

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