Skip to content

Commit b6fc20f

Browse files
[MOB-10804] Rework append for better memory efficiency
1 parent 3b6506f commit b6fc20f

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ public Template visitBody(final BodyContext ctx) {
697697
list.add(candidate);
698698
prev = candidate;
699699
} else {
700-
((Text) prev).append(((Text) candidate).textWithoutEscapeChar());
700+
((Text) prev).append(((Text) candidate));
701701
}
702702
} else {
703703
list.add(candidate);

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

+10-16
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class Text extends BaseTemplate {
3434
/**
3535
* The plain text. Required.
3636
*/
37-
private StringBuilder text;
37+
private final StringBuilder text;
3838

3939
/** The escape's char or empty. */
40-
private String escapeChar;
40+
private final String escapeChar;
4141

4242
/**
4343
* Creates a new {@link Text}.
@@ -67,27 +67,21 @@ public String text() {
6767
return escapeChar + text.toString();
6868
}
6969

70-
/**
71-
* @return Same as {@link #text()} without the escape char.
72-
*/
73-
public char[] textWithoutEscapeChar() {
74-
return text.toString().toCharArray();
75-
}
76-
7770
@Override
7871
protected void merge(final Context scope, final Writer writer) throws IOException {
7972
writer.write(text.toString());
8073
}
8174

8275
/**
83-
* Append text.
76+
* Merges the content of the given {@link Text} instance into this instance.
8477
*
85-
* @param text The text to append.
86-
* @return This object.
78+
* @param other the {@link Text} instance to merge with this instance;
79+
* if null or contains no text, no action is taken
8780
*/
88-
public Text append(final char[] text) {
89-
this.text.append(text);
90-
return this;
91-
}
81+
public void append(final Text other) {
82+
if (other != null && other.text != null) {
83+
this.text.append(other.text);
84+
}
85+
}
9286

9387
}

0 commit comments

Comments
 (0)