-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.go
More file actions
318 lines (261 loc) · 6.95 KB
/
strings.go
File metadata and controls
318 lines (261 loc) · 6.95 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package util
import (
"bytes"
"encoding/json"
"fmt"
"math"
"net/url"
"regexp"
"strconv"
"strings"
"unicode/utf8"
)
func UpTo(s string, numChars int) string {
if len(s) > numChars {
return s[:numChars]
}
return s
}
func UpToR(s string, numChars int) string {
if len(s) > numChars {
return s[len(s)-numChars:]
}
return s
}
func Ellipsoider(s string, maxChars int) string {
if len(s) > maxChars {
maxChars -= 6
return s[:maxChars/2] + " ... " + s[len(s)-maxChars/2:]
}
return s
}
func IndentedDump(v interface{}) string {
firstColLeftMostPrefix := " "
byts, err := json.MarshalIndent(v, firstColLeftMostPrefix, "\t")
if err != nil {
s := fmt.Sprintf("error indent: %v\n", err)
return s
}
byts = bytes.Replace(byts, []byte(`\u003c`), []byte("<"), -1)
byts = bytes.Replace(byts, []byte(`\u003e`), []byte(">"), -1)
byts = bytes.Replace(byts, []byte(`\n`), []byte("\n"), -1)
return string(byts)
}
var ascii1 = regexp.MustCompile(`[^a-z0-9\_]+`)
func Mustaz09_(s string) bool {
if ascii1.MatchString(s) {
return false
}
return true
}
var ascii2 = regexp.MustCompile(`[^a-zA-Z0-9\_]+`)
func MustazAZ09_(s string) bool {
if ascii2.MatchString(s) {
return false
}
return true
}
var ascii09 = regexp.MustCompile(`[^0-9\.,]+`)
func Must09(s string) bool {
if ascii09.MatchString(s) {
return false
}
return true
}
func EnsureUtf8(haystack string) string {
ret := bytes.Buffer{}
for _, codepoint := range haystack {
ret.WriteRune(codepoint)
}
return ret.String()
}
type FloatHumanizer int
// => cut off after 5 digits after separator; 0,
// 0,00142857142... becomes 0,0014286
var fh FloatHumanizer = 6
func HumanizeFloat(f float64) string {
return fh.Humanize(f)
}
func (fh *FloatHumanizer) SetPrecision(newPrec int) {
if newPrec < 2 {
panic("Precision needs to be larger than 1. Use floor instead.")
}
*fh = FloatHumanizer(newPrec)
}
// See test cases
func (precision FloatHumanizer) Humanize(f float64) string {
if f == 0.0 {
return "0"
}
//
// base of 0.01 is -2
// base of 100 is 2
absF := f
if absF < 0 {
absF = -absF
}
base := int(math.Floor(math.Log10(absF)))
//
// For small numbers such as 0.00012345
// precision is increased by three.
precIncrease := 0
if base > -1 {
} else {
precIncrease = -base - 1
}
// This could not prevent exponent
// formatting for f > 10^6:
if false {
formatter := fmt.Sprintf("%%.%vf", int(precision)+precIncrease)
str := fmt.Sprintf(formatter, f)
str = strings.TrimSpace(str)
}
// The only way to suppress the exponent
// is to use strconv.FormatFloat.
// Param 'f' means 'no exponent'.
// Precision could be -1.
// This would produce all ~55 digits of a float64
str := strconv.FormatFloat(f, 'f', int(precision)+precIncrease, 64)
strs := strings.Split(str, ".")
if len(strs) == 1 {
return str
}
// log.Printf("%12v %-20v ", strs[0], strs[1])
// 102.1000 back to 102.1
// 0.012000 back to 0.012
// log.Printf("before: -%v-", strs[1])
for {
if strings.HasSuffix(strs[1], "0") {
strs[1] = strs[1][:len(strs[1])-1]
} else {
break
}
}
if len(strs[1]) == 0 {
return strs[0]
}
// 100.0000012345678 => 100
// but
// 0.0000012345678 => 0.00000123457
startPosZeroes := -1
if base > -1 {
} else {
startPosZeroes += -base
}
// Eliminate sequences of zeros
// 123.4000000567 is shortened to
// 123.4
cutMeOff := strings.Repeat("0", int(precision)-1)
if pos := strings.Index(strs[1], cutMeOff); pos > startPosZeroes {
if pos == 0 {
return strs[0]
}
// pos > 0
strs[1] = strs[1][0:pos]
return strings.Join(strs, ".")
}
// Eliminate sequences of nines
// 123.4999999567 is shortened to
// 123.5
cutMeOff = strings.Repeat("9", int(precision)-1)
if pos := strings.Index(strs[1], cutMeOff); pos > -1 {
// 1234.99999; -1234.99999
if pos == 0 {
if f >= 0 {
return fmt.Sprintf("%v", math.Ceil(f))
}
return fmt.Sprintf("%v", math.Floor(f))
}
// 1234.599999; -1234.599999
strs[1] = strs[1][0:pos]
rn, sz := utf8.DecodeLastRuneInString(strs[1])
if rn != utf8.RuneError && sz == 1 {
// log.Printf("%v - %c => %c\n", strs[1], rn, rn+1)
rn++
strs[1] = strs[1][0 : len(strs[1])-1]
strs[1] = fmt.Sprintf("%v%c", strs[1], rn)
}
return strings.Join(strs, ".")
}
return strings.Join(strs, ".")
}
// normalize spaces
var replNewLines = strings.NewReplacer("\r\n", " ", "\r", " ", "\n", " ")
var replTabs = strings.NewReplacer("\t", " ")
var doubleSpaces = regexp.MustCompile("([ ]+)")
// All kinds of newlines, tabs and double spaces
// are reduced to single space.
// It paves the way for later beautification.
func NormalizeInnerWhitespace(s string) string {
s = replNewLines.Replace(s)
s = replTabs.Replace(s)
s = doubleSpaces.ReplaceAllString(s, " ")
return s
}
// We want only one newline; not several.
// We also want to catch newlines
// trailed by any kind of white space.
var undoubleNewlines = regexp.MustCompile(`(\r?\n(\s*))+`)
func UndoubleNewlines(s string) string {
return undoubleNewlines.ReplaceAllString(s, "\n")
}
var removeProtocol = strings.NewReplacer(
"http://", "",
"https://", "",
)
func RemoveProtocol(s string) string {
return removeProtocol.Replace(s)
}
// Func isSpacey is a shortcut func.
// It detects if there is ONLY whitspace,
// but nothing else.
// All combinations of whitespace lead to return true.
func IsSpacey(s string) bool {
s = strings.TrimSpace(s) // TrimSpace removes leading-trailing \n \r\n
if s == "" {
return true
}
return false
}
var allNumbers = regexp.MustCompile(`[0-9]+`)
// Implicitly contains removeProtokol()
// HostName gets reduced (www. is chopped off)
// Path is cleansed of long number /avatar/304930538/me.jpg => /avatar//me.jpg
// Notice: Url-Params also get stripped.
func UrlBeautify(surl string) string {
if !strings.HasPrefix(surl, "http://") && !strings.HasPrefix(surl, "https://") {
surl = "https://" + surl
}
url2, err := url.Parse(surl)
if err != nil {
return surl
}
pth := url2.Path
pth = allNumbers.ReplaceAllString(pth, "")
hst, _ := HostCore(url2.Host)
return hst + pth
}
var nonAscii = regexp.MustCompile(`[^a-zA-Z0-9\.\_]+`)
var mutatedVowels = strings.NewReplacer("ä", "ae", "ö", "oe", "ü", "ue", "Ä", "ae", "Ö", "oe", "Ü", "ue")
// LowerCasedUnderscored gives us a condensed filename
// cleansed of all non Ascii characters
// where word boundaries are encoded by "_"
//
// whenever we want a transformation of user input
// into innoccuous lower case - sortable - searchable
// ascii - the we should look to this func
// in addition - extensions are respected and cleansed
func LowerCasedUnderscored(s string) string {
//log.Printf("%v\n", s)
s = mutatedVowels.Replace(s)
s = strings.TrimSpace(s)
s = strings.Trim(s, `"' `)
replaced := nonAscii.ReplaceAllString(s, "_")
replaced = strings.Trim(replaced, `_`)
replaced = strings.ToLower(replaced)
// clean the file extension
replaced = strings.Replace(replaced, "_.", ".", -1)
replaced = strings.Replace(replaced, "._", ".", -1)
//log.Printf("%v\n", replaced)
return replaced
}