replacer

package
v0.0.70 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

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

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Replace added in v0.0.55

func Replace(content []byte, replacements map[string]string) []byte

Replace replaces substrings within the provided content byte slice using the provided key-value pairs.

func ReplaceFileContent added in v0.0.69

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

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

func ReplaceFileContentWithMatcher added in v0.0.69

func ReplaceFileContentWithMatcher(path string, m Matcher) ([]byte, error)

ReplaceFileContentWithMatcher replaces occurrences of ${name} with values from the map

func ReplaceObjectContent added in v0.0.69

func ReplaceObjectContent(v any, replacements map[string]string) error

ReplaceObjectContent replaces occurrences of ${name} with values from the map

func ReplaceObjectContentWithMatcher added in v0.0.69

func ReplaceObjectContentWithMatcher(v any, m Matcher) error

ReplaceObjectContentWithMatcher replaces occurrences of ${name} with values from the map

func ReplaceString added in v0.0.55

func ReplaceString(content string, replacements map[string]string) string

ReplaceString replaces substrings within the provided content string using the provided key-value pairs.

Types

type Match added in v0.0.55

type Match struct {
	// contains filtered or unexported fields
}

func NewMatch

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

NewMatch creates a new Match with the provided replacements.

func NewMatchFile added in v0.0.57

func NewMatchFile(path string, ss ...MatchSetting) (*Match, error)

NewMatchFile creates a new Match with the provided replacements from a JSON file.

func (Match) Match added in v0.0.55

func (m Match) Match(content string) (string, bool)

func (Match) Replace added in v0.0.55

func (m Match) Replace(content string) string

func (Match) ReplaceBytes added in v0.0.66

func (m Match) ReplaceBytes(content []byte) []byte

ReplaceBytes replaces the content with the provided replacements.

func (Match) Replacement added in v0.0.67

func (m Match) Replacement() map[string]string

type MatchSetting

type MatchSetting = func(*Match)

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 WithMatchSeparator added in v0.0.66

func WithMatchSeparator(sep string) MatchSetting

WithMatchSeparator returns a new Matcher instance with replacer separator

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
	ReplaceBytes(content []byte) []byte
	Replacement() map[string]string
}

Matcher interface defines methods for matching and replacing strings.

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 Replacement added in v0.0.55

type Replacement struct {
	// contains filtered or unexported fields
}

Replacement struct implements the Replacer interface, storing Replacement hooks and the current keyword.

func New

func New(ss ...Setting) *Replacement

New returns a new Replacer instance with default settings.

func NewHost

func NewHost(ss ...Setting) *Replacement

NewHost returns a new Replacer instance with default host settings.

func (Replacement) Replace added in v0.0.55

func (r Replacement) Replace(content []byte, replacements map[string]string) []byte

Replace within the Replacement struct iterates through the values map, applying custom hooks and replacing placeholders found in the source string.

func (Replacement) ReplaceString added in v0.0.55

func (r Replacement) ReplaceString(content string, replacements map[string]string) string

ReplaceString replaces substrings within the provided content string using the provided key-value pairs.

func (Replacement) ToMatch added in v0.0.55

func (r Replacement) ToMatch(replacements map[string]string) Matcher

type Replacer

type Replacer interface {
	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.

type Setting

type Setting = func(*Replacement)

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(fold bool) 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 WithSeparator added in v0.0.66

func WithSeparator(sep string) Setting

WithSeparator returns a new Replacer instance with the specified separator.

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