I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 41e20ae7 authored by zzz's avatar zzz
Browse files

Utils: Don't truncate at a ZWJ

parent a6c506a1
No related branches found
No related tags found
No related merge requests found
...@@ -65,19 +65,25 @@ public class ServletUtil { ...@@ -65,19 +65,25 @@ public class ServletUtil {
/** /**
* Truncate a String. * Truncate a String.
* Same as s.substring(0, len) except that * Same as s.substring(0, len) except that
* it won't split a surrogate pair. * it won't split a surrogate pair or at a ZWJ.
* *
* @param s non-null * @param s non-null
* @param len greater than zero
* @return s if shorter; s.substring(0, len) if * @return s if shorter; s.substring(0, len) if
* the char at len-1 is not a high surrogate; * the char at len-1 is not a high surrogate
* s.substring(0, len+1) if it is * or the char at len-1 or len is not a zero-width joiner;
* s.substring(0, len+1 or len+2) if it is
* @since 0.9.33 * @since 0.9.33
*/ */
public static String truncate(String s, int len) { public static String truncate(String s, int len) {
if (s.length() <= len) if (s.length() <= len)
return s; return s;
if (Character.isHighSurrogate(s.charAt(len - 1))) char c = s.charAt(len - 1);
// https://en.wikipedia.org/wiki/Zero-width_joiner
if (Character.isHighSurrogate(c) || c == 0x200D)
return s.substring(0, len + 1); return s.substring(0, len + 1);
if (s.charAt(len) == 0x200D)
return s.substring(0, len + 2);
return s.substring(0, len); return s.substring(0, len);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment