Skip to content

Commit feea545

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

File tree

5 files changed

+123
-14
lines changed

5 files changed

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

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)