freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [10/15] incubator-freemarker git commit: Allowed " for quotation in extended DecimalFormat option assignments.
Date Wed, 16 Sep 2015 18:01:09 GMT
Allowed " for quotation in extended DecimalFormat option assignments.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/a071995a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/a071995a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/a071995a

Branch: refs/heads/master
Commit: a071995ab4f122fb8d20296147747621974f6d59
Parents: 205bcda
Author: ddekany <ddekany@apache.org>
Authored: Wed Sep 16 13:43:06 2015 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Wed Sep 16 13:43:06 2015 +0200

----------------------------------------------------------------------
 .../core/ExtendedDecimalFormatParser.java       | 29 ++++++++++----------
 .../core/ExtendedDecimalFormatTest.java         | 15 ++++++++++
 2 files changed, 30 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/a071995a/src/main/java/freemarker/core/ExtendedDecimalFormatParser.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/ExtendedDecimalFormatParser.java b/src/main/java/freemarker/core/ExtendedDecimalFormatParser.java
index 1a44ece..0a14126 100644
--- a/src/main/java/freemarker/core/ExtendedDecimalFormatParser.java
+++ b/src/main/java/freemarker/core/ExtendedDecimalFormatParser.java
@@ -318,46 +318,47 @@ class ExtendedDecimalFormatParser {
     private String fetchValue() throws ParseException {
         int ln = src.length();
         int startPos = pos;
-        boolean quotedMode = false;
+        char openedQuot = 0;
         boolean needsUnescaping = false;
         scanUntilEnd: while (pos < ln) {
             char c = src.charAt(pos);
-            if (c == '\'') {
-                if (!quotedMode) {
+            if (c == '\'' || c == '"') {
+                if (openedQuot == 0) {
                     if (startPos != pos) {
                         throw new java.text.ParseException(
-                                "The \"'\" character can only be used for quoting values,
"
+                                "The " + c + " character can only be used for quoting values,
"
                                         + "but it was in the middle of an non-quoted value.",
                                 pos);
                     }
-                    quotedMode = true;
-                } else {
-                    if (pos + 1 < ln && src.charAt(pos + 1) == '\'') {
-                        pos++; // skip "''" (escaped "'")
+                    openedQuot = c;
+                } else if (c == openedQuot) {
+                    if (pos + 1 < ln && src.charAt(pos + 1) == openedQuot) {
+                        pos++; // skip doubled quote (escaping)
                         needsUnescaping = true;
                     } else {
                         String str = src.substring(startPos + 1, pos);
                         pos++;
-                        return needsUnescaping ? unescape(str) : str;
+                        return needsUnescaping ? unescape(str, openedQuot) : str;
                     }
                 }
             } else {
-                if (!quotedMode && !Character.isJavaIdentifierPart(c)) {
+                if (openedQuot == 0 && !Character.isJavaIdentifierPart(c)) {
                     break scanUntilEnd;
                 }
             }
             pos++;
         } // while
-        if (quotedMode) {
+        if (openedQuot != 0) {
             throw new java.text.ParseException(
-                    "The \"'\" quotation wasn't closed when the end of the source was reached.",
+                    "The " + openedQuot 
+                    + " quotation wasn't closed when the end of the source was reached.",
                     pos);
         }
         return startPos == pos ? null : src.substring(startPos, pos);
     }
 
-    private String unescape(String s) {
-        return StringUtil.replace(s, "\'\'", "\'");
+    private String unescape(String s, char openedQuot) {
+        return openedQuot == '\'' ? StringUtil.replace(s, "\'\'", "\'") : StringUtil.replace(s,
"\"\"", "\"");
     }
 
     private String fetchStandardPattern() {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/a071995a/src/test/java/freemarker/core/ExtendedDecimalFormatTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/core/ExtendedDecimalFormatTest.java b/src/test/java/freemarker/core/ExtendedDecimalFormatTest.java
index 84ebf52..612b063 100644
--- a/src/test/java/freemarker/core/ExtendedDecimalFormatTest.java
+++ b/src/test/java/freemarker/core/ExtendedDecimalFormatTest.java
@@ -101,9 +101,17 @@ public class ExtendedDecimalFormatTest {
         
         assertFormatted("0.0;;inf=infinity", Double.POSITIVE_INFINITY, "infinity");
         assertFormatted("0.0;;inf='infinity'", Double.POSITIVE_INFINITY, "infinity");
+        assertFormatted("0.0;;inf=\"infinity\"", Double.POSITIVE_INFINITY, "infinity");
         assertFormatted("0.0;;inf=''", Double.POSITIVE_INFINITY, "");
+        assertFormatted("0.0;;inf=\"\"", Double.POSITIVE_INFINITY, "");
         assertFormatted("0.0;;inf='x''y'", Double.POSITIVE_INFINITY, "x'y");
+        assertFormatted("0.0;;inf=\"x'y\"", Double.POSITIVE_INFINITY, "x'y");
+        assertFormatted("0.0;;inf='x\"\"y'", Double.POSITIVE_INFINITY, "x\"\"y");
+        assertFormatted("0.0;;inf=\"x''y\"", Double.POSITIVE_INFINITY, "x''y");
         assertFormatted("0.0;;dec=''''", 1, "1'0");
+        assertFormatted("0.0;;dec=\"'\"", 1, "1'0");
+        assertFormatted("0.0;;dec='\"'", 1, "1\"0");
+        assertFormatted("0.0;;dec=\"\"\"\"", 1, "1\"0");
         
         try {
             ExtendedDecimalFormatParser.parse(";;dec=D,", LOC);
@@ -127,6 +135,13 @@ public class ExtendedDecimalFormatTest {
                     allOf(containsString("quotation"), containsString("closed")));
         }
         try {
+            ExtendedDecimalFormatParser.parse(";;dec=\"D", LOC);
+            fail();
+        } catch (java.text.ParseException e) {
+            assertThat(e.getMessage(),
+                    allOf(containsString("quotation"), containsString("closed")));
+        }
+        try {
             ExtendedDecimalFormatParser.parse(";;dec='D'grp=G", LOC);
             fail();
         } catch (java.text.ParseException e) {


Mime
View raw message