Skip to content

Commit f0e3236

Browse files
fix inconsistent behavior
1 parent 0f62ad9 commit f0e3236

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ PHP NEWS
4646
- Standard:
4747
. Fixed bug GH-19926 (reset internal pointer earlier while splicing array
4848
while COW violation flag is still set). (alexandre-daubois)
49+
. Fixed bug GH-18120 (Honor FILE_SKIP_EMPTY_LINES even when
50+
FILE_IGNORE_NEW_LINES is not set). (alexandre-daubois)
4951

5052
- Streams:
5153
. Added so_reuseaddr streams context socket option that allows disabling

ext/standard/file.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,26 @@ PHP_FUNCTION(file)
641641
p++;
642642
parse_eol:
643643
if (skip_blank_lines) {
644-
int windows_eol = 0;
645-
if (p != ZSTR_VAL(target_buf) && eol_marker == '\n' && *(p - 1) == '\r') {
646-
windows_eol++;
644+
size_t eol_len = 1;
645+
646+
if (eol_marker == '\n') {
647+
if (p - ZSTR_VAL(target_buf) >= 2 && *(p - 2) == '\r' && *(p - 1) == '\n') {
648+
eol_len = 2;
649+
} else if (p == e && p > s) {
650+
const char *check = p - 1;
651+
while (check > s && *check == '\r') {
652+
check--;
653+
}
654+
if (check == s && *check == '\r') {
655+
s = p;
656+
continue;
657+
}
658+
eol_len = 1;
659+
}
647660
}
648-
if (p-s-windows_eol == 1) {
661+
662+
size_t line_len = p - s;
663+
if (line_len == eol_len) {
649664
s = p;
650665
continue;
651666
}

ext/standard/tests/file/file_skip_empty_lines.phpt

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test file() function with FILE_SKIP_EMPTY_LINES flag
2+
GH-18120 (Honor FILE_SKIP_EMPTY_LINES even when FILE_IGNORE_NEW_LINES is not set)
33
--FILE--
44
<?php
55
$test_file = __DIR__ . "/file_skip_empty_lines.tmp";
@@ -28,8 +28,24 @@ var_dump($lines);
2828
$lines = file($test_file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
2929
var_dump($lines);
3030

31-
unlink($test_file);
31+
file_put_contents($test_file, "\r");
32+
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
33+
var_dump($lines);
34+
35+
file_put_contents($test_file, "\r\r");
36+
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
37+
var_dump($lines);
38+
39+
file_put_contents($test_file, "\n");
40+
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
41+
var_dump($lines);
42+
43+
file_put_contents($test_file, "\r\n");
44+
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
45+
var_dump($lines);
3246
?>
47+
--CLEAN--
48+
<?php unlink(__DIR__ . "/file_skip_empty_lines.tmp"); ?>
3349
--EXPECT--
3450
array(3) {
3551
[0]=>
@@ -84,14 +100,11 @@ array(6) {
84100
string(6) "Third
85101
"
86102
}
87-
array(3) {
103+
array(2) {
88104
[0]=>
89105
string(7) "First
90106
"
91107
[1]=>
92-
string(2) "
93-
"
94-
[2]=>
95108
string(8) "Second
96109
"
97110
}
@@ -101,3 +114,11 @@ array(2) {
101114
[1]=>
102115
string(6) "Second"
103116
}
117+
array(0) {
118+
}
119+
array(0) {
120+
}
121+
array(0) {
122+
}
123+
array(0) {
124+
}

0 commit comments

Comments
 (0)