Browse Source

[Locale] handle escaped (enquoted) strings when parsing

Igor Wiedler 14 years ago
parent
commit
fd0cc37c71

+ 20 - 5
src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php

@@ -79,11 +79,8 @@ class FullTransformer
     {
         $length = strlen($dateChars);
 
-        if ("'" === $dateChars[0]) {
-            if (preg_match("/^'+$/", $dateChars)) {
-                return str_replace("''", "'", $dateChars);
-            }
-            return str_replace("''", "'", substr($dateChars, 1, -1));
+        if ($this->isQuoteMatch($dateChars)) {
+            return $this->replaceQuoteMatch($dateChars);
         }
 
         if (isset($this->transformers[$dateChars[0]])) {
@@ -107,6 +104,11 @@ class FullTransformer
             $length = strlen($matches[0]);
             $transformerIndex = $matches[0][0];
 
+            $dateChars = $matches[0];
+            if ($that->isQuoteMatch($dateChars)) {
+                return $that->replaceQuoteMatch($dateChars);
+            }
+
             $transformers = $that->getTransformers();
 
             if (isset($transformers[$transformerIndex])) {
@@ -171,6 +173,19 @@ class FullTransformer
         return $ret;
     }
 
+    public function isQuoteMatch($quoteMatch)
+    {
+        return ("'" === $quoteMatch[0]);
+    }
+
+    public function replaceQuoteMatch($quoteMatch)
+    {
+        if (preg_match("/^'+$/", $quoteMatch)) {
+            return str_replace("''", "'", $quoteMatch);
+        }
+        return str_replace("''", "'", substr($quoteMatch, 1, -1));
+    }
+
     private function calculateUnixTimestamp($dateTime, array $options)
     {
         $datetime = $this->extractDateTime($options);

+ 7 - 0
tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php

@@ -533,6 +533,13 @@ class StubIntlDateFormatterTest extends LocaleTestCase
             // regExp metachars in the pattern string
             array('y[M-d', '1970[1-1', 0),
             array('y[M/d', '1970[1/1', 0),
+
+            // quote characters
+            array("'M'", 'M', 0),
+            array("'yy'", 'yy', 0),
+            array("'''yy'", "'yy", 0),
+            array("''y", "'1970", 0),
+            array("H 'o'' clock'", "0 o' clock", 0),
         );
     }