batch_replace

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: MIT Imports: 6 Imported by: 1

README

BatchReplace

BR is a tool to make replaces in bytes/string handy and alloc-free.

In fact it isn't a replacement of strings.Replacer since vanilla replacer made for concurrent use, whereas BatchReplacer made to reduce allocations for big lists of replacements.

Usage example:

originalStr := "this IS a string that contains {tag0}, {tag1}, tag2 and #s"
expectStr := "this WAS a string that contains 'very long substring', 1234567890, 154.195628217573 and etc..."

// Use pool instead of direct using of NewBatchReplace() or NewBatchReplaceStr().
// Pool may help you to get zero allocations on long distance and under high load.
r := batch_replace.AcquireWithStrSrc(originalStr)
defer batch_replace.Release(r)
res := r.StrToStr("IS", "WAS").
    S2S("{tag0}", "'very long substring'").
    StrToInt("{tag1}", int64(1234567890)).
    S2F("tag2", float64(154.195628217573)).
    S2S("#s", "etc...").
    Commit()
fmt.Println(res == expectStr) // true

Benchmarks

BenchmarkBatchReplace/b2x-8         	 1566085	       742.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkBatchReplace/b2x_native-8  	 1570418	       729.9 ns/op	     288 B/op	       6 allocs/op
BenchmarkBatchReplace/s2x-8         	 1543791	       776.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkBatchReplace/s2x_native-8  	 1707190	       721.6 ns/op	     288 B/op	       6 allocs/op
BenchmarkBatchReplace/no_alloc-8    	 2998296	       409.1 ns/op	       0 B/op	       0 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Release

func Release(x *BatchReplace)

Release puts replacer back to default pool.

Types

type BatchReplace

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

BatchReplace is a tool to collect replacement pairs and apply them once.

func Acquire

func Acquire() *BatchReplace

Acquire gets replacer from default pool.

Please note, this method doesn't provide source for replacer and you need to specify it manually by calling SetSource() method.

func AcquireWithBytesSrc

func AcquireWithBytesSrc(src []byte) *BatchReplace

AcquireWithBytesSrc gets replacer from default pool and set byte array as a source. Deprecated: use AcquireWithSource() instead.

func AcquireWithSource added in v1.1.1

func AcquireWithSource[T byteseq.Byteseq](x T) *BatchReplace

AcquireWithSource gets replace from default pool and sets source for replacements.

func AcquireWithStrSrc

func AcquireWithStrSrc(src string) *BatchReplace

AcquireWithStrSrc gets replacer from default pool and set string as a source. Deprecated: use AcquireWithSource() instead.

func NewBatchReplace

func NewBatchReplace[T byteseq.Byteseq](x T) *BatchReplace

NewBatchReplace inits new replacer.

func (*BatchReplace) B2B

func (r *BatchReplace) B2B(old []byte, new []byte) *BatchReplace

B2B is an alias of BytesToBytes.

func (*BatchReplace) B2F

func (r *BatchReplace) B2F(old []byte, new float64) *BatchReplace

B2F is an alias of BytesToFloat.

func (*BatchReplace) B2FT

func (r *BatchReplace) B2FT(old []byte, new float64, fmt byte, prec, bitSize int) *BatchReplace

B2FT is an alias of BytesToFloatTunable.

func (*BatchReplace) B2I

func (r *BatchReplace) B2I(old []byte, new int64) *BatchReplace

B2I is an alias of BytesToInt.

func (*BatchReplace) B2IB

func (r *BatchReplace) B2IB(old []byte, new int64, base int) *BatchReplace

B2IB is an alias of BytesToIntBase.

func (*BatchReplace) B2S

func (r *BatchReplace) B2S(old []byte, new string) *BatchReplace

B2S is an alias of BytesToString.

func (*BatchReplace) B2U

func (r *BatchReplace) B2U(old []byte, new uint64) *BatchReplace

B2U is an alias of BytesToUint.

func (*BatchReplace) B2UB

func (r *BatchReplace) B2UB(old []byte, new uint64, base int) *BatchReplace

B2UB is an alias of BytesToUintBase.

func (*BatchReplace) BytesToBytes

func (r *BatchReplace) BytesToBytes(old, new []byte) *BatchReplace

BytesToBytes registers new bytes to bytes replacement.

func (*BatchReplace) BytesToFloat

func (r *BatchReplace) BytesToFloat(old []byte, new float64) *BatchReplace

BytesToFloat registers bytes to float replacement.

func (*BatchReplace) BytesToFloatTunable

func (r *BatchReplace) BytesToFloatTunable(old []byte, new float64, fmt byte, prec, bitSize int) *BatchReplace

BytesToFloatTunable registers bytes to float replacement with params.

func (*BatchReplace) BytesToInt

func (r *BatchReplace) BytesToInt(old []byte, new int64) *BatchReplace

BytesToInt registers bytes to int replacement.

func (*BatchReplace) BytesToIntBase

func (r *BatchReplace) BytesToIntBase(old []byte, new int64, base int) *BatchReplace

BytesToIntBase registers bytes to int replacement with given base.

func (*BatchReplace) BytesToStr

func (r *BatchReplace) BytesToStr(old []byte, new string) *BatchReplace

BytesToStr registers new bytes-to-bytes to string replacement. Deprecated: use BytesToString() instead.

func (*BatchReplace) BytesToString added in v1.1.3

func (r *BatchReplace) BytesToString(old []byte, new string) *BatchReplace

BytesToString registers new bytes-to-bytes to string replacement.

func (*BatchReplace) BytesToUint

func (r *BatchReplace) BytesToUint(old []byte, new uint64) *BatchReplace

BytesToUint registers bytes to uint replacement.

func (*BatchReplace) BytesToUintBase

func (r *BatchReplace) BytesToUintBase(old []byte, new uint64, base int) *BatchReplace

BytesToUintBase registers bytes to uint replacement with given base.

func (*BatchReplace) Commit

func (r *BatchReplace) Commit() []byte

Commit applies all registered replacement pairs.

func (*BatchReplace) CommitCopy

func (r *BatchReplace) CommitCopy() []byte

CommitCopy perform the replaces and return copy of result.

Made to avoid data sharing. See Commit.

func (*BatchReplace) CommitCopyStr

func (r *BatchReplace) CommitCopyStr() string

CommitCopyStr os a string version of CommitCopy(). Deprecated: use CommitCopyString() instead.

func (*BatchReplace) CommitCopyString added in v1.1.3

func (r *BatchReplace) CommitCopyString() string

CommitCopyString os a string version of CommitCopy().

func (*BatchReplace) CommitStr

func (r *BatchReplace) CommitStr() string

CommitStr is a string version of Commit(). Deprecated: use CommitCopy() instead.

func (*BatchReplace) CommitString added in v1.1.3

func (r *BatchReplace) CommitString() string

CommitString is a string version of Commit().

func (*BatchReplace) Reset

func (r *BatchReplace) Reset() *BatchReplace

Reset clears the replacer with keeping of allocated space to reuse.

func (*BatchReplace) S2B

func (r *BatchReplace) S2B(old string, new []byte) *BatchReplace

S2B is an alias of StringToBytes.

func (*BatchReplace) S2F

func (r *BatchReplace) S2F(old string, new float64) *BatchReplace

S2F is an alias of StringToFloat.

func (*BatchReplace) S2FT

func (r *BatchReplace) S2FT(old string, new float64, fmt byte, prec, bitSize int) *BatchReplace

S2FT is an alias of StringToFloatTunable.

func (*BatchReplace) S2I

func (r *BatchReplace) S2I(old string, new int64) *BatchReplace

S2I is an alias of StringToInt.

func (*BatchReplace) S2IB

func (r *BatchReplace) S2IB(old string, new int64, base int) *BatchReplace

S2IB is an alias of StringToIntBase.

func (*BatchReplace) S2S

func (r *BatchReplace) S2S(old, new string) *BatchReplace

S2S is an alias of StringToString.

func (*BatchReplace) S2U

func (r *BatchReplace) S2U(old string, new uint64) *BatchReplace

S2U is an alias of StringToUint.

func (*BatchReplace) S2UB

func (r *BatchReplace) S2UB(old string, new uint64, base int) *BatchReplace

S2UB is an alias of StringToUintBase.

func (*BatchReplace) SetSource added in v1.1.1

func (r *BatchReplace) SetSource(src []byte) *BatchReplace

SetSource set the source as bytes.

For use outside of pools.

func (*BatchReplace) SetSourceString added in v1.1.1

func (r *BatchReplace) SetSourceString(src string) *BatchReplace

SetSourceString set the source as string.

func (*BatchReplace) SetSrcBytes

func (r *BatchReplace) SetSrcBytes(src []byte) *BatchReplace

SetSrcBytes set the source as bytes.

For use outside of pools. Deprecated: use SetSource() instead.

func (*BatchReplace) SetSrcStr

func (r *BatchReplace) SetSrcStr(src string) *BatchReplace

SetSrcStr set the source as string. Deprecated: use SetSourceString() instead.

func (*BatchReplace) StrToBytes

func (r *BatchReplace) StrToBytes(old string, new []byte) *BatchReplace

StrToBytes registers new bytes to string to bytes replacement. Deprecated: use StringToBytes() instead.

func (*BatchReplace) StrToFloat

func (r *BatchReplace) StrToFloat(old string, new float64) *BatchReplace

StrToFloat registers string to float replacement. Deprecated: use StringToFloat() instead.

func (*BatchReplace) StrToFloatTunable

func (r *BatchReplace) StrToFloatTunable(old string, new float64, fmt byte, prec, bitSize int) *BatchReplace

StrToFloatTunable registers string to float replacement with params. Deprecated: use StringToFloatTunable() instead.

func (*BatchReplace) StrToInt

func (r *BatchReplace) StrToInt(old string, new int64) *BatchReplace

StrToInt registers string to int replacement. Deprecated: use StringToInt() instead.

func (*BatchReplace) StrToIntBase

func (r *BatchReplace) StrToIntBase(old string, new int64, base int) *BatchReplace

StrToIntBase registers string to int replacement with given base. Deprecated: use StringToIntBase() instead.

func (*BatchReplace) StrToStr

func (r *BatchReplace) StrToStr(old, new string) *BatchReplace

StrToStr registers new string-to-string replacement. Deprecated: use StringToString() instead.

func (*BatchReplace) StrToUint

func (r *BatchReplace) StrToUint(old string, new uint64) *BatchReplace

StrToUint registers string to uint replacement. Deprecated: use StringToUint() instead.

func (*BatchReplace) StrToUintBase

func (r *BatchReplace) StrToUintBase(old string, new uint64, base int) *BatchReplace

StrToUintBase registers string to uint replacement with given base. Deprecated: use StringToUintBase() instead.

func (*BatchReplace) StringToBytes added in v1.1.3

func (r *BatchReplace) StringToBytes(old string, new []byte) *BatchReplace

StringToBytes registers new bytes to string to bytes replacement.

func (*BatchReplace) StringToFloat added in v1.1.3

func (r *BatchReplace) StringToFloat(old string, new float64) *BatchReplace

StringToFloat registers string to float replacement.

func (*BatchReplace) StringToFloatTunable added in v1.1.3

func (r *BatchReplace) StringToFloatTunable(old string, new float64, fmt byte, prec, bitSize int) *BatchReplace

StringToFloatTunable registers string to float replacement with params.

func (*BatchReplace) StringToInt added in v1.1.3

func (r *BatchReplace) StringToInt(old string, new int64) *BatchReplace

StringToInt registers string to int replacement.

func (*BatchReplace) StringToIntBase added in v1.1.3

func (r *BatchReplace) StringToIntBase(old string, new int64, base int) *BatchReplace

StringToIntBase registers string to int replacement with given base.

func (*BatchReplace) StringToString added in v1.1.3

func (r *BatchReplace) StringToString(old, new string) *BatchReplace

StringToString registers new string-to-string replacement.

func (*BatchReplace) StringToUint added in v1.1.3

func (r *BatchReplace) StringToUint(old string, new uint64) *BatchReplace

StringToUint registers string to uint replacement.

func (*BatchReplace) StringToUintBase added in v1.1.3

func (r *BatchReplace) StringToUintBase(old string, new uint64, base int) *BatchReplace

StringToUintBase registers string to uint replacement with given base.

type Pool

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

Pool to store batch replacers.

func (*Pool) Get

func (p *Pool) Get(src []byte) *BatchReplace

Get old or create new instance of the batch replacer.

func (*Pool) Put

func (p *Pool) Put(r *BatchReplace)

Put batch replacer to the pool.

Jump to

Keyboard shortcuts

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