Commit 70f2e7ead2d7a1269e7806c42151dac3e3793a29

Authored by Marko Tikvić
1 parent e9419e37db
Exists in master

simplified

Showing 1 changed file with 3 additions and 5 deletions   Show diff stats
1 package webutility 1 package webutility
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "strconv"
5 "strings" 6 "strings"
6 ) 7 )
7 8
8 const sanitisationPatern = "\"';&*<>=\\`:" 9 const sanitisationPatern = "\"';&*<>=\\`:"
9 10
10 // SanitiseString removes characters from s found in patern and returns new modified string. 11 // SanitiseString removes characters from s found in patern and returns new modified string.
11 func SanitiseString(s string) string { 12 func SanitiseString(s string) string {
12 return ReplaceAny(s, sanitisationPatern, "") 13 return ReplaceAny(s, sanitisationPatern, "")
13 } 14 }
14 15
15 // IsWrappedWith ... 16 // IsWrappedWith ...
16 func IsWrappedWith(src, begin, end string) bool { 17 func IsWrappedWith(src, begin, end string) bool {
17 return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) 18 return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end)
18 } 19 }
19 20
20 // ParseInt64Arr ... 21 // ParseInt64Arr ...
21 func ParseInt64Arr(s, sep string) (arr []int64) { 22 func ParseInt64Arr(s, sep string) (arr []int64) {
22 s = strings.TrimSpace(s) 23 s = strings.TrimSpace(s)
23 if s != "" { 24 if s != "" {
24 parts := strings.Split(s, sep) 25 parts := strings.Split(s, sep)
25 arr = make([]int64, len(parts)) 26 arr = make([]int64, len(parts))
26 for i, p := range parts { 27 for i, p := range parts {
27 num := StringToInt64(p) 28 num := StringToInt64(p)
28 arr[i] = num 29 arr[i] = num
29 } 30 }
30 } 31 }
31 32
32 return arr 33 return arr
33 } 34 }
34 35
35 // Int64SliceToString ... 36 // Int64SliceToString ...
36 func Int64SliceToString(arr []int64) (s string) { 37 func Int64SliceToString(arr []int64) (s string) {
37 if len(arr) == 0 { 38 if len(arr) == 0 {
38 return "" 39 return ""
39 } 40 }
40 41
41 s += fmt.Sprintf("%d", arr[0]) 42 s += fmt.Sprintf("%d", arr[0])
42 for i := 1; i < len(arr); i++ { 43 for i := 1; i < len(arr); i++ {
43 s += fmt.Sprintf(",%d", arr[i]) 44 s += fmt.Sprintf(",%d", arr[i])
44 } 45 }
45 46
46 return s 47 return s
47 } 48 }
48 49
49 // CombineStrings ... 50 // CombineStrings ...
50 func CombineStrings(s1, s2, s3 string) string { 51 func CombineStrings(s1, s2, s3 string) string {
51 s1 = strings.TrimSpace(s1) 52 s1 = strings.TrimSpace(s1)
52 s2 = strings.TrimSpace(s2) 53 s2 = strings.TrimSpace(s2)
53 54
54 if s1 != "" && s2 != "" { 55 if s1 != "" && s2 != "" {
55 s1 += s3 + s2 56 s1 += s3 + s2
56 } else { 57 } else {
57 s1 += s2 58 s1 += s2
58 } 59 }
59 60
60 return s1 61 return s1
61 } 62 }
62 63
63 // ReplaceAny replaces any of the characters from patern found in s with r and returns a new resulting string. 64 // ReplaceAny replaces any of the characters from patern found in s with r and returns a new resulting string.
64 func ReplaceAny(s, patern, r string) (n string) { 65 func ReplaceAny(s, patern, r string) (n string) {
65 n = s 66 n = s
66 for _, c := range patern { 67 for _, c := range patern {
67 n = strings.Replace(n, string(c), r, -1) 68 n = strings.Replace(n, string(c), r, -1)
68 } 69 }
69 return n 70 return n
70 } 71 }
71 72
72 func StringToBool(s string) bool { 73 func StringToBool(s string) bool {
73 s = strings.ToLower(s) 74 res, _ := strconv.ParseBool(s)
74 if s == "true" { 75 return res
75 return true
76 }
77 return false
78 } 76 }
79 77
80 func BoolToString(b bool) string { 78 func BoolToString(b bool) string {
81 return fmt.Sprintf("%b", b) 79 return fmt.Sprintf("%b", b)
82 } 80 }
83 81
84 func StringSliceContains(slice []string, s string) bool { 82 func StringSliceContains(slice []string, s string) bool {
85 for i := range slice { 83 for i := range slice {
86 if slice[i] == s { 84 if slice[i] == s {
87 return true 85 return true
88 } 86 }
89 } 87 }
90 return false 88 return false
91 } 89 }
92 90
93 func StringAt(s string, index int) string { 91 func StringAt(s string, index int) string {
94 if len(s)-1 < index || index < 0 { 92 if len(s)-1 < index || index < 0 {
95 return "" 93 return ""
96 } 94 }
97 95
98 return string(s[index]) 96 return string(s[index])
99 } 97 }
100 98
101 func SplitStringAtWholeWords(s string, maxLen int) (res []string) { 99 func SplitStringAtWholeWords(s string, maxLen int) (res []string) {
102 parts := strings.Split(s, " ") 100 parts := strings.Split(s, " ")
103 101
104 res = append(res, parts[0]) 102 res = append(res, parts[0])
105 i := 0 103 i := 0
106 for j := 1; j < len(parts); j++ { 104 for j := 1; j < len(parts); j++ {
107 p := strings.TrimSpace(parts[j]) 105 p := strings.TrimSpace(parts[j])
108 if len(p) > maxLen { 106 if len(p) > maxLen {
109 // TODO(marko): check if maxLen is >= 3 107 // TODO(marko): check if maxLen is >= 3
110 p = p[0 : maxLen-3] 108 p = p[0 : maxLen-3]
111 p += "..." 109 p += "..."
112 } 110 }
113 if len(res[i])+len(p)+1 <= maxLen { 111 if len(res[i])+len(p)+1 <= maxLen {
114 res[i] += " " + p 112 res[i] += " " + p
115 } else { 113 } else {
116 res = append(res, p) 114 res = append(res, p)
117 i++ 115 i++
118 } 116 }
119 } 117 }
120 118
121 return res 119 return res
122 } 120 }