Merge branch 'fix-LANG-1118'

LANG-1118: StringUtils.repeat('z', -1) throws NegativeArraySizeException.
Thanks to Loic Guibert.
This commit is contained in:
Benedikt Ritter 2015-04-28 21:35:34 +02:00
commit 0799f01df1
3 changed files with 12 additions and 0 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
<action issue="LANG-1118" type="fix" dev="britter" due-to="Loic Guibert">StringUtils.repeat('z', -1) throws NegativeArraySizeException</action>
<action issue="LANG-1099" type="add" dev="britter" due-to="Adrian Ber">Add swap and shift operations for arrays to ArrayUtils</action>
<action issue="LANG-979" type="update" dev="britter" due-to="Bruno P. Kinoshita">TypeUtils.parameterizeWithOwner - wrong format descriptor for "invalid number of type parameters".</action>
<action issue="LANG-1112" type="update" dev="britter">MultilineRecursiveToStringStyle largely unusable due to being package-private.</action>

View File

@ -5233,6 +5233,9 @@ public class StringUtils {
* @see #repeat(String, int)
*/
public static String repeat(final char ch, final int repeat) {
if (repeat <= 0) {
return EMPTY;
}
final char[] buf = new char[repeat];
for (int i = repeat - 1; i >= 0; i--) {
buf[i] = ch;

View File

@ -1353,6 +1353,7 @@ public class StringUtilsTest {
assertEquals("", StringUtils.repeat("ab", 0));
assertEquals("", StringUtils.repeat("", 3));
assertEquals("aaa", StringUtils.repeat("a", 3));
assertEquals("", StringUtils.repeat("a", -2));
assertEquals("ababab", StringUtils.repeat("ab", 3));
assertEquals("abcabcabc", StringUtils.repeat("abc", 3));
final String str = StringUtils.repeat("a", 10000); // bigger than pad limit
@ -1374,6 +1375,13 @@ public class StringUtilsTest {
assertEquals("?, ?, ?", StringUtils.repeat("?", ", ", 3));
}
@Test
public void testRepeat_CharInt() {
assertEquals("zzz", StringUtils.repeat('z', 3));
assertEquals("", StringUtils.repeat('z', 0));
assertEquals("", StringUtils.repeat('z', -2));
}
@Test
public void testChop() {