-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Remove empty lines from String#each_line #16232
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
base: master
Are you sure you want to change the base?
Remove empty lines from String#each_line #16232
Conversation
|
I am concerned about handling "\n" lines when given |
|
The line itself is still empty? |
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.
remove_empty applies a filter, chomp defines how the items are presented.
The empty line filter should not be affected by whether non-empty lines end with a NL character or not.
Confusion that may happen is what is filtered with the It seems logical to apply the filter to lines, that's a "\n" line will be filtered out regardless of the |
src/string.cr
Outdated
| if remove_empty | ||
| line_length = count - 1 | ||
| if offset + line_length > 0 && to_unsafe[offset + line_length - 1] === '\r' | ||
| line_length -= 1 | ||
| end | ||
|
|
||
| skip_line = line_length == 0 | ||
| else | ||
| skip_line = false | ||
| end |
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.
suggestion: This logic seems a bit convoluted.
The empty line detection can be simplified to byte_index == offset || (byte_index == offset + 1 && to_unsafe[byte_index - 1] === '\r').
Also, the loop might be a bit better readable if directly skip to the next iteration with next.
| if remove_empty | |
| line_length = count - 1 | |
| if offset + line_length > 0 && to_unsafe[offset + line_length - 1] === '\r' | |
| line_length -= 1 | |
| end | |
| skip_line = line_length == 0 | |
| else | |
| skip_line = false | |
| end | |
| if remove_empty && (byte_index == offset || (byte_index == offset + 1 && to_unsafe[byte_index - 1] === '\r')) | |
| offset = byte_index + 1 | |
| next | |
| end |
Ditto for the iterator. There we can return early with self.next.
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.
Thank you! Done.
Changes:
remove_emptyparameter for methoddef each_line(chomp : Bool = true, & : String ->) : Nilremove_emptyparameter for methoddef each_line(chomp = true)Closes #15149