fix bugs in status split (wrong character limit, omission of word parts)

This commit is contained in:
gutmet 2020-10-12 10:57:08 +02:00
parent 8f0cb6bd75
commit b21bc5825d

View File

@ -340,23 +340,33 @@ func kind(path string) mediaKind {
}
func splitStatus(status string) []string {
characterLimit := CHARACTER_LIMIT
// Twitter has an insane definition of what counts as a character
// ( see https://developer.twitter.com/en/docs/counting-characters )
// - as a crude approximation, anything outside LATIN-1 halfs the limit
for _, ch := range status {
if ch > 0x10FF {
characterLimit = CHARACTER_LIMIT / 2
break
}
}
asRunes := []rune(status)
split := []string{}
words := strings.Split(status, " ")
s := ""
for _, word := range words {
asRunes := []rune(word)
if s == "" && len(asRunes) <= CHARACTER_LIMIT {
s = word
} else if len(s)+1+len(asRunes) <= CHARACTER_LIMIT {
s = s + " " + word
for len(asRunes) != 0 {
var limit int
if len(asRunes) <= characterLimit {
limit = len(asRunes)
} else {
split = append(split, s)
bound := goutil.IntMin(len(asRunes), CHARACTER_LIMIT)
s = string(asRunes[:bound])
tmp := asRunes[0:characterLimit]
lastSpace := strings.LastIndex(string(tmp), " ")
if lastSpace == -1 {
limit = characterLimit
} else {
limit = lastSpace + 1
}
}
if s != "" {
split = append(split, s)
split = append(split, string(asRunes[0:limit]))
asRunes = asRunes[limit:]
}
return split
}