scolebourne 2003/08/01 16:01:53 Modified: lang/src/test/org/apache/commons/lang StringUtilsTest.java lang/src/java/org/apache/commons/lang StringUtils.java Log: Add overlay() as a replacement for overlayString() Revision Changes Path 1.43 +37 -4 jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java Index: StringUtilsTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java,v retrieving revision 1.42 retrieving revision 1.43 diff -u -r1.42 -r1.43 --- StringUtilsTest.java 1 Aug 2003 21:02:16 -0000 1.42 +++ StringUtilsTest.java 1 Aug 2003 23:01:52 -0000 1.43 @@ -490,15 +490,21 @@ assertSame("abcba", StringUtils.replaceChars("abcba", "z", "w")); } - public void testOverlayString() { + public void testOverlayString_StringStringIntInt() { assertEquals("overlayString(String, String, int, int) failed", "foo foor baz", StringUtils.overlayString(SENTENCE, FOO, 4, 6) ); - assertEquals(null, StringUtils.overlayString(null, null, 2, 4)); - assertEquals("abef", StringUtils.overlayString("abcdef", null, 2, 4)); assertEquals("abef", StringUtils.overlayString("abcdef", "", 2, 4)); assertEquals("abzzzzef", StringUtils.overlayString("abcdef", "zzzz", 2, 4)); assertEquals("abcdzzzzcdef", StringUtils.overlayString("abcdef", "zzzz", 4, 2)); try { + StringUtils.overlayString(null, "zzzz", 2, 4); + fail(); + } catch (NullPointerException ex) {} + try { + StringUtils.overlayString("abcdef", null, 2, 4); + fail(); + } catch (NullPointerException ex) {} + try { StringUtils.overlayString("abcdef", "zzzz", -1, 4); fail(); } catch (IndexOutOfBoundsException ex) {} @@ -506,6 +512,33 @@ StringUtils.overlayString("abcdef", "zzzz", 2, 8); fail(); } catch (IndexOutOfBoundsException ex) {} + } + + public void testOverlay_StringStringIntInt() { + assertEquals(null, StringUtils.overlay(null, null, 2, 4)); + assertEquals(null, StringUtils.overlay(null, null, -2, -4)); + + assertEquals("", StringUtils.overlay("", null, 0, 0)); + assertEquals("", StringUtils.overlay("", "", 0, 0)); + assertEquals("zzzz", StringUtils.overlay("", "zzzz", 0, 0)); + assertEquals("zzzz", StringUtils.overlay("", "zzzz", 2, 4)); + assertEquals("zzzz", StringUtils.overlay("", "zzzz", -2, -4)); + + assertEquals("abef", StringUtils.overlay("abcdef", null, 2, 4)); + assertEquals("abef", StringUtils.overlay("abcdef", null, 4, 2)); + assertEquals("abef", StringUtils.overlay("abcdef", "", 2, 4)); + assertEquals("abef", StringUtils.overlay("abcdef", "", 4, 2)); + assertEquals("abzzzzef", StringUtils.overlay("abcdef", "zzzz", 2, 4)); + assertEquals("abzzzzef", StringUtils.overlay("abcdef", "zzzz", 4, 2)); + + assertEquals("zzzzef", StringUtils.overlay("abcdef", "zzzz", -1, 4)); + assertEquals("zzzzef", StringUtils.overlay("abcdef", "zzzz", 4, -1)); + assertEquals("zzzzabcdef", StringUtils.overlay("abcdef", "zzzz", -2, -1)); + assertEquals("zzzzabcdef", StringUtils.overlay("abcdef", "zzzz", -1, -2)); + assertEquals("abcdzzzz", StringUtils.overlay("abcdef", "zzzz", 4, 10)); + assertEquals("abcdzzzz", StringUtils.overlay("abcdef", "zzzz", 10, 4)); + assertEquals("abcdefzzzz", StringUtils.overlay("abcdef", "zzzz", 8, 10)); + assertEquals("abcdefzzzz", StringUtils.overlay("abcdef", "zzzz", 10, 8)); } public void testRepeat_StringInt() { 1.87 +65 -7 jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java Index: StringUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v retrieving revision 1.86 retrieving revision 1.87 diff -u -r1.86 -r1.87 --- StringUtils.java 1 Aug 2003 22:05:43 -0000 1.86 +++ StringUtils.java 1 Aug 2003 23:01:52 -0000 1.87 @@ -204,7 +204,7 @@ * StringUtils.isEmpty(" bob ") = false * </pre> * - * <p>NOTE: This method changed in version 2.0. + * <p>NOTE: This method changed in Lang version 2.0. * It no longer trims the String. * That functionality is available in isBlank().</p> * @@ -2491,7 +2491,8 @@ * <p>Overlays part of a String with another String.</p> * * <pre> - * StringUtils.overlayString(null, *, *, *) = null + * StringUtils.overlayString(null, *, *, *) = NullPointerException + * StringUtils.overlayString(*, null, *, *) = NullPointerException * StringUtils.overlayString("", "abc", 0, 0) = "abc" * StringUtils.overlayString("abcdef", null, 2, 4) = "abef" * StringUtils.overlayString("abcdef", "", 2, 4) = "abef" @@ -2506,19 +2507,76 @@ * @param start the position to start overlaying at, must be valid * @param end the position to stop overlaying before, must be valid * @return overlayed String, <code>null</code> if null String input + * @throws NullPointerException if text or overlay is null * @throws IndexOutOfBoundsException if either position is invalid + * @deprecated Use better named [EMAIL PROTECTED] #overlay(String, String, int, int)} instead. + * Method will be removed in Commons Lang 3.0. */ public static String overlayString(String text, String overlay, int start, int end) { - if (text == null) { + return new StringBuffer(start + overlay.length() + text.length() - end + 1) + .append(text.substring(0, start)) + .append(overlay) + .append(text.substring(end)) + .toString(); + } + + /** + * <p>Overlays part of a String with another String.</p> + * + * <p>A <code>null</code> string input returns <code>null</code>. + * A negative index is treated as zero. + * An index greater than the string length is treated as the string length. + * The start index is always the smaller of the two indices.</p> + * + * <pre> + * StringUtils.overlay(null, *, *, *) = null + * StringUtils.overlay("", "abc", 0, 0) = "abc" + * StringUtils.overlay("abcdef", null, 2, 4) = "abef" + * StringUtils.overlay("abcdef", "", 2, 4) = "abef" + * StringUtils.overlay("abcdef", "", 4, 2) = "abef" + * StringUtils.overlay("abcdef", "zzzz", 2, 4) = "abzzzzef" + * StringUtils.overlay("abcdef", "zzzz", 4, 2) = "abzzzzef" + * StringUtils.overlay("abcdef", "zzzz", -1, 4) = "zzzzef" + * StringUtils.overlay("abcdef", "zzzz", 2, 8) = "abzzzz" + * StringUtils.overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef" + * StringUtils.overlay("abcdef", "zzzz", 8, 10) = "abcdefzzzz" + * </pre> + * + * @param str the String to do overlaying in, may be null + * @param overlay the String to overlay, may be null + * @param start the position to start overlaying at + * @param end the position to stop overlaying before + * @return overlayed String, <code>null</code> if null String input + */ + public static String overlay(String str, String overlay, int start, int end) { + if (str == null) { return null; } if (overlay == null) { overlay = ""; } - return new StringBuffer(start + overlay.length() + text.length() - end + 1) - .append(text.substring(0, start)) + int len = str.length(); + if (start < 0) { + start = 0; + } + if (start > len) { + start = len; + } + if (end < 0) { + end = 0; + } + if (end > len) { + end = len; + } + if (start > end) { + int temp = start; + start = end; + end = temp; + } + return new StringBuffer(len + start - end + overlay.length() + 1) + .append(str.substring(0, start)) .append(overlay) - .append(text.substring(end)) + .append(str.substring(end)) .toString(); }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]