public static String truncate(String s, int length) {
        if (s.codePointCount(0, s.length()) <= length) return s;
        if (length <= 4) return substringByCodepoints(s, 0, length);
        return substringByCodepoints(s, 0, length - 4) + " ...";
    }
Returns a string which is never larger than the given number of code points.
If the string is longer than the given length it will be truncated.
If length is 4 or less the string will be truncated to length.
If length is longer than 4, it will be truncated at length-4 with " ..." added at the end.
vespajlib/src/main/java/com/yahoo/text/Text.java