diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index 750d53edea..672a187d8e 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -37,6 +37,8 @@ import java.util.Iterator; import java.util.Locale; import java.util.Map; import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Pattern; import java.util.zip.Deflater; import net.i2p.I2PAppContext; @@ -1889,4 +1891,38 @@ public class DataHelper { } return rv; } + + /** + * Same as s.split(regex) but caches the compiled pattern for speed. + * This saves about 10 microseconds (Bulldozer) on subsequent invocations. + * + * @param s non-null + * @param regex non-null + * @throws java.util.regex.PatternSyntaxException unchecked + * @since 0.9.24 + */ + public static String[] split(String s, String regex) { + return split(s, regex, 0); + } + + private static final ConcurrentHashMap patterns = new ConcurrentHashMap(); + + /** + * Same as s.split(regex, limit) but caches the compiled pattern for speed. + * This saves about 10 microseconds (Bulldozer) on subsequent invocations. + * + * @param s non-null + * @param regex non-null + * @param limit result threshold + * @throws java.util.regex.PatternSyntaxException unchecked + * @since 0.9.24 + */ + public static String[] split(String s, String regex, int limit) { + Pattern p = patterns.get(regex); + if (p == null) { + p = Pattern.compile(regex); + patterns.putIfAbsent(regex, p); + } + return p.split(s, limit); + } }