-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relax exceptions in left(), right() and mid()
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137552 13f79535-47bb-0310-9956-ffa450edef68
- Loading branch information
Stephen Colebourne
committed
Aug 1, 2003
1 parent
f085c58
commit 9a51cf5
Showing
2 changed files
with
26 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,7 +144,7 @@ | |
* @author <a href="mailto:[email protected]">Gary Gregory</a> | ||
* @author Phil Steitz | ||
* @since 1.0 | ||
* @version $Id: StringUtils.java,v 1.87 2003/08/01 23:01:52 scolebourne Exp $ | ||
* @version $Id: StringUtils.java,v 1.88 2003/08/01 23:11:55 scolebourne Exp $ | ||
*/ | ||
public class StringUtils { | ||
// Performance testing notes (JDK 1.4, Jul03, scolebourne) | ||
|
@@ -1431,7 +1431,7 @@ public static String substring(String str, int start, int end) { | |
* | ||
* <pre> | ||
* StringUtils.left(null, *) = null | ||
* StringUtils.left(*, -ve) = IllegalArgumentException | ||
* StringUtils.left(*, -ve) = "" | ||
* StringUtils.left("", *) = "" | ||
* StringUtils.left("abc", 0) = "" | ||
* StringUtils.left("abc", 2) = "ab" | ||
|
@@ -1441,14 +1441,13 @@ public static String substring(String str, int start, int end) { | |
* @param str the String to get the leftmost characters from, may be null | ||
* @param len the length of the required String, must be zero or positive | ||
* @return the leftmost characters, <code>null</code> if null String input | ||
* @throws IllegalArgumentException if len is less than zero | ||
*/ | ||
public static String left(String str, int len) { | ||
if (str == null) { | ||
return null; | ||
} | ||
if (len < 0) { | ||
throw new IllegalArgumentException("Requested String length " + len + " is less than zero"); | ||
return ""; | ||
} | ||
if (str.length() <= len) { | ||
return str; | ||
|
@@ -1466,7 +1465,7 @@ public static String left(String str, int len) { | |
* | ||
* <pre> | ||
* StringUtils.right(null, *) = null | ||
* StringUtils.right(*, -ve) = IllegalArgumentException | ||
* StringUtils.right(*, -ve) = "" | ||
* StringUtils.right("", *) = "" | ||
* StringUtils.right("abc", 0) = "" | ||
* StringUtils.right("abc", 2) = "bc" | ||
|
@@ -1476,14 +1475,13 @@ public static String left(String str, int len) { | |
* @param str the String to get the rightmost characters from, may be null | ||
* @param len the length of the required String, must be zero or positive | ||
* @return the rightmost characters, <code>null</code> if null String input | ||
* @throws IllegalArgumentException if len is less than zero | ||
*/ | ||
public static String right(String str, int len) { | ||
if (str == null) { | ||
return null; | ||
} | ||
if (len < 0) { | ||
throw new IllegalArgumentException("Requested String length " + len + " is less than zero"); | ||
return ""; | ||
} | ||
if (str.length() <= len) { | ||
return str; | ||
|
@@ -1502,7 +1500,7 @@ public static String right(String str, int len) { | |
* | ||
* <pre> | ||
* StringUtils.mid(null, *, *) = null | ||
* StringUtils.mid(*, *, -ve) = IllegalArgumentException | ||
* StringUtils.mid(*, *, -ve) = "" | ||
* StringUtils.mid("", 0, *) = "" | ||
* StringUtils.mid("abc", 0, 2) = "ab" | ||
* StringUtils.mid("abc", 0, 4) = "abc" | ||
|
@@ -1515,21 +1513,17 @@ public static String right(String str, int len) { | |
* @param pos the position to start from, negative treated as zero | ||
* @param len the length of the required String, must be zero or positive | ||
* @return the middle characters, <code>null</code> if null String input | ||
* @throws IllegalArgumentException if len is less than zero | ||
*/ | ||
public static String mid(String str, int pos, int len) { | ||
if (str == null) { | ||
return null; | ||
} | ||
if (pos > str.length()) { | ||
if (len < 0 || pos > str.length()) { | ||
return ""; | ||
} | ||
if (pos < 0) { | ||
pos = 0; | ||
} | ||
if (len < 0) { | ||
throw new IllegalArgumentException("Requested String length " + len + " is less than zero"); | ||
} | ||
if (str.length() <= (pos + len)) { | ||
return str.substring(pos); | ||
} else { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,7 @@ | |
* @author <a href="mailto:[email protected]">Stephen Colebourne</a> | ||
* @author <a href="mailto:[email protected]">Ringo De Smet</a> | ||
* @author Phil Steitz | ||
* @version $Id: StringUtilsSubstringTest.java,v 1.11 2003/08/01 22:05:43 scolebourne Exp $ | ||
* @version $Id: StringUtilsSubstringTest.java,v 1.12 2003/08/01 23:11:54 scolebourne Exp $ | ||
*/ | ||
public class StringUtilsSubstringTest extends TestCase { | ||
private static final String FOO = "foo"; | ||
|
@@ -139,39 +139,43 @@ public void testLeft_String() { | |
assertSame(null, StringUtils.left(null, -1)); | ||
assertSame(null, StringUtils.left(null, 0)); | ||
assertSame(null, StringUtils.left(null, 2)); | ||
assertSame("", StringUtils.left("", 0)); | ||
assertSame("", StringUtils.left("", 2)); | ||
|
||
assertEquals("", StringUtils.left("", -1)); | ||
assertEquals("", StringUtils.left("", 0)); | ||
assertEquals("", StringUtils.left("", 2)); | ||
|
||
assertEquals("", StringUtils.left(FOOBAR, -1)); | ||
assertEquals("", StringUtils.left(FOOBAR, 0)); | ||
assertEquals(FOO, StringUtils.left(FOOBAR, 3)); | ||
assertSame(FOOBAR, StringUtils.left(FOOBAR, 80)); | ||
try { | ||
StringUtils.left(FOOBAR, -1); | ||
fail(); | ||
} catch (IllegalArgumentException ex) {} | ||
} | ||
|
||
public void testRight_String() { | ||
assertSame(null, StringUtils.right(null, -1)); | ||
assertSame(null, StringUtils.right(null, 0)); | ||
assertSame(null, StringUtils.right(null, 2)); | ||
assertSame("", StringUtils.right("", 0)); | ||
assertSame("", StringUtils.right("", 2)); | ||
|
||
assertEquals("", StringUtils.right("", -1)); | ||
assertEquals("", StringUtils.right("", 0)); | ||
assertEquals("", StringUtils.right("", 2)); | ||
|
||
assertEquals("", StringUtils.right(FOOBAR, -1)); | ||
assertEquals("", StringUtils.right(FOOBAR, 0)); | ||
assertEquals(BAR, StringUtils.right(FOOBAR, 3)); | ||
assertSame(FOOBAR, StringUtils.right(FOOBAR, 80)); | ||
try { | ||
StringUtils.right(FOOBAR, -1); | ||
fail(); | ||
} catch (IllegalArgumentException ex) {} | ||
} | ||
|
||
public void testMid_String() { | ||
assertSame(null, StringUtils.mid(null, -1, 0)); | ||
assertSame(null, StringUtils.mid(null, 0, -1)); | ||
assertSame(null, StringUtils.mid(null, 3, 0)); | ||
assertSame(null, StringUtils.mid(null, 3, 2)); | ||
assertSame("", StringUtils.mid("", 0, 0)); | ||
assertSame("", StringUtils.mid("", 0, 2)); | ||
|
||
assertEquals("", StringUtils.mid("", 0, -1)); | ||
assertEquals("", StringUtils.mid("", 0, 0)); | ||
assertEquals("", StringUtils.mid("", 0, 2)); | ||
|
||
assertEquals("", StringUtils.mid(FOOBAR, 3, -1)); | ||
assertEquals("", StringUtils.mid(FOOBAR, 3, 0)); | ||
assertEquals("b", StringUtils.mid(FOOBAR, 3, 1)); | ||
assertEquals(FOO, StringUtils.mid(FOOBAR, 0, 3)); | ||
|
@@ -180,10 +184,6 @@ public void testMid_String() { | |
assertEquals(BAR, StringUtils.mid(FOOBAR, 3, 80)); | ||
assertEquals("", StringUtils.mid(FOOBAR, 9, 3)); | ||
assertEquals(FOO, StringUtils.mid(FOOBAR, -1, 3)); | ||
try { | ||
StringUtils.mid(FOOBAR, 0, -1); | ||
fail(); | ||
} catch (IllegalArgumentException ex) {} | ||
} | ||
|
||
//----------------------------------------------------------------------- | ||
|