dmitry Fri Oct 21 08:13:40 2005 EDT
Modified files: (Branch: PHP_5_1)
/php-src NEWS
/php-src/ext/standard file.c
/php-src/ext/standard/tests/file fputcsv.phpt
Log:
Fixed fgetcsv() and fputcsv() inconsistency.
http://cvs.php.net/diff.php/php-src/NEWS?r1=1.2027.2.149&r2=1.2027.2.150&ty=u
Index: php-src/NEWS
diff -u php-src/NEWS:1.2027.2.149 php-src/NEWS:1.2027.2.150
--- php-src/NEWS:1.2027.2.149 Fri Oct 21 05:32:37 2005
+++ php-src/NEWS Fri Oct 21 08:13:28 2005
@@ -1,6 +1,7 @@
PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? Oct 2005, PHP 5.1 Release Candidate 4
+- Fixed fgetcsv() and fputcsv() inconsistency. (Dmitry)
- Fixed bug #34905 (Digest authentication does not work with Apache 1). (Ilia)
- Fixed bug #34902 (mysqli::character_set_name() - undefined method). (Tony)
- Fixed bug #34893 (PHP5.1 overloading, Cannot access private property).
http://cvs.php.net/diff.php/php-src/ext/standard/file.c?r1=1.409&r2=1.409.2.1&ty=u
Index: php-src/ext/standard/file.c
diff -u php-src/ext/standard/file.c:1.409 php-src/ext/standard/file.c:1.409.2.1
--- php-src/ext/standard/file.c:1.409 Wed Aug 3 10:07:58 2005
+++ php-src/ext/standard/file.c Fri Oct 21 08:13:37 2005
@@ -21,7 +21,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: file.c,v 1.409 2005/08/03 14:07:58 sniper Exp $ */
+/* $Id: file.c,v 1.409.2.1 2005/10/21 12:13:37 dmitry Exp $ */
/* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
@@ -1849,7 +1849,7 @@
return ptr;
}
-#define FPUTCSV_FLD_CHK(c) php_memnstr(Z_STRVAL_PP(field), c, 1,
Z_STRVAL_PP(field) + Z_STRLEN_PP(field))
+#define FPUTCSV_FLD_CHK(c) memchr(Z_STRVAL_PP(field), c, Z_STRLEN_PP(field))
/* {{{ proto int fputcsv(resource fp, array fields [, string delimiter [,
string enclosure]])
Format line as CSV and write to file pointer */
@@ -1857,6 +1857,7 @@
{
char delimiter = ','; /* allow this to be set as parameter */
char enclosure = '"'; /* allow this to be set as parameter */
+ const char escape_char = '\\';
php_stream *stream;
int ret;
zval *fp = NULL, *fields = NULL, **field = NULL;
@@ -1864,7 +1865,6 @@
int delimiter_str_len, enclosure_str_len;
HashPosition pos;
int count, i = 0;
- char enc_double[3];
smart_str csvline = {0};
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ass",
@@ -1899,9 +1899,6 @@
PHP_STREAM_TO_ZVAL(stream, &fp);
- enc_double[0] = enclosure;
- enc_double[1] = enclosure;
- enc_double[2] = '\0';
count = zend_hash_num_elements(Z_ARRVAL_P(fields));
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(fields), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(fields), (void **)
&field, &pos) == SUCCESS) {
@@ -1910,18 +1907,30 @@
convert_to_string(*field);
}
/* enclose a field that contains a delimiter, an enclosure
character, or a newline */
- if (FPUTCSV_FLD_CHK(&delimiter) || FPUTCSV_FLD_CHK(&enclosure)
|| FPUTCSV_FLD_CHK("\n") ||
- FPUTCSV_FLD_CHK("\r") || FPUTCSV_FLD_CHK(" ") ||
FPUTCSV_FLD_CHK("\t")
- ) {
- zval enclosed_field;
- smart_str_appendl(&csvline, &enclosure, 1);
-
- php_char_to_str_ex(Z_STRVAL_PP(field),
Z_STRLEN_PP(field),
- enclosure, enc_double, 2,
&enclosed_field, 0, NULL);
- smart_str_appendl(&csvline, Z_STRVAL(enclosed_field),
Z_STRLEN(enclosed_field));
- zval_dtor(&enclosed_field);
-
- smart_str_appendl(&csvline, &enclosure, 1);
+ if (FPUTCSV_FLD_CHK(delimiter) ||
+ FPUTCSV_FLD_CHK(enclosure) ||
+ FPUTCSV_FLD_CHK(escape_char) ||
+ FPUTCSV_FLD_CHK('\n') ||
+ FPUTCSV_FLD_CHK('\r') ||
+ FPUTCSV_FLD_CHK('\t') ||
+ FPUTCSV_FLD_CHK(' ')) {
+ char *ch = Z_STRVAL_PP(field);
+ char *end = ch + Z_STRLEN_PP(field);
+ int escaped = 0;
+
+ smart_str_appendc(&csvline, enclosure);
+ while (ch < end) {
+ if (*ch == escape_char) {
+ escaped = 1;
+ } else if (!escaped && *ch == enclosure) {
+ smart_str_appendc(&csvline, enclosure);
+ } else {
+ escaped = 0;
+ }
+ smart_str_appendc(&csvline, *ch);
+ ch++;
+ }
+ smart_str_appendc(&csvline, enclosure);
} else {
smart_str_appendl(&csvline, Z_STRVAL_PP(field),
Z_STRLEN_PP(field));
}
http://cvs.php.net/diff.php/php-src/ext/standard/tests/file/fputcsv.phpt?r1=1.1&r2=1.1.2.1&ty=u
Index: php-src/ext/standard/tests/file/fputcsv.phpt
diff -u php-src/ext/standard/tests/file/fputcsv.phpt:1.1
php-src/ext/standard/tests/file/fputcsv.phpt:1.1.2.1
--- php-src/ext/standard/tests/file/fputcsv.phpt:1.1 Sun Sep 26 17:55:22 2004
+++ php-src/ext/standard/tests/file/fputcsv.phpt Fri Oct 21 08:13:39 2005
@@ -77,10 +77,10 @@
13 => 'aaa,"""bbb """',
14 => '"aaa""aaa""","""bbb""bbb"',
15 => '"aaa""aaa""""""",bbb',
- 16 => 'aaa,"""\\""bbb",ccc',
- 17 => '"aaa""\\""a""","""bbb"""',
- 18 => '"""\\""""","""aaa"""',
- 19 => '"""\\""""""",aaa',
+ 16 => 'aaa,"""\\"bbb",ccc',
+ 17 => '"aaa""\\"a""","""bbb"""',
+ 18 => '"""\\"""","""aaa"""',
+ 19 => '"""\\"""""",aaa',
);
$list = array (
0 => 'aaa,bbb',
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php