Commit a8f0d5a63ebe9483bd8fec8865f2e8a9da90de4c
1 parent
84543a02b2
Exists in
master
SplitText
Showing
1 changed file
with
48 additions
and
1 deletions
Show diff stats
string_util.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "strconv" | 5 | "strconv" |
6 | "strings" | 6 | "strings" |
7 | "unicode" | ||
7 | ) | 8 | ) |
8 | 9 | ||
9 | const sanitisationPatern = "\"';&*<>=\\`:" | 10 | const sanitisationPatern = "\"';&*<>=\\`:" |
10 | 11 | ||
11 | // SanitiseString removes characters from s found in patern and returns new modified string. | 12 | // SanitiseString removes characters from s found in patern and returns new modified string. |
12 | func SanitiseString(s string) string { | 13 | func SanitiseString(s string) string { |
13 | return ReplaceAny(s, sanitisationPatern, "") | 14 | return ReplaceAny(s, sanitisationPatern, "") |
14 | } | 15 | } |
15 | 16 | ||
16 | // IsWrappedWith ... | 17 | // IsWrappedWith ... |
17 | func IsWrappedWith(src, begin, end string) bool { | 18 | func IsWrappedWith(src, begin, end string) bool { |
18 | return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) | 19 | return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) |
19 | } | 20 | } |
20 | 21 | ||
21 | // ParseInt64Arr ... | 22 | // ParseInt64Arr ... |
22 | func ParseInt64Arr(s, sep string) (arr []int64) { | 23 | func ParseInt64Arr(s, sep string) (arr []int64) { |
23 | s = strings.TrimSpace(s) | 24 | s = strings.TrimSpace(s) |
24 | if s != "" { | 25 | if s != "" { |
25 | parts := strings.Split(s, sep) | 26 | parts := strings.Split(s, sep) |
26 | arr = make([]int64, len(parts)) | 27 | arr = make([]int64, len(parts)) |
27 | for i, p := range parts { | 28 | for i, p := range parts { |
28 | num := StringToInt64(p) | 29 | num := StringToInt64(p) |
29 | arr[i] = num | 30 | arr[i] = num |
30 | } | 31 | } |
31 | } | 32 | } |
32 | 33 | ||
33 | return arr | 34 | return arr |
34 | } | 35 | } |
35 | 36 | ||
36 | // Int64SliceToString ... | 37 | // Int64SliceToString ... |
37 | func Int64SliceToString(arr []int64) (s string) { | 38 | func Int64SliceToString(arr []int64) (s string) { |
38 | if len(arr) == 0 { | 39 | if len(arr) == 0 { |
39 | return "" | 40 | return "" |
40 | } | 41 | } |
41 | 42 | ||
42 | s += fmt.Sprintf("%d", arr[0]) | 43 | s += fmt.Sprintf("%d", arr[0]) |
43 | for i := 1; i < len(arr); i++ { | 44 | for i := 1; i < len(arr); i++ { |
44 | s += fmt.Sprintf(",%d", arr[i]) | 45 | s += fmt.Sprintf(",%d", arr[i]) |
45 | } | 46 | } |
46 | 47 | ||
47 | return s | 48 | return s |
48 | } | 49 | } |
49 | 50 | ||
50 | // CombineStrings ... | 51 | // CombineStrings ... |
51 | func CombineStrings(s1, s2, s3 string) string { | 52 | func CombineStrings(s1, s2, s3 string) string { |
52 | s1 = strings.TrimSpace(s1) | 53 | s1 = strings.TrimSpace(s1) |
53 | s2 = strings.TrimSpace(s2) | 54 | s2 = strings.TrimSpace(s2) |
54 | 55 | ||
55 | if s1 != "" && s2 != "" { | 56 | if s1 != "" && s2 != "" { |
56 | s1 += s3 + s2 | 57 | s1 += s3 + s2 |
57 | } else { | 58 | } else { |
58 | s1 += s2 | 59 | s1 += s2 |
59 | } | 60 | } |
60 | 61 | ||
61 | return s1 | 62 | return s1 |
62 | } | 63 | } |
63 | 64 | ||
64 | // ReplaceAny replaces any of the characters from patern found in s with r and returns a new resulting string. | 65 | // ReplaceAny replaces any of the characters from patern found in s with r and returns a new resulting string. |
65 | func ReplaceAny(s, patern, r string) (n string) { | 66 | func ReplaceAny(s, patern, r string) (n string) { |
66 | n = s | 67 | n = s |
67 | for _, c := range patern { | 68 | for _, c := range patern { |
68 | n = strings.Replace(n, string(c), r, -1) | 69 | n = strings.Replace(n, string(c), r, -1) |
69 | } | 70 | } |
70 | return n | 71 | return n |
71 | } | 72 | } |
72 | 73 | ||
73 | // StringToBool ... | 74 | // StringToBool ... |
74 | func StringToBool(s string) bool { | 75 | func StringToBool(s string) bool { |
75 | res, _ := strconv.ParseBool(s) | 76 | res, _ := strconv.ParseBool(s) |
76 | return res | 77 | return res |
77 | } | 78 | } |
78 | 79 | ||
79 | // BoolToString ... | 80 | // BoolToString ... |
80 | func BoolToString(b bool) string { | 81 | func BoolToString(b bool) string { |
81 | return fmt.Sprintf("%b", b) | 82 | return fmt.Sprintf("%b", b) |
82 | } | 83 | } |
83 | 84 | ||
84 | // StringSliceContains ... | 85 | // StringSliceContains ... |
85 | func StringSliceContains(slice []string, s string) bool { | 86 | func StringSliceContains(slice []string, s string) bool { |
86 | for i := range slice { | 87 | for i := range slice { |
87 | if slice[i] == s { | 88 | if slice[i] == s { |
88 | return true | 89 | return true |
89 | } | 90 | } |
90 | } | 91 | } |
91 | return false | 92 | return false |
92 | } | 93 | } |
93 | 94 | ||
94 | // StringAt ... | 95 | // StringAt ... |
95 | func StringAt(s string, index int) string { | 96 | func StringAt(s string, index int) string { |
96 | if len(s)-1 < index || index < 0 { | 97 | if len(s)-1 < index || index < 0 { |
97 | return "" | 98 | return "" |
98 | } | 99 | } |
99 | 100 | ||
100 | return string(s[index]) | 101 | return string(s[index]) |
101 | } | 102 | } |
102 | 103 | ||
104 | // SplitText ... | ||
105 | func SplitText(s string, maxLen int) (lines []string) { | ||
106 | runes := []rune(s) | ||
107 | |||
108 | i, start, sep, l := 0, 0, 0, 0 | ||
109 | for i = 0; i < len(runes); i++ { | ||
110 | c := runes[i] | ||
111 | |||
112 | if unicode.IsSpace(c) { | ||
113 | sep = i | ||
114 | } | ||
115 | |||
116 | if c == '\n' { | ||
117 | if start != sep { | ||
118 | lines = append(lines, string(runes[start:sep])) | ||
119 | } | ||
120 | start = i | ||
121 | sep = i | ||
122 | l = 0 | ||
123 | } else if l >= maxLen { | ||
124 | if start != sep { | ||
125 | lines = append(lines, string(runes[start:sep])) | ||
126 | sep = i | ||
127 | start = i - 1 | ||
128 | l = 0 | ||
129 | } | ||
130 | } else { | ||
131 | l++ | ||
132 | } | ||
133 | } | ||
134 | if start != i-1 { | ||
135 | lines = append(lines, string(runes[start:i-1])) | ||
136 | } | ||
137 | |||
138 | return lines | ||
139 | } | ||
140 | |||
141 | const threeDots = "\u2056\u2056\u2056" | ||
142 | |||
143 | func LimitTextWithThreeDots(txt string, maxLen int) string { | ||
144 | if len(txt) <= maxLen { | ||
145 | return txt | ||
146 | } | ||
147 | |||
148 | return txt[:maxLen] + threeDots | ||
149 | } | ||
150 | |||
103 | // SplitStringAtWholeWords ... | 151 | // SplitStringAtWholeWords ... |
104 | func SplitStringAtWholeWords(s string, maxLen int) (res []string) { | 152 | func SplitStringAtWholeWords(s string, maxLen int) (res []string) { |
105 | parts := strings.Split(s, " ") | 153 | parts := strings.Split(s, " ") |
106 | 154 | ||
107 | res = append(res, parts[0]) | 155 | res = append(res, parts[0]) |
108 | i := 0 | 156 | i := 0 |
109 | for j := 1; j < len(parts); j++ { | 157 | for j := 1; j < len(parts); j++ { |
110 | p := strings.TrimSpace(parts[j]) | 158 | p := strings.TrimSpace(parts[j]) |
111 | if len(p) > maxLen { | 159 | if len(p) > maxLen { |
112 | // TODO(marko): check if maxLen is >= 3 | 160 | // TODO(marko): check if maxLen is >= 3 |
113 | p = p[0 : maxLen-3] | 161 | p = p[0 : maxLen-3] |
114 | p += "..." | 162 | p += "..." |
115 | } | 163 | } |
116 | if len(res[i])+len(p)+1 <= maxLen { | 164 | if len(res[i])+len(p)+1 <= maxLen { |
117 | res[i] += " " + p | 165 | res[i] += " " + p |
118 | } else { | 166 | } else { |
119 | res = append(res, p) | 167 | res = append(res, p) |
120 | i++ | 168 | i++ |
121 | } | 169 | } |
122 | } | 170 | } |
123 | 171 | ||
124 | return res | 172 | return res |
125 | } | 173 | } |
126 |