Skip to content

Commit

Permalink
Update Text.java
Browse files Browse the repository at this point in the history
Prior to this change, the text was internally stored by holding a char[].
This led to many calls to Arrays.copy() and reduced performance.
  • Loading branch information
bechte committed Feb 4, 2021
1 parent 1d44be3 commit 94c1b2f
Showing 1 changed file with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Text extends BaseTemplate {
/**
* The plain text. Required.
*/
private char[] text;
private StringBuilder text;

/** The escape's char or empty. */
private String escapeChar;
Expand All @@ -48,9 +48,7 @@ class Text extends BaseTemplate {
*/
Text(final Handlebars handlebars, final String text, final String escapeChar) {
super(handlebars);
int length = text.length();
this.text = new char[length];
text.getChars(0, length, this.text, 0);
this.text = new StringBuilder(text);
this.escapeChar = escapeChar;
}

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

@Override
public String text() {
return escapeChar + new String(text);
return escapeChar + text.toString();
}

/**
* @return Same as {@link #text()} without the escape char.
*/
public char[] textWithoutEscapeChar() {
return text;
return text.toString().toCharArray();
}

@Override
protected void merge(final Context scope, final Writer writer) throws IOException {
writer.write(text);
writer.write(text.toString());
}

/**
Expand All @@ -88,11 +86,7 @@ protected void merge(final Context scope, final Writer writer) throws IOExceptio
* @return This object.
*/
public Text append(final char[] text) {
int length = this.text.length + text.length;
char[] ntext = new char[length];
System.arraycopy(this.text, 0, ntext, 0, this.text.length);
System.arraycopy(text, 0, ntext, this.text.length, text.length);
this.text = ntext;
this.text.append(text);
return this;
}

Expand Down

0 comments on commit 94c1b2f

Please sign in to comment.