-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaddresses.go
More file actions
38 lines (31 loc) · 757 Bytes
/
addresses.go
File metadata and controls
38 lines (31 loc) · 757 Bytes
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
package main
import (
"regexp"
"sort"
)
type AddressRewriter struct {
Source *regexp.Regexp
Dest string
}
func (r AddressRewriter) RewriteAll(addresses []string) []string {
rewritten := make(map[string]bool, 0)
for _, addr := range addresses {
rewritten[r.Rewrite(addr)] = true
}
results := make([]string, 0, len(rewritten))
for addr, _ := range rewritten {
results = append(results, addr)
}
sort.Strings(results)
return results
}
func (r AddressRewriter) Rewrite(address string) string {
if r.Source == nil || !r.Source.MatchString(address) {
return address
}
res := []byte{}
for _, s := range r.Source.FindAllStringSubmatchIndex(address, -1) {
res = r.Source.ExpandString(res, r.Dest, address, s)
}
return string(res)
}