Commit e95e6836241d84915a9fea4c24e8e2cb6ea2a0b3
1 parent
c23b95e2f2
Exists in
master
merged string sanitisation in string utility
Showing
2 changed files
with
33 additions
and
22 deletions
Show diff stats
string_sanitisation.go
string_util.go
... | ... | @@ -5,6 +5,13 @@ import ( |
5 | 5 | "strings" |
6 | 6 | ) |
7 | 7 | |
8 | +const sanitisationPatern = "\"';&*<>=\\`:" | |
9 | + | |
10 | +// SanitiseString removes characters from s found in patern and returns new modified string. | |
11 | +func SanitiseString(s string) string { | |
12 | + return ReplaceAny(s, sanitisationPatern, "") | |
13 | +} | |
14 | + | |
8 | 15 | // IsWrappedWith ... |
9 | 16 | func IsWrappedWith(src, begin, end string) bool { |
10 | 17 | return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) |
... | ... | @@ -27,12 +34,13 @@ func ParseInt64Arr(s, sep string) (arr []int64) { |
27 | 34 | |
28 | 35 | // Int64SliceToString ... |
29 | 36 | func Int64SliceToString(arr []int64) (s string) { |
30 | - for i, num := range arr { | |
31 | - if i == 0 { | |
32 | - s += fmt.Sprintf("%d", num) | |
33 | - } else { | |
34 | - s += fmt.Sprintf(",%d", num) | |
35 | - } | |
37 | + if len(arr) == 0 { | |
38 | + return "" | |
39 | + } | |
40 | + | |
41 | + s += fmt.Sprintf("%d", arr[0]) | |
42 | + for i := 1; i < len(arr); i++ { | |
43 | + s += fmt.Sprintf(",%d", arr[i]) | |
36 | 44 | } |
37 | 45 | |
38 | 46 | return s |
... | ... | @@ -90,14 +98,25 @@ func StringAt(s string, index int) string { |
90 | 98 | return string(s[index]) |
91 | 99 | } |
92 | 100 | |
93 | -func SplitStringAfterN(s string, n, count int) (parts []string) { | |
94 | - rem := s | |
101 | +func SplitStringAtWholeWords(s string, maxLen int) (res []string) { | |
102 | + parts := strings.Split(s, " ") | |
103 | + | |
104 | + res = append(res, parts[0]) | |
95 | 105 | i := 0 |
96 | - for len(rem) > n && (i < count || count < 0) { | |
97 | - parts = append(parts, rem[0:n]) | |
98 | - rem = rem[n:] | |
99 | - i++ | |
106 | + for j := 1; j < len(parts); j++ { | |
107 | + p := strings.TrimSpace(parts[j]) | |
108 | + if len(p) > maxLen { | |
109 | + // TODO(marko): check if maxLen is >= 3 | |
110 | + p = p[0 : maxLen-3] | |
111 | + p += "..." | |
112 | + } | |
113 | + if len(res[i])+len(p)+1 <= maxLen { | |
114 | + res[i] += " " + p | |
115 | + } else { | |
116 | + res = append(res, p) | |
117 | + i++ | |
118 | + } | |
100 | 119 | } |
101 | - parts = append(parts, rem) | |
102 | - return parts | |
120 | + | |
121 | + return res | |
103 | 122 | } | ... | ... |