Blame view

string_util.go 2.21 KB
18fcd6d6b   Marko Tikvić   merged with util ...
1
  package webutility
cacf57bd4   Marko Tikvić   merging with /uti...
2
3
4
5
6
  
  import (
  	"fmt"
  	"strings"
  )
e95e68362   Marko Tikvić   merged string san...
7
8
9
10
11
12
  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...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  // 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)
  	if s != "" {
  		parts := strings.Split(s, sep)
  		arr = make([]int64, len(parts))
  		for i, p := range parts {
  			num := StringToInt64(p)
  			arr[i] = num
  		}
  	}
  
  	return arr
  }
  
  // Int64SliceToString ...
  func Int64SliceToString(arr []int64) (s string) {
e95e68362   Marko Tikvić   merged string san...
35
36
37
38
39
40
41
  	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...
42
43
44
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  	}
  
  	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
  }
  
  func StringToBool(s string) bool {
  	s = strings.ToLower(s)
  	if s == "true" {
  		return true
  	}
  	return false
  }
  
  func BoolToString(b bool) string {
  	return fmt.Sprintf("%b", b)
  }
  
  func StringSliceContains(slice []string, s string) bool {
  	for i := range slice {
  		if slice[i] == s {
  			return true
  		}
  	}
  	return false
  }
dd72ba20d   Marko Tikvić   CharAt
90
91
92
93
94
95
96
97
  
  func StringAt(s string, index int) string {
  	if len(s)-1 < index || index < 0 {
  		return ""
  	}
  
  	return string(s[index])
  }
e95e68362   Marko Tikvić   merged string san...
98
99
100
101
  func SplitStringAtWholeWords(s string, maxLen int) (res []string) {
  	parts := strings.Split(s, " ")
  
  	res = append(res, parts[0])
dd72ba20d   Marko Tikvić   CharAt
102
  	i := 0
e95e68362   Marko Tikvić   merged string san...
103
104
105
106
107
108
109
110
111
112
113
114
115
  	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
116
  	}
e95e68362   Marko Tikvić   merged string san...
117
118
  
  	return res
dd72ba20d   Marko Tikvić   CharAt
119
  }