diff --git a/drivel.go b/drivel.go index 7ad2962..2c68dcd 100644 --- a/drivel.go +++ b/drivel.go @@ -340,23 +340,33 @@ func kind(path string) mediaKind { } func splitStatus(status string) []string { - 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 - } else { - split = append(split, s) - bound := goutil.IntMin(len(asRunes), CHARACTER_LIMIT) - s = string(asRunes[:bound]) + 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 } } - if s != "" { - split = append(split, s) + asRunes := []rune(status) + 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 }