Changeset 541dae3 for apps/jetty


Ignore:
Timestamp:
Jan 10, 2018 5:39:58 PM (2 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
8d6d1bc
Parents:
50eb93d
Message:

Util: New util to truncate a string that won't split across a surrogate pair

File:
1 edited

Legend:

Unmodified
Added
Removed
  • apps/jetty/java/src/net/i2p/servlet/util/ServletUtil.java

    r50eb93d r541dae3  
    6262            ua.startsWith("Vodafone");
    6363    }
     64
     65    /**
     66      * Truncate a String.
     67      * Same as s.substring(0, len) except that
     68      * it won't split a surrogate pair.
     69      *
     70      * @param s non-null
     71      * @return s if shorter; s.substring(0, len) if
     72      *           the char at len-1 is not a high surrogate;
     73      *           s.substring(0, len+1) if it is
     74      * @since 0.9.33
     75      */
     76    public static String truncate(String s, int len) {
     77        if (s.length() <= len)
     78            return s;
     79        if (Character.isHighSurrogate(s.charAt(len - 1)))
     80            return s.substring(0, len + 1);
     81        return s.substring(0, len);
     82    }
    6483}
Note: See TracChangeset for help on using the changeset viewer.