forked from goforj/str
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.go
More file actions
100 lines (92 loc) · 2.56 KB
/
Copy pathreplace.go
File metadata and controls
100 lines (92 loc) · 2.56 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package str
import (
"regexp"
"sort"
"strings"
)
// ReplaceFirst replaces the first occurrence of old with repl.
// @group Replace
//
// Example: replace first
//
// v := str.Of("gopher gopher").ReplaceFirst("gopher", "go").String()
// println(v)
// // #string go gopher
func (s String) ReplaceFirst(old, repl string) String {
return String{s: strings.Replace(s.s, old, repl, 1)}
}
// ReplaceLast replaces the last occurrence of old with repl.
// @group Replace
//
// Example: replace last
//
// v := str.Of("gopher gopher").ReplaceLast("gopher", "go").String()
// println(v)
// // #string gopher go
func (s String) ReplaceLast(old, repl string) String {
idx := strings.LastIndex(s.s, old)
if idx == -1 || old == "" {
return s
}
var b strings.Builder
b.Grow(len(s.s) - len(old) + len(repl))
b.WriteString(s.s[:idx])
b.WriteString(repl)
b.WriteString(s.s[idx+len(old):])
return String{s: b.String()}
}
// ReplaceArray replaces all occurrences of each old in olds with repl.
// @group Replace
//
// Example: replace many
//
// v := str.Of("The---Go---Toolkit")
// println(v.ReplaceArray([]string{"---"}, "-").String())
// // #string The-Go-Toolkit
func (s String) ReplaceArray(olds []string, repl string) String {
out := s.s
for _, old := range olds {
if old == "" {
continue
}
out = strings.ReplaceAll(out, old, repl)
}
return String{s: out}
}
// ReplaceMatches applies repl to each regex match and returns the result.
// @group Replace
//
// Example: regex replace with callback
//
// re := regexp.MustCompile(`\d+`)
// v := str.Of("Hello 123 World").ReplaceMatches(re, func(m string) string { return "[" + m + "]" }).String()
// println(v)
// // #string Hello [123] World
func (s String) ReplaceMatches(pattern *regexp.Regexp, repl func(string) string) String {
return String{s: pattern.ReplaceAllStringFunc(s.s, repl)}
}
// Swap replaces multiple values using strings.Replacer built from a map.
// @group Replace
//
// Example: swap map
//
// pairs := map[string]string{"Gophers": "GoForj", "are": "is", "great": "fantastic"}
// v := str.Of("Gophers are great!").Swap(pairs).String()
// println(v)
// // #string GoForj is fantastic!
func (s String) Swap(pairs map[string]string) String {
if len(pairs) == 0 {
return s
}
keys := make([]string, 0, len(pairs))
for k := range pairs {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return len(keys[i]) > len(keys[j]) })
repPairs := make([]string, 0, len(keys)*2)
for _, k := range keys {
repPairs = append(repPairs, k, pairs[k])
}
r := strings.NewReplacer(repPairs...)
return String{s: r.Replace(s.s)}
}