Skip to content

Commit feac7d3

Browse files
author
Henri Yandell
committed
Bug #20652 fixed.
Submitted by: Fredrik Westermarck git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137370 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0525da4 commit feac7d3

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/java/org/apache/commons/lang/StringUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
* @author Arun Mammen Thomas
7878
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
7979
* @since 1.0
80-
* @version $Id: StringUtils.java,v 1.46 2003/06/08 14:10:54 scolebourne Exp $
80+
* @version $Id: StringUtils.java,v 1.47 2003/06/21 22:24:55 bayard Exp $
8181
*/
8282
public class StringUtils {
8383

@@ -1048,6 +1048,9 @@ public static String chop(String str) {
10481048
*/
10491049
public static String chopNewline(String str) {
10501050
int lastIdx = str.length() - 1;
1051+
if (lastIdx == 0) {
1052+
return "";
1053+
}
10511054
char last = str.charAt(lastIdx);
10521055
if (last == '\n') {
10531056
if (str.charAt(lastIdx - 1) == '\r') {

src/test/org/apache/commons/lang/StringUtilsTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
7070
* @author Holger Krauth
7171
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
72-
* @version $Id: StringUtilsTest.java,v 1.19 2003/04/16 04:37:33 bayard Exp $
72+
* @version $Id: StringUtilsTest.java,v 1.20 2003/06/21 22:24:56 bayard Exp $
7373
*/
7474
public class StringUtilsTest extends TestCase {
7575

@@ -333,6 +333,27 @@ public void testChomp() {
333333
"foo", StringUtils.chomp("foo", "foooo"));
334334
}
335335

336+
public void testChopNewLine() {
337+
338+
String[][] newLineCases = {
339+
{ FOO + "\r\n", FOO } ,
340+
{ FOO + "\n" , FOO } ,
341+
{ FOO + "\r", FOO + "\r" },
342+
{ FOO, FOO },
343+
{ FOO + "\n" + FOO , FOO + "\n" + FOO },
344+
{ FOO + "\n\n", FOO + "\n"},
345+
{ "\n", "" },
346+
{ "\r\n", "" }
347+
};
348+
349+
for (int i = 0; i < newLineCases.length; i++) {
350+
String original = newLineCases[i][0];
351+
String expectedResult = newLineCases[i][1];
352+
assertEquals("chopNewline(String) failed",
353+
expectedResult, StringUtils.chopNewline(original));
354+
}
355+
}
356+
336357
public void testSliceFunctions() {
337358

338359
String[][] sliceCases = {

0 commit comments

Comments
 (0)