summaryrefslogtreecommitdiffstats
path: root/bridge/helper/helper.go
diff options
context:
space:
mode:
authorDuco van Amstel <duco.vanamstel@gmail.com>2018-11-14 21:43:52 +0000
committerWim <wim@42.be>2018-11-14 22:43:52 +0100
commit85564a35fd507fca986065c38262a71764ed41a7 (patch)
treede3f51e4ec07b3be3b03197acfbbf31f3d69cb74 /bridge/helper/helper.go
parent09713d40ba8b78671964efe83cdd1da124d44114 (diff)
downloadmatterbridge-msglm-85564a35fd507fca986065c38262a71764ed41a7.tar.gz
matterbridge-msglm-85564a35fd507fca986065c38262a71764ed41a7.tar.bz2
matterbridge-msglm-85564a35fd507fca986065c38262a71764ed41a7.zip
Fix IRC line splitting. Closes #584 (#587)
Diffstat (limited to 'bridge/helper/helper.go')
-rw-r--r--bridge/helper/helper.go42
1 files changed, 34 insertions, 8 deletions
diff --git a/bridge/helper/helper.go b/bridge/helper/helper.go
index dab2bb47..bd5e140e 100644
--- a/bridge/helper/helper.go
+++ b/bridge/helper/helper.go
@@ -40,16 +40,42 @@ func DownloadFileAuth(url string, auth string) (*[]byte, error) {
return &data, nil
}
-func SplitStringLength(input string, length int) string {
- a := []rune(input)
- str := ""
- for i, r := range a {
- str += string(r)
- if i > 0 && (i+1)%length == 0 {
- str += "\n"
+// GetSubLines splits messages in newline-delimited lines. If maxLineLength is
+// specified as non-zero GetSubLines will and also clip long lines to the
+// maximum length and insert a warning marker that the line was clipped.
+//
+// TODO: The current implementation has the inconvenient that it disregards
+// word boundaries when splitting but this is hard to solve without potentially
+// breaking formatting and other stylistic effects.
+func GetSubLines(message string, maxLineLength int) []string {
+ const clippingMessage = " <clipped message>"
+
+ var lines []string
+ for _, line := range strings.Split(strings.TrimSpace(message), "\n") {
+ if maxLineLength == 0 || len([]byte(line)) <= maxLineLength {
+ lines = append(lines, line)
+ continue
}
+
+ // !!! WARNING !!!
+ // Before touching the splitting logic below please ensure that you PROPERLY
+ // understand how strings, runes and range loops over strings work in Go.
+ // A good place to start is to read https://blog.golang.org/strings. :-)
+ var splitStart int
+ var startOfPreviousRune int
+ for i := range line {
+ if i-splitStart > maxLineLength-len([]byte(clippingMessage)) {
+ lines = append(lines, line[splitStart:startOfPreviousRune]+clippingMessage)
+ splitStart = startOfPreviousRune
+ }
+ startOfPreviousRune = i
+ }
+ // This last append is safe to do without looking at the remaining byte-length
+ // as we assume that the byte-length of the last rune will never exceed that of
+ // the byte-length of the clipping message.
+ lines = append(lines, line[splitStart:])
}
- return str
+ return lines
}
// handle all the stuff we put into extra