Skip to content

Commit 978ec5e

Browse files
MiguelAMarcelinojknack
authored andcommitted
Rework append for better memory efficiency
1 parent 7d49fbb commit 978ec5e

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ public Template visitBody(final BodyContext ctx) {
767767
list.add(candidate);
768768
prev = candidate;
769769
} else {
770-
((Text) prev).append(((Text) candidate).textWithoutEscapeChar());
770+
((Text) prev).append(((Text) candidate));
771771
}
772772
} else {
773773
list.add(candidate);

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
class Text extends BaseTemplate {
2121

2222
/** The plain text. Required. */
23-
private StringBuilder text;
23+
private final StringBuilder text;
2424

2525
/** The escape's char or empty. */
26-
private String escapeChar;
26+
private final String escapeChar;
2727

2828
/**
2929
* Creates a new {@link Text}.
@@ -53,26 +53,20 @@ public String text() {
5353
return escapeChar + text.toString();
5454
}
5555

56-
/**
57-
* @return Same as {@link #text()} without the escape char.
58-
*/
59-
public char[] textWithoutEscapeChar() {
60-
return text.toString().toCharArray();
61-
}
62-
6356
@Override
6457
protected void merge(final Context scope, final Writer writer) throws IOException {
6558
writer.write(text.toString());
6659
}
6760

6861
/**
69-
* Append text.
62+
* Merges the content of the given {@link Text} instance into this instance.
7063
*
71-
* @param text The text to append.
72-
* @return This object.
64+
* @param other the {@link Text} instance to merge with this instance;
65+
* if null or contains no text, no action is taken
7366
*/
74-
public Text append(final char[] text) {
75-
this.text.append(text);
76-
return this;
67+
public void append(final Text other) {
68+
if (other != null && other.text != null) {
69+
this.text.append(other.text);
70+
}
7771
}
7872
}

0 commit comments

Comments
 (0)