Add org.apache.commons.text.TextStringBuilder.wrap(char[]).
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 44c862d..1d3ffab 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -62,6 +62,7 @@
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.charAtDelete(int).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.TextStringBuilder(CharSequence).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.matcher.StringMatcherFactory.stringMatcher(char...).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.wrap(char[]).</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] junit-jupiter 5.5.1 -> 5.5.2.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] org.assertj:assertj-core 3.13.2 -> 3.16.1.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[build] com.puppycrawl.tools:checkstyle 8.23 -> 8.34.</action>
diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java
index 50674cb..4a5690e 100644
--- a/src/main/java/org/apache/commons/text/TextStringBuilder.java
+++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java
@@ -275,6 +275,19 @@
*/
private static final int TRUE_STRING_SIZE = "true".length();
+ /**
+ * Constructs an instance from a reference to a character array. Changes to the input chars are reflected in this
+ * instance until the internal buffer needs to be reallocated. Using a reference to an array allows the instance to
+ * be initialized without copying the whole input array.
+ *
+ * @param initialBuffer a reference to a character array.
+ * @return A new instance.
+ * @since 1.9
+ */
+ public static TextStringBuilder wrap(final char[] initialBuffer) {
+ return new TextStringBuilder(initialBuffer);
+ }
+
/** Internal data storage. */
private char[] buffer;
@@ -295,6 +308,16 @@
}
/**
+ * Constructs an instance from a reference to a character array.
+ *
+ * @param initialBuffer a reference to a character array.
+ */
+ private TextStringBuilder(final char[] initialBuffer) {
+ this.buffer = initialBuffer;
+ this.size = initialBuffer.length;
+ }
+
+ /**
* Constructs an instance from a character sequence, allocating 32 extra characters for growth.
*
* @param seq the string to copy, null treated as blank string
diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
index 1cd4106..995f916 100644
--- a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
+++ b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java
@@ -19,6 +19,7 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -2166,7 +2167,6 @@
assertThrows(IndexOutOfBoundsException.class, () -> sb.toString(15, 20));
}
- // -----------------------------------------------------------------------
@Test
public void testTrim() {
final TextStringBuilder sb = new TextStringBuilder();
@@ -2188,4 +2188,15 @@
assertEquals("a b c", sb.trim().toString());
}
+ @Test
+ public void testWrap() {
+ char[] test = "abc".toCharArray();
+ final TextStringBuilder sb = TextStringBuilder.wrap(test);
+ assertArrayEquals(test, sb.getBuffer());
+ assertEquals(test.length, sb.length());
+ assertEquals(test.length, sb.size());
+ sb.ensureCapacity(sb.capacity() * 2);
+ assertFalse(Arrays.equals(test, sb.getBuffer()));
+ }
+
}