fix bugs in status split (wrong character limit, omission of word parts)
This commit is contained in:
parent
8f0cb6bd75
commit
b21bc5825d
36
drivel.go
36
drivel.go
|
@ -340,23 +340,33 @@ func kind(path string) mediaKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitStatus(status string) []string {
|
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{}
|
split := []string{}
|
||||||
words := strings.Split(status, " ")
|
for len(asRunes) != 0 {
|
||||||
s := ""
|
var limit int
|
||||||
for _, word := range words {
|
if len(asRunes) <= characterLimit {
|
||||||
asRunes := []rune(word)
|
limit = len(asRunes)
|
||||||
if s == "" && len(asRunes) <= CHARACTER_LIMIT {
|
|
||||||
s = word
|
|
||||||
} else if len(s)+1+len(asRunes) <= CHARACTER_LIMIT {
|
|
||||||
s = s + " " + word
|
|
||||||
} else {
|
} else {
|
||||||
split = append(split, s)
|
tmp := asRunes[0:characterLimit]
|
||||||
bound := goutil.IntMin(len(asRunes), CHARACTER_LIMIT)
|
lastSpace := strings.LastIndex(string(tmp), " ")
|
||||||
s = string(asRunes[:bound])
|
if lastSpace == -1 {
|
||||||
|
limit = characterLimit
|
||||||
|
} else {
|
||||||
|
limit = lastSpace + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if s != "" {
|
split = append(split, string(asRunes[0:limit]))
|
||||||
split = append(split, s)
|
asRunes = asRunes[limit:]
|
||||||
}
|
}
|
||||||
return split
|
return split
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user