Skip to content

Commit 9a51cf5

Browse files
author
Stephen Colebourne
committed
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
1 parent f085c58 commit 9a51cf5

2 files changed

Lines changed: 26 additions & 32 deletions

File tree

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
145145
* @author Phil Steitz
146146
* @since 1.0
147-
* @version $Id: StringUtils.java,v 1.87 2003/08/01 23:01:52 scolebourne Exp $
147+
* @version $Id: StringUtils.java,v 1.88 2003/08/01 23:11:55 scolebourne Exp $
148148
*/
149149
public class StringUtils {
150150
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
@@ -1431,7 +1431,7 @@ public static String substring(String str, int start, int end) {
14311431
*
14321432
* <pre>
14331433
* StringUtils.left(null, *) = null
1434-
* StringUtils.left(*, -ve) = IllegalArgumentException
1434+
* StringUtils.left(*, -ve) = ""
14351435
* StringUtils.left("", *) = ""
14361436
* StringUtils.left("abc", 0) = ""
14371437
* StringUtils.left("abc", 2) = "ab"
@@ -1441,14 +1441,13 @@ public static String substring(String str, int start, int end) {
14411441
* @param str the String to get the leftmost characters from, may be null
14421442
* @param len the length of the required String, must be zero or positive
14431443
* @return the leftmost characters, <code>null</code> if null String input
1444-
* @throws IllegalArgumentException if len is less than zero
14451444
*/
14461445
public static String left(String str, int len) {
14471446
if (str == null) {
14481447
return null;
14491448
}
14501449
if (len < 0) {
1451-
throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
1450+
return "";
14521451
}
14531452
if (str.length() <= len) {
14541453
return str;
@@ -1466,7 +1465,7 @@ public static String left(String str, int len) {
14661465
*
14671466
* <pre>
14681467
* StringUtils.right(null, *) = null
1469-
* StringUtils.right(*, -ve) = IllegalArgumentException
1468+
* StringUtils.right(*, -ve) = ""
14701469
* StringUtils.right("", *) = ""
14711470
* StringUtils.right("abc", 0) = ""
14721471
* StringUtils.right("abc", 2) = "bc"
@@ -1476,14 +1475,13 @@ public static String left(String str, int len) {
14761475
* @param str the String to get the rightmost characters from, may be null
14771476
* @param len the length of the required String, must be zero or positive
14781477
* @return the rightmost characters, <code>null</code> if null String input
1479-
* @throws IllegalArgumentException if len is less than zero
14801478
*/
14811479
public static String right(String str, int len) {
14821480
if (str == null) {
14831481
return null;
14841482
}
14851483
if (len < 0) {
1486-
throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
1484+
return "";
14871485
}
14881486
if (str.length() <= len) {
14891487
return str;
@@ -1502,7 +1500,7 @@ public static String right(String str, int len) {
15021500
*
15031501
* <pre>
15041502
* StringUtils.mid(null, *, *) = null
1505-
* StringUtils.mid(*, *, -ve) = IllegalArgumentException
1503+
* StringUtils.mid(*, *, -ve) = ""
15061504
* StringUtils.mid("", 0, *) = ""
15071505
* StringUtils.mid("abc", 0, 2) = "ab"
15081506
* StringUtils.mid("abc", 0, 4) = "abc"
@@ -1515,21 +1513,17 @@ public static String right(String str, int len) {
15151513
* @param pos the position to start from, negative treated as zero
15161514
* @param len the length of the required String, must be zero or positive
15171515
* @return the middle characters, <code>null</code> if null String input
1518-
* @throws IllegalArgumentException if len is less than zero
15191516
*/
15201517
public static String mid(String str, int pos, int len) {
15211518
if (str == null) {
15221519
return null;
15231520
}
1524-
if (pos > str.length()) {
1521+
if (len < 0 || pos > str.length()) {
15251522
return "";
15261523
}
15271524
if (pos < 0) {
15281525
pos = 0;
15291526
}
1530-
if (len < 0) {
1531-
throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
1532-
}
15331527
if (str.length() <= (pos + len)) {
15341528
return str.substring(pos);
15351529
} else {

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
6565
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
6666
* @author Phil Steitz
67-
* @version $Id: StringUtilsSubstringTest.java,v 1.11 2003/08/01 22:05:43 scolebourne Exp $
67+
* @version $Id: StringUtilsSubstringTest.java,v 1.12 2003/08/01 23:11:54 scolebourne Exp $
6868
*/
6969
public class StringUtilsSubstringTest extends TestCase {
7070
private static final String FOO = "foo";
@@ -139,39 +139,43 @@ public void testLeft_String() {
139139
assertSame(null, StringUtils.left(null, -1));
140140
assertSame(null, StringUtils.left(null, 0));
141141
assertSame(null, StringUtils.left(null, 2));
142-
assertSame("", StringUtils.left("", 0));
143-
assertSame("", StringUtils.left("", 2));
142+
143+
assertEquals("", StringUtils.left("", -1));
144+
assertEquals("", StringUtils.left("", 0));
145+
assertEquals("", StringUtils.left("", 2));
146+
147+
assertEquals("", StringUtils.left(FOOBAR, -1));
144148
assertEquals("", StringUtils.left(FOOBAR, 0));
145149
assertEquals(FOO, StringUtils.left(FOOBAR, 3));
146150
assertSame(FOOBAR, StringUtils.left(FOOBAR, 80));
147-
try {
148-
StringUtils.left(FOOBAR, -1);
149-
fail();
150-
} catch (IllegalArgumentException ex) {}
151151
}
152152

153153
public void testRight_String() {
154154
assertSame(null, StringUtils.right(null, -1));
155155
assertSame(null, StringUtils.right(null, 0));
156156
assertSame(null, StringUtils.right(null, 2));
157-
assertSame("", StringUtils.right("", 0));
158-
assertSame("", StringUtils.right("", 2));
157+
158+
assertEquals("", StringUtils.right("", -1));
159+
assertEquals("", StringUtils.right("", 0));
160+
assertEquals("", StringUtils.right("", 2));
161+
162+
assertEquals("", StringUtils.right(FOOBAR, -1));
159163
assertEquals("", StringUtils.right(FOOBAR, 0));
160164
assertEquals(BAR, StringUtils.right(FOOBAR, 3));
161165
assertSame(FOOBAR, StringUtils.right(FOOBAR, 80));
162-
try {
163-
StringUtils.right(FOOBAR, -1);
164-
fail();
165-
} catch (IllegalArgumentException ex) {}
166166
}
167167

168168
public void testMid_String() {
169169
assertSame(null, StringUtils.mid(null, -1, 0));
170170
assertSame(null, StringUtils.mid(null, 0, -1));
171171
assertSame(null, StringUtils.mid(null, 3, 0));
172172
assertSame(null, StringUtils.mid(null, 3, 2));
173-
assertSame("", StringUtils.mid("", 0, 0));
174-
assertSame("", StringUtils.mid("", 0, 2));
173+
174+
assertEquals("", StringUtils.mid("", 0, -1));
175+
assertEquals("", StringUtils.mid("", 0, 0));
176+
assertEquals("", StringUtils.mid("", 0, 2));
177+
178+
assertEquals("", StringUtils.mid(FOOBAR, 3, -1));
175179
assertEquals("", StringUtils.mid(FOOBAR, 3, 0));
176180
assertEquals("b", StringUtils.mid(FOOBAR, 3, 1));
177181
assertEquals(FOO, StringUtils.mid(FOOBAR, 0, 3));
@@ -180,10 +184,6 @@ public void testMid_String() {
180184
assertEquals(BAR, StringUtils.mid(FOOBAR, 3, 80));
181185
assertEquals("", StringUtils.mid(FOOBAR, 9, 3));
182186
assertEquals(FOO, StringUtils.mid(FOOBAR, -1, 3));
183-
try {
184-
StringUtils.mid(FOOBAR, 0, -1);
185-
fail();
186-
} catch (IllegalArgumentException ex) {}
187187
}
188188

189189
//-----------------------------------------------------------------------

0 commit comments

Comments
 (0)