Blame view

string_util.go 3.86 KB
18fcd6d6b   Marko Tikvić   merged with util ...
1
  package webutility
cacf57bd4   Marko Tikvić   merging with /uti...
2
3
4
  
  import (
  	"fmt"
70f2e7ead   Marko Tikvić   simplified
5
  	"strconv"
cacf57bd4   Marko Tikvić   merging with /uti...
6
  	"strings"
a8f0d5a63   Marko Tikvić   SplitText
7
  	"unicode"
cacf57bd4   Marko Tikvić   merging with /uti...
8
  )
e95e68362   Marko Tikvić   merged string san...
9
10
11
12
13
14
  const sanitisationPatern = "\"';&*<>=\\`:"
  
  // SanitiseString removes characters from s found in patern and returns new modified string.
  func SanitiseString(s string) string {
  	return ReplaceAny(s, sanitisationPatern, "")
  }
cacf57bd4   Marko Tikvić   merging with /uti...
15
16
17
18
19
20
21
22
  // IsWrappedWith ...
  func IsWrappedWith(src, begin, end string) bool {
  	return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end)
  }
  
  // ParseInt64Arr ...
  func ParseInt64Arr(s, sep string) (arr []int64) {
  	s = strings.TrimSpace(s)
b80ee4b2b   Marko Tikvić   new stuff
23
24
25
26
27
28
29
30
  	if s == "" {
  		return
  	}
  	parts := strings.Split(s, sep)
  	arr = make([]int64, len(parts))
  	for i, p := range parts {
  		num := StringToInt64(p)
  		arr[i] = num
cacf57bd4   Marko Tikvić   merging with /uti...
31
32
33
34
35
36
37
  	}
  
  	return arr
  }
  
  // Int64SliceToString ...
  func Int64SliceToString(arr []int64) (s string) {
e95e68362   Marko Tikvić   merged string san...
38
39
40
41
42
43
44
  	if len(arr) == 0 {
  		return ""
  	}
  
  	s += fmt.Sprintf("%d", arr[0])
  	for i := 1; i < len(arr); i++ {
  		s += fmt.Sprintf(",%d", arr[i])
cacf57bd4   Marko Tikvić   merging with /uti...
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
  	}
  
  	return s
  }
  
  // CombineStrings ...
  func CombineStrings(s1, s2, s3 string) string {
  	s1 = strings.TrimSpace(s1)
  	s2 = strings.TrimSpace(s2)
  
  	if s1 != "" && s2 != "" {
  		s1 += s3 + s2
  	} else {
  		s1 += s2
  	}
  
  	return s1
  }
  
  // ReplaceAny replaces any of the characters from patern found in s with r and returns a new resulting string.
  func ReplaceAny(s, patern, r string) (n string) {
  	n = s
  	for _, c := range patern {
  		n = strings.Replace(n, string(c), r, -1)
  	}
  	return n
  }
46b2215eb   Marko Tikvić   lintfix
72
  // StringToBool ...
cacf57bd4   Marko Tikvić   merging with /uti...
73
  func StringToBool(s string) bool {
70f2e7ead   Marko Tikvić   simplified
74
75
  	res, _ := strconv.ParseBool(s)
  	return res
cacf57bd4   Marko Tikvić   merging with /uti...
76
  }
46b2215eb   Marko Tikvić   lintfix
77
  // BoolToString ...
cacf57bd4   Marko Tikvić   merging with /uti...
78
79
80
  func BoolToString(b bool) string {
  	return fmt.Sprintf("%b", b)
  }
46b2215eb   Marko Tikvić   lintfix
81
  // StringSliceContains ...
cacf57bd4   Marko Tikvić   merging with /uti...
82
83
84
85
86
87
88
89
  func StringSliceContains(slice []string, s string) bool {
  	for i := range slice {
  		if slice[i] == s {
  			return true
  		}
  	}
  	return false
  }
dd72ba20d   Marko Tikvić   CharAt
90

3173b06a4   Marko Tikvić   SplitString()
91
92
93
94
95
96
97
98
99
  func SplitString(s, sep string) (res []string) {
  	parts := strings.Split(s, sep)
  	for _, p := range parts {
  		if p != "" {
  			res = append(res, p)
  		}
  	}
  	return res
  }
46b2215eb   Marko Tikvić   lintfix
100
  // StringAt ...
dd72ba20d   Marko Tikvić   CharAt
101
102
103
104
105
106
107
  func StringAt(s string, index int) string {
  	if len(s)-1 < index || index < 0 {
  		return ""
  	}
  
  	return string(s[index])
  }
6cc94a06e   Marko Tikvić   cleanup
108
  func StringAtRune(s string, index int) string {
0c71c5487   Marko Tikvić   minor changes
109
110
111
  	str := []rune(s)
  	if index < len(str) {
  		return string(str[index])
6cc94a06e   Marko Tikvić   cleanup
112
113
114
  	}
  	return ""
  }
a8f0d5a63   Marko Tikvić   SplitText
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
  // SplitText ...
  func SplitText(s string, maxLen int) (lines []string) {
  	runes := []rune(s)
  
  	i, start, sep, l := 0, 0, 0, 0
  	for i = 0; i < len(runes); i++ {
  		c := runes[i]
  
  		if unicode.IsSpace(c) {
  			sep = i
  		}
  
  		if c == '
  ' {
  			if start != sep {
  				lines = append(lines, string(runes[start:sep]))
  			}
  			start = i
  			sep = i
  			l = 0
  		} else if l >= maxLen {
  			if start != sep {
  				lines = append(lines, string(runes[start:sep]))
  				sep = i
  				start = i - 1
  				l = 0
  			}
  		} else {
  			l++
  		}
  	}
  	if start != i-1 {
  		lines = append(lines, string(runes[start:i-1]))
  	}
  
  	return lines
  }
6cc94a06e   Marko Tikvić   cleanup
152
153
  func CutTextWith(txt string, maxLen int, tail string) string {
  	if len(txt) < maxLen || len(txt) <= len(tail) {
1c83dc241   Marko Tikvić   GUID and timer
154
155
  		return txt
  	}
6cc94a06e   Marko Tikvić   cleanup
156
  	return txt[:maxLen-3] + tail
1c83dc241   Marko Tikvić   GUID and timer
157
  }
6cc94a06e   Marko Tikvić   cleanup
158
  func LimitTextWith(txt string, maxLen int, tail string) string {
b80ee4b2b   Marko Tikvić   new stuff
159
160
161
  	if len(txt) <= maxLen {
  		return txt
  	}
6cc94a06e   Marko Tikvić   cleanup
162
  	return txt[:maxLen] + tail
b80ee4b2b   Marko Tikvić   new stuff
163
  }
46b2215eb   Marko Tikvić   lintfix
164
  // SplitStringAtWholeWords ...
e95e68362   Marko Tikvić   merged string san...
165
166
167
168
  func SplitStringAtWholeWords(s string, maxLen int) (res []string) {
  	parts := strings.Split(s, " ")
  
  	res = append(res, parts[0])
dd72ba20d   Marko Tikvić   CharAt
169
  	i := 0
e95e68362   Marko Tikvić   merged string san...
170
171
172
173
174
175
176
177
178
179
180
181
182
  	for j := 1; j < len(parts); j++ {
  		p := strings.TrimSpace(parts[j])
  		if len(p) > maxLen {
  			// TODO(marko): check if maxLen is >= 3
  			p = p[0 : maxLen-3]
  			p += "..."
  		}
  		if len(res[i])+len(p)+1 <= maxLen {
  			res[i] += " " + p
  		} else {
  			res = append(res, p)
  			i++
  		}
dd72ba20d   Marko Tikvić   CharAt
183
  	}
e95e68362   Marko Tikvić   merged string san...
184
185
  
  	return res
dd72ba20d   Marko Tikvić   CharAt
186
  }
b80ee4b2b   Marko Tikvić   new stuff
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  
  // StringToInt64 ...
  func StringToInt64(s string) int64 {
  	i, _ := strconv.ParseInt(s, 10, 64)
  	return i
  }
  
  // StringToFloat64 ...
  func StringToFloat64(s string) float64 {
  	f, _ := strconv.ParseFloat(s, 64)
  	return f
  }
  
  func StringToValidInt64(s string) (int64, bool) {
  	i, err := strconv.ParseInt(s, 10, 64)
  	if err != nil {
  		return i, false
  	}
  	return i, true
  }