diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 4859882a2..01867e705 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@
+ * Only the specified portion of the buffer will be processed. + * The rest of the buffer is not processed, and is not returned. + * + * @param source the buffer to use as a template, not changed, null returns null + * @param offset the start offset within the array, must be valid + * @param length the length within the array to be processed, must be valid + * @return the result of the replace operation + * @since TODO + */ + public String replace(CharSequence source, int offset, int length) { + if (source == null) { + return null; + } + StrBuilder buf = new StrBuilder(length).append(source, offset, length); + substitute(buf, 0, length); + return buf.toString(); + } //----------------------------------------------------------------------- /** @@ -510,6 +549,49 @@ public boolean replaceIn(final StringBuffer source, final int offset, final int return true; } + //----------------------------------------------------------------------- + /** + * Replaces all the occurrences of variables within the given source buffer + * with their matching values from the resolver. + * The buffer is updated with the result. + * + * @param source the buffer to replace in, updated, null returns zero + * @return true if altered + * @since TODO + */ + public boolean replaceIn(StringBuilder source) { + if (source == null) { + return false; + } + return replaceIn(source, 0, source.length()); + } + + /** + * Replaces all the occurrences of variables within the given source builder + * with their matching values from the resolver. + * The builder is updated with the result. + *
+ * Only the specified portion of the buffer will be processed. + * The rest of the buffer is not processed, but it is not deleted. + * + * @param source the buffer to replace in, updated, null returns zero + * @param offset the start offset within the array, must be valid + * @param length the length within the buffer to be processed, must be valid + * @return true if altered + * @since TODO + */ + public boolean replaceIn(StringBuilder source, int offset, int length) { + if (source == null) { + return false; + } + StrBuilder buf = new StrBuilder(length).append(source, offset, length); + if (substitute(buf, 0, length) == false) { + return false; + } + source.replace(offset, offset + length, buf.toString()); + return true; + } + //----------------------------------------------------------------------- /** * Replaces all the occurrences of variables within the given source diff --git a/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java b/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java index 013cc83c6..476dbcc07 100644 --- a/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java +++ b/src/test/java/org/apache/commons/lang3/text/StrSubstitutorTest.java @@ -547,6 +547,13 @@ private void doTestReplace(final String expectedResult, final String replaceTemp assertEquals(expectedShortResult, sub.replace(buf, 1, buf.length() - 2)); } + // replace using StringBuilder + StringBuilder builder = new StringBuilder(replaceTemplate); + assertEquals(expectedResult, sub.replace(builder)); + if (substring) { + assertEquals(expectedShortResult, sub.replace(builder, 1, builder.length() - 2)); + } + // replace using StrBuilder StrBuilder bld = new StrBuilder(replaceTemplate); assertEquals(expectedResult, sub.replace(bld)); @@ -568,6 +575,16 @@ private void doTestReplace(final String expectedResult, final String replaceTemp assertEquals(expectedResult, buf.toString()); // expect full result as remainder is untouched } + // replace in StringBuilder + builder = new StringBuilder(replaceTemplate); + assertTrue(sub.replaceIn(builder)); + assertEquals(expectedResult, builder.toString()); + if (substring) { + builder = new StringBuilder(replaceTemplate); + assertTrue(sub.replaceIn(builder, 1, builder.length() - 2)); + assertEquals(expectedResult, builder.toString()); // expect full result as remainder is untouched + } + // replace in StrBuilder bld = new StrBuilder(replaceTemplate); assertTrue(sub.replaceIn(bld));