Optimized lpad, thanks to Radim Kubacki for the code!

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4930 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2007-01-09 13:39:11 +00:00
parent d4d8d5be61
commit 6bc6397ed6
2 changed files with 10 additions and 14 deletions

View File

@ -284,9 +284,9 @@ public class Benchmark {
while (buf2.length() <= 50) {
buf2.append(" ");
}
buf2.append(StringUtil.lpad(MessageFormat.format("{0,number,0.00000}", new Object[]{new Double(benchmarkResult.getTime()/1000000000.0)}), 8, " "));
buf2.append(StringUtil.lpad(MessageFormat.format("{0,number,0.00000}", new Object[]{new Double(benchmarkResult.getTime()/1000000000.0)}), 8));
if (benchmarkResult.getType() <= TYPE_RULE_CHAIN_RULE) {
buf2.append(StringUtil.lpad(MessageFormat.format("{0,number,###,###,###,###,###}", new Object[]{new Long(benchmarkResult.getCount())}), 20, " "));
buf2.append(StringUtil.lpad(MessageFormat.format("{0,number,###,###,###,###,###}", new Object[]{new Long(benchmarkResult.getCount())}), 20));
}
switch (benchmarkResult.getType()) {
case TYPE_RULE:

View File

@ -263,19 +263,15 @@ public class StringUtil {
* Left pads a string.
* @param s The String to pad
* @param length The desired minimum length of the resulting padded String
* @param pad The String to pad with
* @return The resulting left padded String
*/
public static String lpad(String s, int length, String pad) {
if (pad == null || pad.length() == 0) {
pad = " ";
}
StringBuffer buf = new StringBuffer(length);
for (int i = 0; i < length - s.length();) {
buf.append(pad);
i+= pad.length();
}
buf.append(s);
return buf.toString();
public static String lpad(String s, int length) {
String res = s;
if (length - s.length() > 0) {
char [] arr = new char[length - s.length()];
java.util.Arrays.fill(arr, ' ');
res = new StringBuffer(length).append(arr).append(s).toString();
}
return res;
}
}