Skip to content

Commit bbc313d

Browse files
bechtejknack
authored andcommitted
Update Text.java
Prior to this change, the text was internally stored by holding a char[]. This led to many calls to Arrays.copy() and reduced performance.
1 parent 45d3096 commit bbc313d

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

  • handlebars/src/main/java/com/github/jknack/handlebars/internal

handlebars/src/main/java/com/github/jknack/handlebars/internal/Text.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Text extends BaseTemplate {
3434
/**
3535
* The plain text. Required.
3636
*/
37-
private char[] text;
37+
private StringBuilder text;
3838

3939
/** The escape's char or empty. */
4040
private String escapeChar;
@@ -48,9 +48,7 @@ class Text extends BaseTemplate {
4848
*/
4949
Text(final Handlebars handlebars, final String text, final String escapeChar) {
5050
super(handlebars);
51-
int length = text.length();
52-
this.text = new char[length];
53-
text.getChars(0, length, this.text, 0);
51+
this.text = new StringBuilder(text);
5452
this.escapeChar = escapeChar;
5553
}
5654

@@ -66,19 +64,19 @@ class Text extends BaseTemplate {
6664

6765
@Override
6866
public String text() {
69-
return escapeChar + new String(text);
67+
return escapeChar + text.toString();
7068
}
7169

7270
/**
7371
* @return Same as {@link #text()} without the escape char.
7472
*/
7573
public char[] textWithoutEscapeChar() {
76-
return text;
74+
return text.toString().toCharArray();
7775
}
7876

7977
@Override
8078
protected void merge(final Context scope, final Writer writer) throws IOException {
81-
writer.write(text);
79+
writer.write(text.toString());
8280
}
8381

8482
/**
@@ -88,11 +86,7 @@ protected void merge(final Context scope, final Writer writer) throws IOExceptio
8886
* @return This object.
8987
*/
9088
public Text append(final char[] text) {
91-
int length = this.text.length + text.length;
92-
char[] ntext = new char[length];
93-
System.arraycopy(this.text, 0, ntext, 0, this.text.length);
94-
System.arraycopy(text, 0, ntext, this.text.length, text.length);
95-
this.text = ntext;
89+
this.text.append(text);
9690
return this;
9791
}
9892

0 commit comments

Comments
 (0)