forked from uber/prototool
/
strs.go
218 lines (200 loc) · 5.53 KB
/
strs.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Package strs contains common string manipulation functionality.
//
// This functionality is not really centralized anywhere in Golang OSS world,
// and there are some specific requirements we have. This is used mostly
// in the lint package.
package strs
import (
"sort"
"strings"
"unicode"
"unicode/utf8"
)
// IsCapitalized returns true if is not empty and the first letter is
// an uppercase character.
func IsCapitalized(s string) bool {
if s == "" {
return false
}
r, _ := utf8.DecodeRuneInString(s)
return isUpper(r)
}
// IsCamelCase returns false if s is empty or contains any character that is
// not between 'A' and 'Z', 'a' and 'z', '0' and '9', or in extraRunes.
// It does not care about lowercase or uppercase.
func IsCamelCase(s string) bool {
if s == "" {
return false
}
for _, c := range s {
if !(isLetter(c) || isDigit(c)) {
return false
}
}
return true
}
// IsLowerSnakeCase returns true if s only contains lowercase letters,
// digits, and/or underscores. s MUST NOT begin or end with an underscore.
func IsLowerSnakeCase(s string) bool {
if s == "" || s[0] == '_' || s[len(s)-1] == '_' {
return false
}
for _, r := range s {
if !(isLower(r) || isDigit(r) || r == '_') {
return false
}
}
return true
}
// IsUpperSnakeCase returns true if s only contains uppercase letters,
// digits, and/or underscores. s MUST NOT begin or end with an underscore.
func IsUpperSnakeCase(s string) bool {
if s == "" || s[0] == '_' || s[len(s)-1] == '_' {
return false
}
for _, r := range s {
if !(isUpper(r) || isDigit(r) || r == '_') {
return false
}
}
return true
}
// ToUpperSnakeCase converts s to UPPER_SNAKE_CASE.
func ToUpperSnakeCase(s string) string {
return strings.ToUpper(toSnake(s))
}
// ToUpperCamelCase converts s to UpperCamelCase.
//
// We use this for files, so any delimiter (_, -, or space) is
// used to denote word boundaries, but we trim spaces from the
// beginning and end of the string first.
//
// If a letter is uppercase, it will stay uppercase regardless,
// this is for cases of abbreviations.
func ToUpperCamelCase(s string) string {
output := ""
var previous rune
for i, c := range strings.TrimSpace(s) {
if !isDelimiter(c) {
if i == 0 || isDelimiter(previous) || isUpper(c) {
output += string(unicode.ToUpper(c))
} else {
output += string(unicode.ToLower(c))
}
}
previous = c
}
return output
}
// DedupeSort returns s with no duplicates and no empty strings, sorted.
// If modifier is not nil, modifier will be applied to each element in s.
func DedupeSort(s []string, modifier func(string) string) []string {
m := make(map[string]struct{}, len(s))
o := make([]string, 0, len(m))
for _, e := range s {
if e == "" {
continue
}
key := e
if modifier != nil {
key = modifier(e)
}
if _, ok := m[key]; ok {
continue
}
m[key] = struct{}{}
o = append(o, key)
}
sort.Strings(o)
return o
}
// Intersection return the intersection between one and
// two, sorted and dropping empty strings.
func Intersection(one []string, two []string) []string {
m1 := make(map[string]struct{})
for _, e := range one {
if e == "" {
continue
}
m1[e] = struct{}{}
}
m2 := make(map[string]struct{})
for _, e := range two {
if e == "" {
continue
}
m2[e] = struct{}{}
}
for key := range m1 {
if _, ok := m2[key]; !ok {
delete(m1, key)
}
}
s := make([]string, 0, len(m1))
for key := range m1 {
s = append(s, key)
}
sort.Strings(s)
return s
}
// IsLowercase returns true if s is not empty and is all lowercase.
func IsLowercase(s string) bool {
if s == "" {
return false
}
return strings.ToLower(s) == s
}
// IsUppercase returns true if s is not empty and is all uppercase.
func IsUppercase(s string) bool {
if s == "" {
return false
}
return strings.ToUpper(s) == s
}
// toSnake converts s to snake_case.
// It is assumed s has no spaces.
func toSnake(s string) string {
output := ""
for i, c := range s {
if i > 0 && isUpper(c) && output[len(output)-1] != '_' && i < len(s)-1 && !isUpper(rune(s[i+1])) {
output += "_" + string(c)
} else {
output += string(c)
}
}
return output
}
func isLetter(r rune) bool {
return isUpper(r) || isLower(r)
}
func isLower(r rune) bool {
return 'a' <= r && r <= 'z'
}
func isUpper(r rune) bool {
return 'A' <= r && r <= 'Z'
}
func isDigit(r rune) bool {
return '0' <= r && r <= '9'
}
func isDelimiter(r rune) bool {
return r == '-' || r == '_' || r == ' ' || r == '\t' || r == '\n' || r == '\r'
}