Commit e95e6836241d84915a9fea4c24e8e2cb6ea2a0b3

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