-
Notifications
You must be signed in to change notification settings - Fork 35
/
strings.go
155 lines (142 loc) · 3.61 KB
/
strings.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package build
import (
"net"
"net/url"
"sort"
"strings"
"golang.org/x/net/idna"
"golang.org/x/text/language"
"golang.org/x/text/unicode/norm"
)
// Normalize normalizes a domain name into valid, lowercase, Unicode form.
func Normalize(s string) string {
s = norm.NFKC.String(s)
s = strings.Trim(s, "-.")
s = strings.ToLower(s)
s, _ = idna.ToASCII(s)
s, _ = idna.ToUnicode(s)
return s
}
// ToASCII normalizes a domain name to ASCII/punycode.
func ToASCII(s string) string {
s, _ = idna.ToASCII(s)
return s
}
const hostnameProxy = "_____HOSTNAME_____"
// NormalizeURL normalizes the domain name part of a URL, with a Unicode (non-IDNA encoded) hostname.
// Returns a blank string if it is unable to parse s as a valid URL.
func NormalizeURL(s string) string {
if s == "" {
return s
}
u, _ := url.Parse(s)
if u == nil {
return ""
}
h, p, err := net.SplitHostPort(u.Host)
if err != nil {
h = u.Hostname()
}
if (u.Scheme == "http" && p == "80") || (u.Scheme == "https" && p == "443") {
p = ""
}
h = Normalize(h)
if p != "" {
h = net.JoinHostPort(h, p)
}
u.Host = hostnameProxy
if (u.Scheme == "http" || u.Scheme == "https") && u.Path == "" {
u.Path = "/"
}
return strings.Replace(u.String(), hostnameProxy, h, 1)
}
// ToASCIIURL normalizes the domain name part of a URL to ASCII/punycode.
func ToASCIIURL(s string) string {
if s == "" {
return s
}
u, _ := url.Parse(s)
if u == nil {
return ""
}
u.Host = ToASCII(u.Host)
return u.String()
}
// normalizeLang normalizes a 4-char ISO 15924 script code (e.g. “Latn”)
// or a BCP 47 language tag, e.g. (“en” or “en-us”).
func normalizeLang(s string) (string, error) {
if s == "none" {
s = "und"
}
tag, err := language.Parse(s)
if err != nil {
if len(s) != 4 {
return "", err
}
tag, err = language.Parse("mul-" + s) // Try parsing as a script, e.g. “Latn”
}
return tag.String(), err
}
// NormalizeDomains normalizes a slice of domain names.
// Returns a sorted, de-duplicated, normalized list of domain names.
func NormalizeDomains(in []string) []string {
out := in[:]
for i := range out {
out[i] = Normalize(out[i])
}
out = NewSet(out...).Values()
sort.Strings(out)
return out
}
// IndexOfString finds s as a substring in haystack, returning the index and offset of s in haystack.
// Returns -1, -1 if s is not present in haystack.
func IndexOfString(haystack []string, s string) (int, int) {
for i := range haystack {
j := strings.Index(haystack[i], s)
if j < 0 {
continue
}
return i, j
}
return -1, -1
}
// IndexOrAppendString finds or adds s to haystack, returning the index and offset of s in haystack.
// Returns 0, 0, 0 for an empty string.
func IndexOrAppendString(haystack *[]string, s string) (int, int, int) {
if len(s) == 0 {
return 0, 0, 0
}
i, j := IndexOfString(*haystack, s)
if i < 0 {
i = len(*haystack)
*haystack = append(*haystack, s)
}
return i, j, j + len(s)
}
// IndexOfStrings finds a rank-sorted slice of domain names (needle)
// within a larger slice (haystack).
func IndexOfStrings(haystack []string, needle []string) int {
outer:
for i := range haystack {
for j := range needle {
if i+j >= len(haystack) || needle[j] != haystack[i+j] {
continue outer
}
}
return i
}
return -1
}
// IndexOrAppendStrings finds or appends a slice of rank-sorted domain names (needle)
// Returns 0,0 for a zero-length needle.
func IndexOrAppendStrings(haystack *[]string, needle []string) (int, int) {
if len(needle) == 0 {
return 0, 0
}
idx := IndexOfStrings(*haystack, needle)
if idx < 0 {
idx = len(*haystack)
*haystack = append(*haystack, needle...)
}
return idx, idx + len(needle)
}