Changeset 41e20ae for apps


Ignore:
Timestamp:
Jul 25, 2018 3:34:23 PM (2 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
0bcf9ca
Parents:
a6c506a
Message:

Utils: Don't truncate at a ZWJ

File:
1 edited

Legend:

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

    ra6c506a r41e20ae  
    6666      * Truncate a String.
    6767      * Same as s.substring(0, len) except that
    68       * it won't split a surrogate pair.
     68      * it won't split a surrogate pair or at a ZWJ.
    6969      *
    7070      * @param s non-null
     71      * @param len greater than zero
    7172      * @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
     73      *           the char at len-1 is not a high surrogate
     74      *           or the char at len-1 or len is not a zero-width joiner;
     75      *           s.substring(0, len+1 or len+2) if it is
    7476      * @since 0.9.33
    7577      */
     
    7779        if (s.length() <= len)
    7880            return s;
    79         if (Character.isHighSurrogate(s.charAt(len - 1)))
     81        char c = s.charAt(len - 1);
     82        // https://en.wikipedia.org/wiki/Zero-width_joiner
     83        if (Character.isHighSurrogate(c) || c == 0x200D)
    8084            return s.substring(0, len + 1);
     85        if (s.charAt(len) == 0x200D)
     86            return s.substring(0, len + 2);
    8187        return s.substring(0, len);
    8288    }
Note: See TracChangeset for help on using the changeset viewer.