Skip to content

Instantly share code, notes, and snippets.

@codelynx
Last active June 19, 2023 00:32
Show Gist options
  • Save codelynx/c1de603a85e7503fe9597d027e93f4de to your computer and use it in GitHub Desktop.
Save codelynx/c1de603a85e7503fe9597d027e93f4de to your computer and use it in GitHub Desktop.
enumerate lines from FileHandle (NSFileHandle) in swift3 – good for enumerating huge text file line by line
//
// FileHandle+Z.swift
// ZKit
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
extension FileHandle {
func enumerateLines(_ block: @escaping (String, UnsafeMutablePointer<Bool>) -> Void) {
// find the end of file
var offset = self.offsetInFile
let eof = self.seekToEndOfFile()
self.seek(toFileOffset: offset)
let blockSize = 1024
var buffer = Data()
// process to the end of file
while offset + UInt64(buffer.count) < eof {
var found = false
// make sure buffer contains at least one CR, LF or null
while !found && offset + UInt64(buffer.count) < eof {
let block = self.readData(ofLength: blockSize)
buffer.append(block)
for byte in block {
if [0x0d, 0x0a, 0x00].contains(byte) {
found = true ; break
}
}
}
// retrieve lines within the buffer
var index = 0
var head = 0 // head of line
var done = false
buffer.enumerateBytes({ (pointer, count, stop) in
while index < count {
// find a line terminator
if [0x0d, 0x0a, 0x00].contains(pointer[index]) {
let lineData = Data(pointer[head ..< index])
if let line = String(bytes: lineData, encoding: .utf8) {
block(line, &stop)
if pointer[index] == 0x0d && index+1 < count && pointer[index+1] == 0x0a {
index += 2 ; head = index
}
else { index += 1 ; head = index }
if stop { done = true ; return } // stop requested
}
else { return } // end of enumerateLines
}
else { index += 1 }
}
})
offset += UInt64(head)
buffer.replaceSubrange(0 ..< head, with: Data())
if done { // stop requested
self.seek(toFileOffset: offset)
return
}
}
}
}
@codelynx
Copy link
Author

codelynx commented Nov 22, 2016

let fileURL = Bundle.main.url(forResource: "huge_utf8_text", withExtension: "txt")!
let fileHandle = try! FileHandle(forReadingFrom: fileURL)
fileHandle.enumerateLines { (string, stop) in
	if someCondition { stop.pointee = true }
	print(string)
}

@type-111
Copy link

type-111 commented Jun 2, 2017

How does the property 'count' get initialized in the block called by enumerateBytes? When I run your code sample the while loop on line 61 never gets executed due to 'count' property always being equal to zero. I had to modify 'count' to 'buffer.count' on lines 61 and 67 in order for the while loop to be executed, but this does not seem like appropriate fix. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment