batch_replace

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2022 License: MIT Imports: 5 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 SetSrcBytes() and SetSrcStr() methods.

func AcquireWithBytesSrc

func AcquireWithBytesSrc(src []byte) *BatchReplace

AcquireWithBytesSrc gets replacer from default pool and set byte array as a source.

func AcquireWithStrSrc

func AcquireWithStrSrc(src string) *BatchReplace

AcquireWithStrSrc gets replacer from default pool and set string as a source.

func NewBatchReplace

func NewBatchReplace(s []byte) *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 BytesToStr.

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 []byte, 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.

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().

func (*BatchReplace) CommitStr

func (r *BatchReplace) CommitStr() string

CommitStr 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 StrToBytes.

func (*BatchReplace) S2F

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

S2F is an alias of StrToFloat.

func (*BatchReplace) S2FT

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

S2FT is an alias of StrToFloatTunable.

func (*BatchReplace) S2I

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

S2I is an alias of StrToInt.

func (*BatchReplace) S2IB

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

S2IB is an alias of StrToIntBase.

func (*BatchReplace) S2S

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

S2S is an alias of StrToStr.

func (*BatchReplace) S2U

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

S2U is an alias of StrToUint.

func (*BatchReplace) S2UB

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

S2UB is an alias of StrToUintBase.

func (*BatchReplace) SetSrcBytes

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

SetSrcBytes set the source as bytes.

For use outside of pools.

func (*BatchReplace) SetSrcStr

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

SetSrcStr set the source as string.

func (*BatchReplace) StrToBytes

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

StrToBytes registers new bytes to string to bytes replacement.

func (*BatchReplace) StrToFloat

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

StrToFloat registers string to float replacement.

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.

func (*BatchReplace) StrToInt

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

StrToInt segisters string to int replacement.

func (*BatchReplace) StrToIntBase

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

StrToIntBase registers string to int replacement with given base.

func (*BatchReplace) StrToStr

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

StrToStr registers new string to string replacement.

func (*BatchReplace) StrToUint

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

StrToUint registers string to uint replacement.

func (*BatchReplace) StrToUintBase

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

StrToUintBase 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