Source File
replace.go
Belonging Package
strings
package strings
import (
)
func ( ...string) *Replacer {
if len()%2 == 1 {
panic("strings.NewReplacer: odd argument count")
}
return &Replacer{oldnew: append([]string(nil), ...)}
}
func ( *Replacer) () {
.r = .build()
.oldnew = nil
}
func ( *Replacer) () replacer {
:= .oldnew
if len() == 2 && len([0]) > 1 {
return makeSingleStringReplacer([0], [1])
}
:= true
for := 0; < len(); += 2 {
if len([]) != 1 {
return makeGenericReplacer()
}
if len([+1]) != 1 {
= false
}
}
if {
:= byteReplacer{}
for := range {
[] = byte()
for := len() - 2; >= 0; -= 2 {
:= [][0]
:= [+1][0]
[] =
}
return &
}
for := len() - 2; >= 0; -= 2 {
:= [][0]
:= 0
:= &.root
:= 0
for != nil {
if .priority > && !( && == &.root) {
= .priority
= .value
=
= true
}
if == "" {
break
}
if .table != nil {
:= .mapping[[0]]
if int() == .tableSize {
break
}
= .table[]
= [1:]
++
} else if .prefix != "" && HasPrefix(, .prefix) {
+= len(.prefix)
= [len(.prefix):]
= .next
} else {
break
}
}
return
}
type genericReplacer struct {
mapping [256]byte
}
func ( []string) *genericReplacer {
func ( *appendSliceWriter) ( string) (int, error) {
* = append(*, ...)
return len(), nil
}
type stringWriter struct {
w io.Writer
}
func ( stringWriter) ( string) (int, error) {
return .w.Write([]byte())
}
func ( io.Writer) io.StringWriter {
, := .(io.StringWriter)
if ! {
= stringWriter{}
}
return
}
func ( *genericReplacer) ( string) string {
:= make(appendSliceWriter, 0, len())
.WriteString(&, )
return string()
}
func ( *genericReplacer) ( io.Writer, string) ( int, error) {
:= getStringWriter()
var , int
var bool
, , := .lookup([:], )
= && == 0
if {
, = .WriteString([:])
+=
if != nil {
return
}
, = .WriteString()
+=
if != nil {
return
}
+=
=
continue
}
++
}
if != len() {
, = .WriteString([:])
+=
}
return
}
type singleStringReplacer struct {
value string
}
func ( string, string) *singleStringReplacer {
return &singleStringReplacer{finder: makeStringFinder(), value: }
}
func ( *singleStringReplacer) ( string) string {
var []byte
, := 0, false
for {
:= .finder.next([:])
if == -1 {
break
}
= true
= append(, [:+]...)
= append(, .value...)
+= + len(.finder.pattern)
}
if ! {
return
}
= append(, [:]...)
return string()
}
func ( *singleStringReplacer) ( io.Writer, string) ( int, error) {
:= getStringWriter()
var , int
for {
:= .finder.next([:])
if == -1 {
break
}
, = .WriteString([ : +])
+=
if != nil {
return
}
, = .WriteString(.value)
+=
if != nil {
return
}
+= + len(.finder.pattern)
}
, = .WriteString([:])
+=
return
}
const countCutOff = 8
func ( *byteStringReplacer) ( string) string {
:= len()
if len(.toReplace)*countCutOff <= len() {
for , := range .toReplace {
+= * (len(.replacements[[0]]) - 1)
= true
}
}
} else {
for := 0; < len(); ++ {
:= []
+= len(.replacements[]) - 1
= true
}
}
}
if ! {
return
}
:= make([]byte, )
:= 0
for := 0; < len(); ++ {
:= []
if .replacements[] != nil {
+= copy([:], .replacements[])
} else {
[] =
++
}
}
return string()
}
func ( *byteStringReplacer) ( io.Writer, string) ( int, error) {
:= getStringWriter()
:= 0
for := 0; < len(); ++ {
:= []
if .replacements[] == nil {
continue
}
if != {
, := .WriteString([:])
+=
if != nil {
return ,
}
}
= + 1
, := .Write(.replacements[])
+=
if != nil {
return ,
}
}
if != len() {
var int
, = .WriteString([:])
+=
}
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |