forked from I2P_Developers/i2p.i2p
Util: New util to truncate a string that won't split across a surrogate pair
This commit is contained in:
@@ -1534,7 +1534,7 @@ public class I2PSnarkServlet extends BasicServlet {
|
||||
String basename = snark.getBaseName();
|
||||
String fullBasename = basename;
|
||||
if (basename.length() > MAX_DISPLAYED_FILENAME_LENGTH) {
|
||||
String start = basename.substring(0, MAX_DISPLAYED_FILENAME_LENGTH);
|
||||
String start = ServletUtil.truncate(basename, MAX_DISPLAYED_FILENAME_LENGTH);
|
||||
if (start.indexOf(' ') < 0 && start.indexOf('-') < 0) {
|
||||
// browser has nowhere to break it
|
||||
basename = start + HELLIP;
|
||||
|
@@ -61,4 +61,23 @@ public class ServletUtil {
|
||||
ua.startsWith("SEC-") || ua.startsWith("SonyEricsson") ||
|
||||
ua.startsWith("Vodafone");
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate a String.
|
||||
* Same as s.substring(0, len) except that
|
||||
* it won't split a surrogate pair.
|
||||
*
|
||||
* @param s non-null
|
||||
* @return s if shorter; s.substring(0, len) if
|
||||
* the char at len-1 is not a high surrogate;
|
||||
* s.substring(0, len+1) if it is
|
||||
* @since 0.9.33
|
||||
*/
|
||||
public static String truncate(String s, int len) {
|
||||
if (s.length() <= len)
|
||||
return s;
|
||||
if (Character.isHighSurrogate(s.charAt(len - 1)))
|
||||
return s.substring(0, len + 1);
|
||||
return s.substring(0, len);
|
||||
}
|
||||
}
|
||||
|
@@ -28,6 +28,7 @@ import net.i2p.router.transport.TransportUtil;
|
||||
import net.i2p.router.web.CSSHelper;
|
||||
import net.i2p.router.web.HelperBase;
|
||||
import net.i2p.router.web.NewsHelper;
|
||||
import net.i2p.servlet.util.ServletUtil;
|
||||
import net.i2p.stat.Rate;
|
||||
import net.i2p.stat.RateStat;
|
||||
import net.i2p.util.PortMapper;
|
||||
@@ -575,7 +576,7 @@ public class SummaryHelper extends HelperBase {
|
||||
if (name.length() <= 32)
|
||||
buf.append(DataHelper.escapeHTML(name));
|
||||
else
|
||||
buf.append(DataHelper.escapeHTML(name.substring(0,29))).append("…");
|
||||
buf.append(DataHelper.escapeHTML(ServletUtil.truncate(name, 29))).append("…");
|
||||
buf.append("</a></b></td>\n");
|
||||
LeaseSet ls = _context.netDb().lookupLeaseSetLocally(h);
|
||||
if (ls != null && _context.tunnelManager().getOutboundClientTunnelCount(h) > 0) {
|
||||
|
@@ -43,6 +43,7 @@ import java.util.TimeZone;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.i2p.data.DataHelper;
|
||||
import net.i2p.servlet.util.ServletUtil;
|
||||
import net.i2p.util.SystemVersion;
|
||||
|
||||
/**
|
||||
@@ -334,7 +335,7 @@ class Mail {
|
||||
shortSender = '<' + shortSender + '>'; // add missing <> (but thunderbird doesn't...)
|
||||
boolean trim = shortSender.length() > 35;
|
||||
if (trim)
|
||||
shortSender = shortSender.substring( 0, 32 ).trim();
|
||||
shortSender = ServletUtil.truncate(shortSender, 32).trim();
|
||||
shortSender = html.encode( shortSender );
|
||||
if (trim)
|
||||
shortSender += "…"; // must be after html encode
|
||||
@@ -361,7 +362,7 @@ class Mail {
|
||||
shortSubject = formattedSubject;
|
||||
boolean trim = formattedSubject.length() > 65;
|
||||
if (trim)
|
||||
shortSubject = formattedSubject.substring( 0, 62 ).trim();
|
||||
shortSubject = ServletUtil.truncate(formattedSubject, 62).trim();
|
||||
shortSubject = html.encode( shortSubject );
|
||||
if (trim)
|
||||
shortSubject += "…"; // must be after html encode
|
||||
|
Reference in New Issue
Block a user