Pierre Post created TEXT-236:
--------------------------------
Summary: TextStringBuilder inserting at end throws
StringIndexOutOfBoundsException
Key: TEXT-236
URL: https://issues.apache.org/jira/browse/TEXT-236
Project: Commons Text
Issue Type: Bug
Affects Versions: 1.13.1, 1.13.0
Reporter: Pierre Post
The methods
{{TextStringBuilder#insert(...)}}
throw a {{StringIndexOutOfBoundsException}} when you try to insert something at
the end of the buffer. Nevertheless, the documentation of {{validateIndex()}}
still states "{_}Validates that an index is in the range 0 <= index *<=*
size.{_}"
I think this is really an unexpected behavior and also differs from the
behavior from {{java.lang.StringBuilder}} and
{{{}org.apache.commons.text.StrBuilder{}}}, as the following example shows:
{code:java}
StringBuilder stringBuilder = new StringBuilder();
// works as expected
stringBuilder.insert(0, "test");
StrBuilder strBuilder = new StrBuilder();
// works as expected
strBuilder.insert(0, "test");
TextStringBuilder textStringBuilder = new TextStringBuilder();
// throws java.lang.StringIndexOutOfBoundsException: String index out of range: 0
textStringBuilder.insert(0, "test");
{code}
However, care must be taken, as {{validateIndex()}} is also used by the various
char methods (e.g. {{{}setCharAt(){}}}) where the current validation IS
justified. Validation should be separated for these two use cases so that
insertion at the end of the buffer is again possible.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)