Skip to content

Commit 25718ef

Browse files
fix inconsistent behavior
1 parent 0e05083 commit 25718ef

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

ext/standard/file.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,27 @@ PHP_FUNCTION(file)
658658
p++;
659659
parse_eol:
660660
if (skip_blank_lines) {
661-
int windows_eol = 0;
662-
if (p != ZSTR_VAL(target_buf) && eol_marker == '\n' && *(p - 1) == '\r') {
663-
windows_eol++;
661+
size_t eol_len = 1;
662+
663+
if (eol_marker == '\n') {
664+
if (p >= ZSTR_VAL(target_buf) + 2 && *(p - 2) == '\r' && *(p - 1) == '\n') {
665+
eol_len = 2;
666+
} else if (p == e && p > s) {
667+
const char *check = p - 1;
668+
bool all_cr = true;
669+
while (check >= s && *check == '\r') {
670+
check--;
671+
}
672+
if (check < s) {
673+
s = p;
674+
continue;
675+
}
676+
eol_len = 1;
677+
}
664678
}
665-
if (p-s-windows_eol == 1) {
679+
680+
size_t line_len = (size_t)(p - s);
681+
if (line_len == eol_len) {
666682
s = p;
667683
continue;
668684
}

ext/standard/tests/file/file_skip_empty_lines.phpt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ var_dump($lines);
2828
$lines = file($test_file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
2929
var_dump($lines);
3030

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);
46+
3147
unlink($test_file);
3248
?>
3349
--EXPECT--
@@ -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)