Add File:Lines()#2216
Closed
Zaurzo wants to merge 0 commit intoFacepunch:masterfrom
Closed
Conversation
Lexicality
reviewed
Feb 18, 2025
|
|
||
| lines.i = 1 | ||
|
|
||
| return lineIterator, lines |
Contributor
There was a problem hiding this comment.
❓ why not return ipairs(lines)?
Contributor
Author
There was a problem hiding this comment.
I wanted it to be similar to the vanilla Lua io.lines iterator. Check the request I linked
Contributor
There was a problem hiding this comment.
ah nevermind, I understand now
Contributor
Author
|
Added Example: This prints out the lines with berries in them. local f = file.Open('foods.txt', 'wb', 'DATA')
f:Write('apple\n')
f:Write('watermelon\n')
f:Write('banana\n')
f:Write('strawberry\n')
f:Write('blueberry\n')
f:Write('cranberry\n')
f:Write('pizza\n')
f:Close()
local f = file.Open('foods.txt', 'rb', 'DATA')
for line in f:Lines(3, 6) do
print(line)
end
f:Close()Output: |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds a native Lua function to the file class that returns an iterator for the lines of the file. This would close this very old request: Facepunch/garrysmod-requests#1101
You can already iterate through the lines of the file like so:
However, as the request linked pointed out, this is cumbersome; and also quite frankly, ugly.
There is a cleaner way to do this, suggested by Spar:
Unfortunately, there's a few problems with this. One, it's way slower than the above method. Two,
ReadLineadvances the file pointer, which may be a problem; and, three,ReadLineis also limited to 8192 characters! Another thing to note is that the return value ofReadLinecontains the\ncharacter at the end of the string.This pull request solves every one of these problems!
Wow!
This addition provides a clean and simple way to iterate over the lines of a file handle, whilst keeping speed and accuracy!
Speed Comparisons
Code:
Output:
As you can see here, Spar's method is very slow. On the other hand, my method is roughly the same speed as the regular method!