-
Notifications
You must be signed in to change notification settings - Fork 7.9k
file() Feature Request - added support for file() to skip blank lines #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,6 +295,7 @@ PHP_MINIT_FUNCTION(file) | |
REGISTER_LONG_CONSTANT("FILE_USE_INCLUDE_PATH", PHP_FILE_USE_INCLUDE_PATH, CONST_CS | CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("FILE_IGNORE_NEW_LINES", PHP_FILE_IGNORE_NEW_LINES, CONST_CS | CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("FILE_SKIP_EMPTY_LINES", PHP_FILE_SKIP_EMPTY_LINES, CONST_CS | CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("FILE_INDEX_LINES", PHP_FILE_INDEX_LINES, CONST_CS | CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("FILE_APPEND", PHP_FILE_APPEND, CONST_CS | CONST_PERSISTENT); | ||
REGISTER_LONG_CONSTANT("FILE_NO_DEFAULT_CONTEXT", PHP_FILE_NO_DEFAULT_CONTEXT, CONST_CS | CONST_PERSISTENT); | ||
|
||
|
@@ -715,6 +716,7 @@ PHP_FUNCTION(file) | |
zend_bool use_include_path; | ||
zend_bool include_new_line; | ||
zend_bool skip_blank_lines; | ||
zend_bool index_lines; | ||
php_stream *stream; | ||
zval *zcontext = NULL; | ||
php_stream_context *context = NULL; | ||
|
@@ -723,14 +725,20 @@ PHP_FUNCTION(file) | |
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|lr!", &filename, &filename_len, &flags, &zcontext) == FAILURE) { | ||
return; | ||
} | ||
if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) { | ||
if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_INDEX_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) { | ||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%ld' flag is not supported", flags); | ||
RETURN_FALSE; | ||
} | ||
|
||
use_include_path = flags & PHP_FILE_USE_INCLUDE_PATH; | ||
include_new_line = !(flags & PHP_FILE_IGNORE_NEW_LINES); | ||
skip_blank_lines = flags & PHP_FILE_SKIP_EMPTY_LINES; | ||
|
||
/* Added a new line-number to array-key indexing optional flag by Sherif */ | ||
index_lines = flags & PHP_FILE_INDEX_LINES; | ||
if (index_lines) { | ||
++i; | ||
} | ||
|
||
context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); | ||
|
||
|
@@ -759,7 +767,20 @@ PHP_FUNCTION(file) | |
* will not need to be done for every single line in the file. */ | ||
if (include_new_line) { | ||
do { | ||
p++; | ||
int windows_eol = 0; | ||
if (p != target_buf && eol_marker == '\n' && *(p - 1) == '\r') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will *(p -1) always be valid? - I didn't check all the code paths, on first sight it looks dangerous. |
||
windows_eol++; | ||
} | ||
if (skip_blank_lines && !(p-s-windows_eol)) { | ||
s = ++p; | ||
if (index_lines) { | ||
++i; | ||
} | ||
continue; | ||
} | ||
else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The else should be on the same line as } |
||
p++; | ||
} | ||
parse_eol: | ||
add_index_stringl(return_value, i++, estrndup(s, p-s), p-s, 0); | ||
s = p; | ||
|
@@ -772,6 +793,9 @@ PHP_FUNCTION(file) | |
} | ||
if (skip_blank_lines && !(p-s-windows_eol)) { | ||
s = ++p; | ||
if (index_lines) { | ||
++i; | ||
} | ||
continue; | ||
} | ||
add_index_stringl(return_value, i++, estrndup(s, p-s-windows_eol), p-s-windows_eol, 0); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
Test file() function : Support for FILE_SKIP_EMPTY_LINES without FILE_IGNORE_NEW_LINES and FILE_INDEX_LINES flags | ||
--FILE-- | ||
<?php | ||
|
||
$f = tmpnam(__DIR__, "phpfiletest"); | ||
file_put_contents($f, "Line 1" . PHP_EOL . "Line 2" . PHP_EOL . PHP_EOL . "Last line..."); | ||
$a = file($f, FILE_SKIP_EMPTY_LINES); | ||
$b = file($f, FILE_INDEX_LINES | FILE_SKIP_EMPTY_LINES); | ||
|
||
var_dump($a, $b); | ||
|
||
@unlink($f); | ||
|
||
?> | ||
--EXPECTF-- | ||
array(3) { | ||
[0]=> | ||
string(7) "Line 1 | ||
" | ||
[1]=> | ||
string(7) "Line 2 | ||
" | ||
[2]=> | ||
string(12) "Last line..." | ||
} | ||
array(3) { | ||
[1]=> | ||
string(7) "Line 1 | ||
" | ||
[2]=> | ||
string(7) "Line 2 | ||
" | ||
[4]=> | ||
string(12) "Last line..." | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think crediting this way in a comment makes much sense and is more confusing than helping. Code comments should explain things.