diff --git a/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java b/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java index 098d024c..a5aadf2b 100644 --- a/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java +++ b/compiler/src/main/java/com/github/mustachejava/util/HtmlEscaper.java @@ -56,23 +56,24 @@ public class HtmlEscaper { public static void escape(String value, Writer writer) { try { - char[] chars = value.toCharArray(); - int length = chars.length; + int length = value.length(); int start = 0; for (int i = 0; i < length; i++) { - char c = chars[i]; + char c = value.charAt(i); char[] escaped; // We only possibly escape chars in the range 0-96 if (c <= 96 && (escaped = ESC[c]) != null) { // Write from the last replacement to before this one - if (i > start) writer.write(chars, start, i - start); + if (i > start) writer.write(value, start, i - start); // Write the replacement writer.write(escaped); // Move the pointer to the position after replacement start = i + 1; } } - writer.write(chars, start, length - start); + if (start < length) { + writer.write(value, start, length - start); + } } catch (IOException e) { throw new MustacheException("Failed to encode value: " + value, e); }