From e95e6836241d84915a9fea4c24e8e2cb6ea2a0b3 Mon Sep 17 00:00:00 2001 From: "marko.tikvic" Date: Tue, 21 May 2019 14:58:15 +0200 Subject: [PATCH] merged string sanitisation in string utility --- string_sanitisation.go | 8 -------- string_util.go | 47 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 22 deletions(-) delete mode 100644 string_sanitisation.go diff --git a/string_sanitisation.go b/string_sanitisation.go deleted file mode 100644 index 5774efb..0000000 --- a/string_sanitisation.go +++ /dev/null @@ -1,8 +0,0 @@ -package webutility - -const patern = "\"';&*<>=\\`:" - -// SanitiseString removes characters from s found in patern and returns new modified string. -func SanitiseString(s string) string { - return ReplaceAny(s, patern, "") -} diff --git a/string_util.go b/string_util.go index 14cf4e2..03ac492 100644 --- a/string_util.go +++ b/string_util.go @@ -5,6 +5,13 @@ import ( "strings" ) +const sanitisationPatern = "\"';&*<>=\\`:" + +// SanitiseString removes characters from s found in patern and returns new modified string. +func SanitiseString(s string) string { + return ReplaceAny(s, sanitisationPatern, "") +} + // IsWrappedWith ... func IsWrappedWith(src, begin, end string) bool { return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) @@ -27,12 +34,13 @@ func ParseInt64Arr(s, sep string) (arr []int64) { // Int64SliceToString ... func Int64SliceToString(arr []int64) (s string) { - for i, num := range arr { - if i == 0 { - s += fmt.Sprintf("%d", num) - } else { - s += fmt.Sprintf(",%d", num) - } + if len(arr) == 0 { + return "" + } + + s += fmt.Sprintf("%d", arr[0]) + for i := 1; i < len(arr); i++ { + s += fmt.Sprintf(",%d", arr[i]) } return s @@ -90,14 +98,25 @@ func StringAt(s string, index int) string { return string(s[index]) } -func SplitStringAfterN(s string, n, count int) (parts []string) { - rem := s +func SplitStringAtWholeWords(s string, maxLen int) (res []string) { + parts := strings.Split(s, " ") + + res = append(res, parts[0]) i := 0 - for len(rem) > n && (i < count || count < 0) { - parts = append(parts, rem[0:n]) - rem = rem[n:] - i++ + 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++ + } } - parts = append(parts, rem) - return parts + + return res } -- 1.8.1.2