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