From 426faf92c5aba99773a24b70e01d1f504e6235d7 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 3 May 2013 10:12:00 -0700 Subject: [PATCH] Do our save/restore slightly differently. --- .../com/samskivert/mustache/Mustache.java | 46 +++++++------------ .../com/samskivert/mustache/MustacheTest.java | 2 +- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index 0fa57c8..d8715b4 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -218,15 +218,13 @@ public class Mustache * Compiles the supplied template into a repeatedly executable intermediate form. */ protected static Template compile (Reader source, Compiler compiler) { - String originalDelims = compiler.delims == null ? null : compiler.delims.toString(); - //compiler.delims.start1 + Delims orig = compiler.delims.save(); try { Accumulator accum = new Parser(compiler).parse(source); return new Template(accum.finish(), compiler); } finally { - if (originalDelims != null) { - compiler.delims.updateDelims(originalDelims); - } + // restore the original delimiters + compiler.delims.restore(orig); } } @@ -421,34 +419,12 @@ public class Mustache } protected static class Delims { - public char start1 = '{'; - public char start2 = '{'; - public char end1 = '}'; - public char end2 = '}'; + public char start1 = '{', end1 = '}'; + public char start2 = '{', end2 = '}'; public boolean isStaches () { return start1 == '{' && start2 == '{' && end1 == '}' && end2 == '}'; } - - /** - * Returns a String representation that is compatible with - * {@link #updateDelims(java.lang.String)}. - * @return - */ - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append(start1); - if (start2 != NO_CHAR) { - builder.append(start2); - } - builder.append(" "); - builder.append(end1); - if (end2 != NO_CHAR) { - builder.append(end2); - } - return builder.toString(); - } public Delims updateDelims (String dtext) { String[] delims = dtext.split(" "); @@ -482,6 +458,18 @@ public class Mustache return this; } + Delims save () { + return new Delims().restore(this); + } + + Delims restore (Delims delims) { + this.start1 = delims.start1; + this.start2 = delims.start2; + this.end1 = delims.end1; + this.end2 = delims.end2; + return this; + } + private static String errmsg (String dtext) { return "Invalid delimiter configuration '" + dtext + "'. Must be of the " + "form {{=1 2=}} or {{=12 34=}} where 1, 2, 3 and 4 are delimiter chars."; diff --git a/src/test/java/com/samskivert/mustache/MustacheTest.java b/src/test/java/com/samskivert/mustache/MustacheTest.java index 13b2bb2..4908684 100644 --- a/src/test/java/com/samskivert/mustache/MustacheTest.java +++ b/src/test/java/com/samskivert/mustache/MustacheTest.java @@ -488,7 +488,7 @@ public class MustacheTest String nullvar = null; }); } - + @Test public void testCompilingDoesntChangeCompilersDelimiters() { Mustache.Compiler compiler = Mustache.compiler(); test(compiler,