Just tweaking some very common string appending with buffer size prediction

This commit is contained in:
Rob Bygrave committed 2014-06-26 01:15:48 +12:00
1 parent 28cb2e210d
commit 7966d74eb3
7 files changed
+65 -25

No files matched your search

@@ -0,0 +1,38 @@
package com.avaje.ebeaninternal.server.lib.util;
/**
* String utility for adding strings together.
* <p>
* Predicts a decent buffer size to append the strings into.
*/
public class Str {
/**
* Append strings together.
*/
public static String add(String s0, String s1, String ... args) {
// determine a decent buffer size
int len = 16 + s0.length() + s1.length();
for (int i = 0; i < args.length; i++) {
len += args[i].length();
}
// append all the strings into the buffer
StringBuilder sb = new StringBuilder(len);
sb.append(s0).append(s1);
for (int i = 0; i < args.length; i++) {
sb.append(args[i]);
}
return sb.toString();
}
/**
* Append two strings together.
*/
public static String add(String s0, String s1) {
StringBuilder sb = new StringBuilder(s0.length() + s1.length() + 5);
return sb.append(s0).append(s1).toString();
}
}