Skip to content

Commit d8ff4fd

Browse files
fix inconsistent behavior
1 parent 0f62ad9 commit d8ff4fd

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
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: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,27 @@ 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+
bool all_cr = true;
652+
while (check >= s && *check == '\r') {
653+
check--;
654+
}
655+
if (check < s) {
656+
s = p;
657+
continue;
658+
}
659+
eol_len = 1;
660+
}
647661
}
648-
if (p-s-windows_eol == 1) {
662+
663+
size_t line_len = (size_t)(p - s);
664+
if (line_len == eol_len) {
649665
s = p;
650666
continue;
651667
}

ext/standard/tests/file/file_skip_empty_lines.phpt

Lines changed: 26 additions & 5 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,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)