From f0b9894e48b7c3d35babed960d4fef834fcf2071 Mon Sep 17 00:00:00 2001 From: zzz Date: Thu, 1 Mar 2018 16:26:31 +0000 Subject: [PATCH] Util: Warn on dubious split() regex --- core/java/src/net/i2p/data/DataHelper.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index 5135de3fe2..9f7fef83eb 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -1951,7 +1951,7 @@ public class DataHelper { * This saves about 10 microseconds (Bulldozer) on subsequent invocations. * * @param s non-null - * @param regex non-null + * @param regex non-null, don't forget to enclose multiple choices with [] * @throws java.util.regex.PatternSyntaxException unchecked * @since 0.9.24 */ @@ -1966,7 +1966,7 @@ public class DataHelper { * This saves about 10 microseconds (Bulldozer) on subsequent invocations. * * @param s non-null - * @param regex non-null + * @param regex non-null, don't forget to enclose multiple choices with [] * @param limit result threshold * @throws java.util.regex.PatternSyntaxException unchecked * @since 0.9.24 @@ -1974,6 +1974,11 @@ public class DataHelper { public static String[] split(String s, String regex, int limit) { Pattern p = patterns.get(regex); if (p == null) { + // catches easy mistake, and also swapping the args by mistake + if (regex.length() > 1 && !regex.startsWith("[") && !regex.equals("\r\n")) { + //(new Exception("Warning: Split on regex: \"" + regex + "\" should probably be enclosed with []")).printStackTrace(); + System.out.println("Warning: Split on regex: \"" + regex + "\" should probably be enclosed with []"); + } p = Pattern.compile(regex); patterns.putIfAbsent(regex, p); }