Renaming the newly public pad(int, char) method to repeat(char, int) [note the swap of param order]. I've also pulled the faster implementation out of repeat(String, int).

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1096419 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2011-04-25 06:38:04 +00:00
parent f0bd811e71
commit a9831e81fa
1 changed files with 13 additions and 22 deletions

View File

@ -4474,18 +4474,13 @@ public class StringUtils {
return str; return str;
} }
if (inputLength == 1 && repeat <= PAD_LIMIT) { if (inputLength == 1 && repeat <= PAD_LIMIT) {
return pad(repeat, str.charAt(0)); return repeat(str.charAt(0), repeat);
} }
int outputLength = inputLength * repeat; int outputLength = inputLength * repeat;
switch (inputLength) { switch (inputLength) {
case 1 : case 1 :
char ch = str.charAt(0); return repeat(str.charAt(0), repeat);
char[] output1 = new char[outputLength];
for (int i = repeat - 1; i >= 0; i--) {
output1[i] = ch;
}
return new String(output1);
case 2 : case 2 :
char ch0 = str.charAt(0); char ch0 = str.charAt(0);
char ch1 = str.charAt(1); char ch1 = str.charAt(1);
@ -4539,9 +4534,9 @@ public class StringUtils {
* to a given length.</p> * to a given length.</p>
* *
* <pre> * <pre>
* StringUtils.padding(0, 'e') = "" * StringUtils.repeat(0, 'e') = ""
* StringUtils.padding(3, 'e') = "eee" * StringUtils.repeat(3, 'e') = "eee"
* StringUtils.padding(-2, 'e') throws IndexOutOfBoundsException * StringUtils.repeat(-2, 'e') = ""
* </pre> * </pre>
* *
* <p>Note: this method doesn't not support padding with * <p>Note: this method doesn't not support padding with
@ -4551,19 +4546,15 @@ public class StringUtils {
* consider using {@link #repeat(String, int)} instead. * consider using {@link #repeat(String, int)} instead.
* </p> * </p>
* *
* @param repeat number of times to repeat delim * @param ch character to repeat
* @param padChar character to repeat * @param repeat number of times to repeat char, negative treated as zero
* @return String with repeated character * @return String with repeated character
* @throws IndexOutOfBoundsException if <code>repeat &lt; 0</code>
* @see #repeat(String, int) * @see #repeat(String, int)
*/ */
public static String pad(int repeat, char padChar) throws IndexOutOfBoundsException { public static String repeat(char ch, int repeat) {
if (repeat < 0) { char[] buf = new char[repeat];
throw new IndexOutOfBoundsException("Cannot pad a negative amount: " + repeat); for (int i = repeat - 1; i >= 0; i--) {
} buf[i] = ch;
final char[] buf = new char[repeat];
for (int i = 0; i < buf.length; i++) {
buf[i] = padChar;
} }
return new String(buf); return new String(buf);
} }
@ -4623,7 +4614,7 @@ public class StringUtils {
if (pads > PAD_LIMIT) { if (pads > PAD_LIMIT) {
return rightPad(str, size, String.valueOf(padChar)); return rightPad(str, size, String.valueOf(padChar));
} }
return str.concat(pad(pads, padChar)); return str.concat(repeat(padChar, pads));
} }
/** /**
@ -4735,7 +4726,7 @@ public class StringUtils {
if (pads > PAD_LIMIT) { if (pads > PAD_LIMIT) {
return leftPad(str, size, String.valueOf(padChar)); return leftPad(str, size, String.valueOf(padChar));
} }
return pad(pads, padChar).concat(str); return repeat(padChar, pads).concat(str);
} }
/** /**