derick                                   Sun, 12 Dec 2010 17:17:16 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=306273

Log:
- Added the + modifier to parseFromFormat to allow trailing text in the
  string to parse without throwing an error.
#- Patch by Stas, test case by me.

Changed paths:
    U   php/php-src/trunk/NEWS
    U   php/php-src/trunk/ext/date/lib/parse_date.c
    U   php/php-src/trunk/ext/date/lib/parse_date.re
    A   php/php-src/trunk/ext/date/tests/bug51866.phpt

Modified: php/php-src/trunk/NEWS
===================================================================
--- php/php-src/trunk/NEWS      2010-12-12 17:12:29 UTC (rev 306272)
+++ php/php-src/trunk/NEWS      2010-12-12 17:17:16 UTC (rev 306273)
@@ -121,6 +121,10 @@
   . Added support for CURLOPT_MAX_RECV_SPEED_LARGE and
     CURLOPT_MAX_SEND_SPEED_LARGE. FR #51815. (Pierrick)

+- Improved Date extension:
+  . Added the + modifier to parseFromFormat to allow trailing text in the
+    string to parse without throwing an error. (Stas, Derick)
+
 - Improved DBA extension:
   . Added Tokyo Cabinet abstract DB support. (Michael Maclean)
   . Added Berkeley DB 5 support. (Johannes, Chris Jones)

Modified: php/php-src/trunk/ext/date/lib/parse_date.c
===================================================================
--- php/php-src/trunk/ext/date/lib/parse_date.c 2010-12-12 17:12:29 UTC (rev 
306272)
+++ php/php-src/trunk/ext/date/lib/parse_date.c 2010-12-12 17:17:16 UTC (rev 
306273)
@@ -1,4 +1,4 @@
-/* Generated by re2c 0.13.5 on Sat Nov 13 14:56:03 2010 */
+/* Generated by re2c 0.13.5 on Sun Dec 12 16:09:49 2010 */
 #line 1 "ext/date/lib/parse_date.re"
 /*
    +----------------------------------------------------------------------+
@@ -24824,6 +24824,7 @@
        timelib_sll tmp;
        Scanner in;
        Scanner *s = ∈
+       int allow_extra = 0;

        memset(&in, 0, sizeof(in));
        in.errors = malloc(sizeof(struct timelib_error_container));
@@ -25049,6 +25050,10 @@
                                timelib_eat_until_separator((char **) &ptr);
                                break;

+                       case '+': /* allow extra chars in the format */
+                               allow_extra = 1;
+                               break;
+
                        default:
                                if (*fptr != *ptr) {
                                        add_pbf_error(s, "The format separator 
does not match", string, begin);
@@ -25058,8 +25063,16 @@
                fptr++;
        }
        if (*ptr) {
-               add_pbf_error(s, "Trailing data", string, ptr);
+               if (allow_extra) {
+                       add_pbf_warning(s, "Trailing data", string, ptr);
+               } else {
+                       add_pbf_error(s, "Trailing data", string, ptr);
+               }
        }
+       /* ignore trailing +'s */
+       while (*fptr == '+') {
+               fptr++;
+       }
        if (*fptr) {
                add_pbf_error(s, "Data missing", string, ptr);
        }

Modified: php/php-src/trunk/ext/date/lib/parse_date.re
===================================================================
--- php/php-src/trunk/ext/date/lib/parse_date.re        2010-12-12 17:12:29 UTC 
(rev 306272)
+++ php/php-src/trunk/ext/date/lib/parse_date.re        2010-12-12 17:17:16 UTC 
(rev 306273)
@@ -1831,6 +1831,7 @@
        timelib_sll tmp;
        Scanner in;
        Scanner *s = ∈
+       int allow_extra = 0;

        memset(&in, 0, sizeof(in));
        in.errors = malloc(sizeof(struct timelib_error_container));
@@ -2056,6 +2057,10 @@
                                timelib_eat_until_separator((char **) &ptr);
                                break;

+                       case '+': /* allow extra chars in the format */
+                               allow_extra = 1;
+                               break;
+
                        default:
                                if (*fptr != *ptr) {
                                        add_pbf_error(s, "The format separator 
does not match", string, begin);
@@ -2065,8 +2070,16 @@
                fptr++;
        }
        if (*ptr) {
-               add_pbf_error(s, "Trailing data", string, ptr);
+               if (allow_extra) {
+                       add_pbf_warning(s, "Trailing data", string, ptr);
+               } else {
+                       add_pbf_error(s, "Trailing data", string, ptr);
+               }
        }
+       /* ignore trailing +'s */
+       while (*fptr == '+') {
+               fptr++;
+       }
        if (*fptr) {
                add_pbf_error(s, "Data missing", string, ptr);
        }

Added: php/php-src/trunk/ext/date/tests/bug51866.phpt
===================================================================
--- php/php-src/trunk/ext/date/tests/bug51866.phpt                              
(rev 0)
+++ php/php-src/trunk/ext/date/tests/bug51866.phpt      2010-12-12 17:17:16 UTC 
(rev 306273)
@@ -0,0 +1,159 @@
+--TEST--
+Bug #51866 (Lenient parsing with parseFromFormat)
+--FILE--
+<?php
+$tests = array(
+       array( 'Y-m-d',   '2001-11-29 13:20:01' ),
+       array( 'Y-m-d+',  '2001-11-29 13:20:01' ),
+       array( 'Y-m-d +', '2001-11-29 13:20:01' ),
+       array( 'Y-m-d+',  '2001-11-29' ),
+       array( 'Y-m-d +', '2001-11-29' ),
+       array( 'Y-m-d +', '2001-11-29 ' ),
+);
+foreach( $tests as $test )
+{
+       list($format, $str) = $test;
+       var_dump($format, $str);
+       $d = DateTime::createFromFormat($format, $str);
+       var_dump($d);
+       var_dump(DateTime::getLastErrors());
+
+       echo "\n\n";
+}
+--EXPECTF--
+string(5) "Y-m-d"
+string(19) "2001-11-29 13:20:01"
+bool(false)
+array(4) {
+  ["warning_count"]=>
+  int(0)
+  ["warnings"]=>
+  array(0) {
+  }
+  ["error_count"]=>
+  int(1)
+  ["errors"]=>
+  array(1) {
+    [10]=>
+    string(13) "Trailing data"
+  }
+}
+
+
+string(6) "Y-m-d+"
+string(19) "2001-11-29 13:20:01"
+object(DateTime)#2 (3) {
+  ["date"]=>
+  string(19) "2001-11-29 %d:%d:%d"
+  ["timezone_type"]=>
+  int(3)
+  ["timezone"]=>
+  string(13) "Europe/London"
+}
+array(4) {
+  ["warning_count"]=>
+  int(1)
+  ["warnings"]=>
+  array(1) {
+    [10]=>
+    string(13) "Trailing data"
+  }
+  ["error_count"]=>
+  int(0)
+  ["errors"]=>
+  array(0) {
+  }
+}
+
+
+string(7) "Y-m-d +"
+string(19) "2001-11-29 13:20:01"
+object(DateTime)#3 (3) {
+  ["date"]=>
+  string(19) "2001-11-29 %d:%d:%d"
+  ["timezone_type"]=>
+  int(3)
+  ["timezone"]=>
+  string(13) "Europe/London"
+}
+array(4) {
+  ["warning_count"]=>
+  int(1)
+  ["warnings"]=>
+  array(1) {
+    [11]=>
+    string(13) "Trailing data"
+  }
+  ["error_count"]=>
+  int(0)
+  ["errors"]=>
+  array(0) {
+  }
+}
+
+
+string(6) "Y-m-d+"
+string(10) "2001-11-29"
+object(DateTime)#2 (3) {
+  ["date"]=>
+  string(19) "2001-11-29 %d:%d:%d"
+  ["timezone_type"]=>
+  int(3)
+  ["timezone"]=>
+  string(13) "Europe/London"
+}
+array(4) {
+  ["warning_count"]=>
+  int(0)
+  ["warnings"]=>
+  array(0) {
+  }
+  ["error_count"]=>
+  int(0)
+  ["errors"]=>
+  array(0) {
+  }
+}
+
+
+string(7) "Y-m-d +"
+string(10) "2001-11-29"
+bool(false)
+array(4) {
+  ["warning_count"]=>
+  int(0)
+  ["warnings"]=>
+  array(0) {
+  }
+  ["error_count"]=>
+  int(1)
+  ["errors"]=>
+  array(1) {
+    [10]=>
+    string(12) "Data missing"
+  }
+}
+
+
+string(7) "Y-m-d +"
+string(11) "2001-11-29 "
+object(DateTime)#2 (3) {
+  ["date"]=>
+  string(19) "2001-11-29 %d:%d:%d"
+  ["timezone_type"]=>
+  int(3)
+  ["timezone"]=>
+  string(13) "Europe/London"
+}
+array(4) {
+  ["warning_count"]=>
+  int(0)
+  ["warnings"]=>
+  array(0) {
+  }
+  ["error_count"]=>
+  int(0)
+  ["errors"]=>
+  array(0) {
+  }
+}

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to