Don't use a regexp in splitTextAtChunk
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
It was not reliable, split all the words using spaces and concatenate them again to form each line to be < limit works better.
This commit is contained in:
parent
6b162f1595
commit
7330ed07a1
17
cmd/run.go
17
cmd/run.go
|
@ -7,7 +7,6 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -204,14 +203,20 @@ func parseTelegramChatID(s string) int64 {
|
||||||
|
|
||||||
// Split text in chunks of almost specified size.
|
// Split text in chunks of almost specified size.
|
||||||
func splitTextAtChunk(text string, size int) []string {
|
func splitTextAtChunk(text string, size int) []string {
|
||||||
// FIX: heuristic technique: reduce by 50 characters to make the split for sure under size.
|
words := strings.SplitN(text, " ", -1)
|
||||||
r := fmt.Sprintf(".{1,%d}(\\s|$)", size-50)
|
|
||||||
var splitTextAtChunk = regexp.MustCompile(r)
|
|
||||||
|
|
||||||
chunks := []string{}
|
chunks := []string{}
|
||||||
for _, i := range splitTextAtChunk.FindAllIndex([]byte(text), -1) {
|
var message string
|
||||||
chunks = append(chunks, text[i[0]:i[1]])
|
for _, word := range words {
|
||||||
|
|
||||||
|
if len(message+" "+word) > size {
|
||||||
|
chunks = append(chunks, message)
|
||||||
|
message = word
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
message += " " + word
|
||||||
}
|
}
|
||||||
|
chunks = append(chunks, message)
|
||||||
|
|
||||||
return chunks
|
return chunks
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue