Skip to content

Commit

Permalink
[MOB-10804] Rework append for better memory efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelAMarcelino committed Feb 4, 2025
1 parent 3b6506f commit b6fc20f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ public Template visitBody(final BodyContext ctx) {
list.add(candidate);
prev = candidate;
} else {
((Text) prev).append(((Text) candidate).textWithoutEscapeChar());
((Text) prev).append(((Text) candidate));
}
} else {
list.add(candidate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class Text extends BaseTemplate {
/**
* The plain text. Required.
*/
private StringBuilder text;
private final StringBuilder text;

/** The escape's char or empty. */
private String escapeChar;
private final String escapeChar;

/**
* Creates a new {@link Text}.
Expand Down Expand Up @@ -67,27 +67,21 @@ public String text() {
return escapeChar + text.toString();
}

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

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

/**
* Append text.
* Merges the content of the given {@link Text} instance into this instance.
*
* @param text The text to append.
* @return This object.
* @param other the {@link Text} instance to merge with this instance;
* if null or contains no text, no action is taken
*/
public Text append(final char[] text) {
this.text.append(text);
return this;
}
public void append(final Text other) {
if (other != null && other.text != null) {
this.text.append(other.text);
}
}

}

0 comments on commit b6fc20f

Please sign in to comment.