-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace.go
66 lines (61 loc) · 1.62 KB
/
replace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package stringutils
import (
"strings"
"unicode/utf8"
)
// Replace returns a copy of the string s with the first n
// non-overlapping instances of old replaced by new.
// Also return change flag.
// If old is empty, it matches at the beginning of the string
// and after each UTF-8 sequence, yielding up to k+1 replacements
// for a k-rune string.
// If n < 0, there is no limit on the number of replacements.
func Replace(s, old, new string, n int) (string, bool) {
if old == new || n == 0 {
return s, false // avoid allocation
}
// Compute number of replacements.
if m := strings.Count(s, old); m == 0 {
return s, false // avoid allocation
} else if n < 0 || m < n {
n = m
}
// Apply replacements to buffer.
var b strings.Builder
b.Grow(len(s) + n*(len(new)-len(old)))
start := 0
if len(old) == 0 {
for i := 0; i < n; i++ {
j := start
if i > 0 {
_, wid := utf8.DecodeRuneInString(s[start:])
j += wid
}
b.WriteString(s[start:j])
b.WriteString(new)
start = j + len(old)
}
} else {
for i := 0; i < n; i++ {
j := strings.Index(s[start:], old)
if j == -1 {
break
}
j += start
b.WriteString(s[start:j])
b.WriteString(new)
start = j + len(old)
}
}
b.WriteString(s[start:])
return b.String(), true
}
// ReplaceAll returns a copy of the string s with all
// non-overlapping instances of old replaced by new.
// Also return change flag.
// If old is empty, it matches at the beginning of the string
// and after each UTF-8 sequence, yielding up to k+1 replacements
// for a k-rune string.
func ReplaceAll(s, old, new string) (string, bool) {
return Replace(s, old, new, -1)
}