diff --git a/string_util.go b/string_util.go index cf9b555..1b915e2 100644 --- a/string_util.go +++ b/string_util.go @@ -5,6 +5,8 @@ import ( "strconv" "strings" "unicode" + + "golang.org/x/exp/utf8string" ) const sanitisationPatern = "\"';&*<>=\\`:" @@ -112,6 +114,15 @@ func StringAt(s string, index int) string { return string(s[index]) } +func StringAtRune(s string, index int) string { + str := utf8string.NewString(s) + max := str.RuneCount() + if index < max { + return string(str.At(index)) + } + return "" +} + // SplitText ... func SplitText(s string, maxLen int) (lines []string) { runes := []rune(s) @@ -149,38 +160,20 @@ func SplitText(s string, maxLen int) (lines []string) { return lines } -func CutTextWithThreeDots(txt string, maxLen int) string { - if len(txt) < maxLen || len(txt) <= 3 { - return txt - } - - return txt[:maxLen-3] + "..." -} - -const threeDots = "\u2056\u2056\u2056" - -func LimitTextWithThreeDots(txt string, maxLen int) string { - if len(txt) <= maxLen { - return txt - } - - return txt[:maxLen] + threeDots -} - -func LimitMSWordTextWithThreeDots(txt string, maxLen int) string { - if len(txt) <= maxLen { +func CutTextWith(txt string, maxLen int, tail string) string { + if len(txt) < maxLen || len(txt) <= len(tail) { return txt } - return txt[:maxLen] + "..." + return txt[:maxLen-3] + tail } -func ThreeDots(txt string, maxLen int) string { +func LimitTextWith(txt string, maxLen int, tail string) string { if len(txt) <= maxLen { return txt } - return txt[:maxLen] + "..." + return txt[:maxLen] + tail } // SplitStringAtWholeWords ...