This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push: new b1900dd4 TextStringBuidler#hashCode() allocates a String on each call. b1900dd4 is described below commit b1900dd4dabe1e5d3807951ecdf2305d33556276 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Nov 17 14:56:08 2022 -0500 TextStringBuidler#hashCode() allocates a String on each call. See also PR #387 --- src/changes/changes.xml | 1 + src/main/java/org/apache/commons/text/TextStringBuilder.java | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 675ef28a..b257d144 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -48,6 +48,7 @@ The <action> type attribute can be add,update,fix,remove. <!-- FIX --> <action issue="TEXT-219" type="fix" dev="aherbert" due-to="Jaap Sperling">Fix StringTokenizer.getTokenList to return an independent modifiable list</action> <action type="fix" dev="aherbert" due-to="James Nord">Fix javadoc for StringEscapeUtils.escapeHtml4 #382</action> + <action type="fix" dev="ggregory" due-to="Pavel Belousov, Gary Gregory">TextStringBuidler#hashCode() allocates a String on each call #387.</action> <!-- ADD --> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Dependabot">Bump actions/cache from 3.0.8 to 3.0.10 #361, #365.</action> diff --git a/src/main/java/org/apache/commons/text/TextStringBuilder.java b/src/main/java/org/apache/commons/text/TextStringBuilder.java index 1c63f828..d391fc11 100644 --- a/src/main/java/org/apache/commons/text/TextStringBuilder.java +++ b/src/main/java/org/apache/commons/text/TextStringBuilder.java @@ -1964,7 +1964,13 @@ public class TextStringBuilder implements CharSequence, Appendable, Serializable */ @Override public int hashCode() { - return this.toString().hashCode(); + // no allocation + final char[] buf = buffer; + int result = 0; + for (int i = 0; i < size; i++) { + result = 31 * result + buf[i]; + } + return result; } /**