Skip to content

Commit 0294ac1

Browse files
Fix GH-18120: Honor FILE_SKIP_EMPTY_LINES even when FILE_IGNORE_NEW_LINES is not set
1 parent 55a3e33 commit 0294ac1

File tree

5 files changed

+124
-14
lines changed

5 files changed

+124
-14
lines changed

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ PHP 8.5 UPGRADE NOTES
429429
. socket_set_option with multicast context throws a ValueError
430430
when the created socket is not of AF_INET/AF_INET6 family.
431431

432+
- Standard:
433+
. file() now correctly honors the FILE_SKIP_EMPTY_LINES flag even when
434+
FILE_IGNORE_NEW_LINES is not set. Previously, FILE_SKIP_EMPTY_LINES
435+
only worked when combined with FILE_IGNORE_NEW_LINES. See GH-18120.
436+
432437
- Tidy:
433438
. tidy::__construct/parseFile/parseString now throws a ValueError
434439
if the configuration contains an invalid or set a read-only

ext/standard/file.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,16 @@ PHP_FUNCTION(file)
640640
do {
641641
p++;
642642
parse_eol:
643+
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++;
647+
}
648+
if (p-s-windows_eol == 1) {
649+
s = p;
650+
continue;
651+
}
652+
}
643653
add_index_stringl(return_value, i++, s, p-s);
644654
s = p;
645655
} while ((p = memchr(p, eol_marker, (e-p))));
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
--TEST--
2+
Test file() function with FILE_SKIP_EMPTY_LINES flag
3+
--FILE--
4+
<?php
5+
$file_path = __DIR__;
6+
$test_file = $file_path . "/file_skip_empty_lines.tmp";
7+
8+
$test_data = "First\n\nSecond\n\n\nThird\n";
9+
file_put_contents($test_file, $test_data);
10+
11+
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
12+
var_dump($lines);
13+
14+
$lines = file($test_file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
15+
var_dump($lines);
16+
17+
$lines = file($test_file, FILE_IGNORE_NEW_LINES);
18+
var_dump($lines);
19+
20+
$lines = file($test_file);
21+
var_dump($lines);
22+
23+
$test_data_win = "First\r\n\r\nSecond\r\n";
24+
file_put_contents($test_file, $test_data_win);
25+
26+
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
27+
var_dump($lines);
28+
29+
$lines = file($test_file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
30+
var_dump($lines);
31+
32+
unlink($test_file);
33+
?>
34+
--EXPECT--
35+
array(3) {
36+
[0]=>
37+
string(6) "First
38+
"
39+
[1]=>
40+
string(7) "Second
41+
"
42+
[2]=>
43+
string(6) "Third
44+
"
45+
}
46+
array(3) {
47+
[0]=>
48+
string(5) "First"
49+
[1]=>
50+
string(6) "Second"
51+
[2]=>
52+
string(5) "Third"
53+
}
54+
array(6) {
55+
[0]=>
56+
string(5) "First"
57+
[1]=>
58+
string(0) ""
59+
[2]=>
60+
string(6) "Second"
61+
[3]=>
62+
string(0) ""
63+
[4]=>
64+
string(0) ""
65+
[5]=>
66+
string(5) "Third"
67+
}
68+
array(6) {
69+
[0]=>
70+
string(6) "First
71+
"
72+
[1]=>
73+
string(1) "
74+
"
75+
[2]=>
76+
string(7) "Second
77+
"
78+
[3]=>
79+
string(1) "
80+
"
81+
[4]=>
82+
string(1) "
83+
"
84+
[5]=>
85+
string(6) "Third
86+
"
87+
}
88+
array(3) {
89+
[0]=>
90+
string(7) "First
91+
"
92+
[1]=>
93+
string(2) "
94+
"
95+
[2]=>
96+
string(8) "Second
97+
"
98+
}
99+
array(2) {
100+
[0]=>
101+
string(5) "First"
102+
[1]=>
103+
string(6) "Second"
104+
}

ext/standard/tests/file/file_variation.phpt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,14 @@ array(5) {
106106
[4]=>
107107
string(5) " data"
108108
}
109-
array(5) {
109+
array(3) {
110110
[0]=>
111111
string(4) "Gar
112112
"
113113
[1]=>
114-
string(1) "
115-
"
116-
[2]=>
117114
string(6) "bage
118115
"
119-
[3]=>
120-
string(1) "
121-
"
122-
[4]=>
116+
[2]=>
123117
string(5) " data"
124118
}
125119
*** Testing with variation in use_include_path argument ***

ext/standard/tests/file/file_variation7.phpt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,17 @@ array(5) {
6262
}
6363

6464
file() with FILE_SKIP_EMPTY_LINES:
65-
array(5) {
65+
array(4) {
6666
[0]=>
6767
string(7) "Line 1
6868
"
6969
[1]=>
70-
string(1) "
71-
"
72-
[2]=>
7370
string(2) "
7471
"
75-
[3]=>
72+
[2]=>
7673
string(3) "
7774
"
78-
[4]=>
75+
[3]=>
7976
string(7) "\Line 3"
8077
}
8178

0 commit comments

Comments
 (0)