-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
229 lines (200 loc) · 4.17 KB
/
string.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
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package strings
import (
"strings"
"unicode"
librunes "github.com/shuLhan/share/lib/runes"
)
//
// CleanURI remove known links from text and return it.
// This function assume that space in URI is using '%20' not literal space,
// as in ' '.
//
// List of known links: http, https, ftp, ftps, ssh, file, rsync, and ws.
//
func CleanURI(text string) string {
if len(text) == 0 {
return ""
}
var URIPrefixes = []string{
"http://", "https://", "ftp://", "ftps://", "ssh://", "file://", "rsync://", "ws://",
}
ctext := []rune(text)
for _, uri := range URIPrefixes {
startat := 0
curi := []rune(uri)
newtext := []rune{}
for {
begin := librunes.TokenFind(ctext, curi, startat)
if begin < 0 {
if startat > 0 {
newtext = append(newtext, ctext[startat:]...)
}
break
}
newtext = append(newtext, ctext[startat:begin]...)
end := librunes.FindSpace(ctext, begin)
if end < 0 {
break
}
startat = end
}
if len(newtext) > 0 {
ctext = newtext
}
}
return string(ctext)
}
//
// CleanWikiMarkup remove wiki markup from text.
//
// List of known wiki markups,
// - [[Category: ... ]]
// - [[:Category: ... ]]
// - [[File: ... ]]
// - [[Help: ... ]]
// - [[Image: ... ]]
// - [[Special: ... ]]
// - [[Wikipedia: ... ]]
// - {{DEFAULTSORT: ... }}
// - {{Template: ... }}
// - <ref ... />
//
func CleanWikiMarkup(text string) string {
ctext := []rune(text)
for _, mu := range listWikiMarkup {
ctext, _ = librunes.EncloseRemove(ctext, []rune(mu.begin), []rune(mu.end))
}
return string(ctext)
}
//
// MergeSpaces replace two or more horizontal spaces (' ', '\t', '\v', '\f',
// '\r') with single space.
// If withline is true it also replace two or more new lines with single
// new-line.
//
func MergeSpaces(text string, withline bool) string {
var (
isspace bool
isnewline bool
)
out := make([]rune, 0, len(text))
for _, v := range text {
switch v {
case ' ', '\t', '\v', '\f', '\r':
if isspace {
continue
}
isspace = true
default:
isspace = false
}
if withline {
if v == '\n' {
if isnewline {
continue
}
isnewline = true
} else if isnewline {
isnewline = false
}
}
out = append(out, v)
}
return string(out)
}
//
// Reverse the string.
//
func Reverse(input string) string {
r := []rune(input)
x := 0
y := len(r) - 1
for x < len(r)/2 {
r[x], r[y] = r[y], r[x]
x++
y--
}
return string(r)
}
//
// SingleSpace convert all sequences of white spaces into single space ' '.
//
func SingleSpace(in string) string {
var isspace bool
out := make([]rune, 0, len(in))
for _, r := range in {
if unicode.IsSpace(r) {
if isspace {
continue
}
isspace = true
continue
}
if isspace {
out = append(out, ' ')
isspace = false
}
out = append(out, r)
}
if isspace {
out = append(out, ' ')
}
return string(out)
}
//
// Split given a text, return all words in text.
//
// A word is any sequence of character which have length equal or greater than
// one and separated by white spaces.
//
// If cleanit is true remove any non-alphanumeric in the start and the end of
// each words.
//
// If uniq is true remove duplicate words, in case insensitive manner.
//
func Split(text string, cleanit bool, uniq bool) (words []string) {
words = strings.Fields(text)
if cleanit {
// Remove non-alphanumeric character from start and end of each word.
for x, word := range words {
words[x] = TrimNonAlnum(word)
}
}
if uniq {
return Uniq(words, false)
}
return words
}
//
// TrimNonAlnum remove non alpha-numeric character at the beginning and
// end for `text`.
//
func TrimNonAlnum(text string) string {
r := []rune(text)
rlen := len(r)
start := 0
for ; start < rlen; start++ {
if unicode.IsLetter(r[start]) || unicode.IsDigit(r[start]) {
break
}
}
if start >= rlen {
return ""
}
r = r[start:]
rlen = len(r)
end := rlen - 1
for ; end >= 0; end-- {
if unicode.IsLetter(r[end]) || unicode.IsDigit(r[end]) {
break
}
}
if end < 0 {
return ""
}
r = r[:end+1]
return string(r)
}