iliaa                                    Sun, 29 May 2011 10:23:06 +0000

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

Log:
Fixed bug #53848 (fgetcsv() ignores spaces at beginnings of fields).

Bug: http://bugs.php.net/53848 (Closed) fgetcsv ignores spaces at beginnings of 
fields
      
Changed paths:
    U   php/php-src/branches/PHP_5_3/NEWS
    U   php/php-src/branches/PHP_5_3/ext/standard/file.c
    A   php/php-src/branches/PHP_5_3/ext/standard/tests/file/bug53848.phpt
    U   php/php-src/branches/PHP_5_4/ext/standard/file.c
    A   php/php-src/branches/PHP_5_4/ext/standard/tests/file/bug53848.phpt
    U   php/php-src/trunk/ext/standard/file.c
    A   php/php-src/trunk/ext/standard/tests/file/bug53848.phpt

Modified: php/php-src/branches/PHP_5_3/NEWS
===================================================================
--- php/php-src/branches/PHP_5_3/NEWS   2011-05-29 09:23:08 UTC (rev 311542)
+++ php/php-src/branches/PHP_5_3/NEWS   2011-05-29 10:23:06 UTC (rev 311543)
@@ -44,6 +44,7 @@
   . Fixed bug #54866 (incorrect accounting for realpath_cache_size) (Dustin 
Ward)
   . Fixed bug #54721 (Different Hashes on Windows, BSD and Linux on wrong Salt 
size)
     (Pierre, os at irj dot ru)
+  . Fixed bug #53848 (fgetcsv() ignores spaces at beginnings of fields). (Ilia)
   . Fixed bug #50363 (Invalid parsing in convert.quoted-printable-decode 
filter).
     (slusarz at curecanti dot org)
   . Fixed bug #48465 (sys_get_temp_dir() possibly inconsistent when using

Modified: php/php-src/branches/PHP_5_3/ext/standard/file.c
===================================================================
--- php/php-src/branches/PHP_5_3/ext/standard/file.c    2011-05-29 09:23:08 UTC 
(rev 311542)
+++ php/php-src/branches/PHP_5_3/ext/standard/file.c    2011-05-29 10:23:06 UTC 
(rev 311543)
@@ -2196,30 +2196,17 @@
                char *comp_end, *hunk_begin;

                tptr = temp;
-
-               /* 1. Strip any leading space */
-               for (;;) {
-                       inc_len = (bptr < limit ? (*bptr == '\0' ? 1: 
php_mblen(bptr, limit - bptr)): 0);
-                       switch (inc_len) {
-                               case -2:
-                               case -1:
-                                       inc_len = 1;
-                                       php_mblen(NULL, 0);
-                                       break;
-                               case 0:
-                                       goto quit_loop_1;
-                               case 1:
-                                       if (!isspace((int)*(unsigned char 
*)bptr) || *bptr == delimiter) {
-                                               goto quit_loop_1;
-                                       }
-                                       break;
-                               default:
-                                       goto quit_loop_1;
+               inc_len = (bptr < limit ? (*bptr == '\0' ? 1: php_mblen(bptr, 
limit - bptr)): 0);
+               if (inc_len == 1) {
+                       char *tmp = bptr;
+                       while (isspace((int)*(unsigned char *)tmp)) {
+                               tmp++;
                        }
-                       bptr += inc_len;
+                       if (*tmp == enclosure) {
+                               bptr = tmp;
+                       }
                }

-       quit_loop_1:
                if (first_field && bptr == line_end) {
                        add_next_index_null(return_value);
                        break;

Added: php/php-src/branches/PHP_5_3/ext/standard/tests/file/bug53848.phpt
===================================================================
--- php/php-src/branches/PHP_5_3/ext/standard/tests/file/bug53848.phpt          
                (rev 0)
+++ php/php-src/branches/PHP_5_3/ext/standard/tests/file/bug53848.phpt  
2011-05-29 10:23:06 UTC (rev 311543)
@@ -0,0 +1,25 @@
+--TEST--
+Bug #53848 (fgetcsv removes leading spaces from fields)
+--FILE--
+<?php
+$file = dirname(__FILE__) . "/bug39538.csv";
+@unlink($file);
+file_put_contents($file, "a,b\n  c,  d");
+$fp = fopen($file, "r");
+while ($l = fgetcsv($fp)) var_dump($l);
+fclose($fp);
+@unlink($file);
+?>
+--EXPECT--
+array(2) {
+  [0]=>
+  string(1) "a"
+  [1]=>
+  string(1) "b"
+}
+array(2) {
+  [0]=>
+  string(3) "  c"
+  [1]=>
+  string(3) "  d"
+}

Modified: php/php-src/branches/PHP_5_4/ext/standard/file.c
===================================================================
--- php/php-src/branches/PHP_5_4/ext/standard/file.c    2011-05-29 09:23:08 UTC 
(rev 311542)
+++ php/php-src/branches/PHP_5_4/ext/standard/file.c    2011-05-29 10:23:06 UTC 
(rev 311543)
@@ -2098,29 +2098,17 @@

                tptr = temp;

-               /* 1. Strip any leading space */
-               for (;;) {
-                       inc_len = (bptr < limit ? (*bptr == '\0' ? 1: 
php_mblen(bptr, limit - bptr)): 0);
-                       switch (inc_len) {
-                               case -2:
-                               case -1:
-                                       inc_len = 1;
-                                       php_ignore_value(php_mblen(NULL, 0));
-                                       break;
-                               case 0:
-                                       goto quit_loop_1;
-                               case 1:
-                                       if (!isspace((int)*(unsigned char 
*)bptr) || *bptr == delimiter) {
-                                               goto quit_loop_1;
-                                       }
-                                       break;
-                               default:
-                                       goto quit_loop_1;
+               inc_len = (bptr < limit ? (*bptr == '\0' ? 1: php_mblen(bptr, 
limit - bptr)): 0);
+               if (inc_len == 1) {
+                       char *tmp = bptr;
+                       while (isspace((int)*(unsigned char *)tmp)) {
+                               tmp++;
                        }
-                       bptr += inc_len;
+                       if (*tmp == enclosure) {
+                               bptr = tmp;
+                       }
                }

-       quit_loop_1:
                if (first_field && bptr == line_end) {
                        add_next_index_null(return_value);
                        break;

Added: php/php-src/branches/PHP_5_4/ext/standard/tests/file/bug53848.phpt
===================================================================
--- php/php-src/branches/PHP_5_4/ext/standard/tests/file/bug53848.phpt          
                (rev 0)
+++ php/php-src/branches/PHP_5_4/ext/standard/tests/file/bug53848.phpt  
2011-05-29 10:23:06 UTC (rev 311543)
@@ -0,0 +1,25 @@
+--TEST--
+Bug #53848 (fgetcsv removes leading spaces from fields)
+--FILE--
+<?php
+$file = dirname(__FILE__) . "/bug39538.csv";
+@unlink($file);
+file_put_contents($file, "a,b\n  c,  d");
+$fp = fopen($file, "r");
+while ($l = fgetcsv($fp)) var_dump($l);
+fclose($fp);
+@unlink($file);
+?>
+--EXPECT--
+array(2) {
+  [0]=>
+  string(1) "a"
+  [1]=>
+  string(1) "b"
+}
+array(2) {
+  [0]=>
+  string(3) "  c"
+  [1]=>
+  string(3) "  d"
+}

Modified: php/php-src/trunk/ext/standard/file.c
===================================================================
--- php/php-src/trunk/ext/standard/file.c       2011-05-29 09:23:08 UTC (rev 
311542)
+++ php/php-src/trunk/ext/standard/file.c       2011-05-29 10:23:06 UTC (rev 
311543)
@@ -2098,29 +2098,17 @@

                tptr = temp;

-               /* 1. Strip any leading space */
-               for (;;) {
-                       inc_len = (bptr < limit ? (*bptr == '\0' ? 1: 
php_mblen(bptr, limit - bptr)): 0);
-                       switch (inc_len) {
-                               case -2:
-                               case -1:
-                                       inc_len = 1;
-                                       php_ignore_value(php_mblen(NULL, 0));
-                                       break;
-                               case 0:
-                                       goto quit_loop_1;
-                               case 1:
-                                       if (!isspace((int)*(unsigned char 
*)bptr) || *bptr == delimiter) {
-                                               goto quit_loop_1;
-                                       }
-                                       break;
-                               default:
-                                       goto quit_loop_1;
+               inc_len = (bptr < limit ? (*bptr == '\0' ? 1: php_mblen(bptr, 
limit - bptr)): 0);
+               if (inc_len == 1) {
+                       char *tmp = bptr;
+                       while (isspace((int)*(unsigned char *)tmp)) {
+                               tmp++;
+                       }
+                       if (*tmp == enclosure) {
+                               bptr = tmp;
                        }
-                       bptr += inc_len;
-               }
+               }

-       quit_loop_1:
                if (first_field && bptr == line_end) {
                        add_next_index_null(return_value);
                        break;

Added: php/php-src/trunk/ext/standard/tests/file/bug53848.phpt
===================================================================
--- php/php-src/trunk/ext/standard/tests/file/bug53848.phpt                     
        (rev 0)
+++ php/php-src/trunk/ext/standard/tests/file/bug53848.phpt     2011-05-29 
10:23:06 UTC (rev 311543)
@@ -0,0 +1,25 @@
+--TEST--
+Bug #53848 (fgetcsv removes leading spaces from fields)
+--FILE--
+<?php
+$file = dirname(__FILE__) . "/bug39538.csv";
+@unlink($file);
+file_put_contents($file, "a,b\n  c,  d");
+$fp = fopen($file, "r");
+while ($l = fgetcsv($fp)) var_dump($l);
+fclose($fp);
+@unlink($file);
+?>
+--EXPECT--
+array(2) {
+  [0]=>
+  string(1) "a"
+  [1]=>
+  string(1) "b"
+}
+array(2) {
+  [0]=>
+  string(3) "  c"
+  [1]=>
+  string(3) "  d"
+}

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

Reply via email to