Created
November 16, 2019 16:05
-
-
Save jonahaung/af0d0d519ea2c4c7463c28eebbea4a9a to your computer and use it in GitHub Desktop.
Text Reader
This file contains 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
import Foundation | |
public class TextReader { | |
let encoding: String.Encoding | |
let chunkSize: Int | |
let fileHandle: FileHandle | |
var buffer: Data | |
let delimPattern : Data | |
var isAtEOF: Bool = false | |
public init?(url: URL, delimeter: String = "\n", encoding: String.Encoding = .utf8, chunkSize: Int = 4096) | |
{ | |
guard let fileHandle = try? FileHandle(forReadingFrom: url) else { return nil } | |
self.fileHandle = fileHandle | |
self.chunkSize = chunkSize | |
self.encoding = encoding | |
buffer = Data(capacity: chunkSize) | |
delimPattern = delimeter.data(using: .utf8)! | |
} | |
deinit { | |
fileHandle.closeFile() | |
} | |
public func rewind() { | |
fileHandle.seek(toFileOffset: 0) | |
buffer.removeAll(keepingCapacity: true) | |
isAtEOF = false | |
} | |
public func nextLine() -> String? { | |
if isAtEOF { return nil } | |
repeat { | |
if let range = buffer.range(of: delimPattern, options: [], in: buffer.startIndex..<buffer.endIndex) { | |
let subData = buffer.subdata(in: buffer.startIndex..<range.lowerBound) | |
let line = String(data: subData, encoding: encoding) | |
buffer.replaceSubrange(buffer.startIndex..<range.upperBound, with: []) | |
return line | |
} else { | |
let tempData = fileHandle.readData(ofLength: chunkSize) | |
if tempData.count == 0 { | |
isAtEOF = true | |
return (buffer.count > 0) ? String(data: buffer, encoding: encoding) : nil | |
} | |
buffer.append(tempData) | |
} | |
} while true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment