Skip to content

Commit

Permalink
add exit from read cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzh committed Jun 27, 2020
1 parent e9c2e9e commit bfb4be0
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions Sources/FileUtils/ReadFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,29 @@ public extension FileUtils {
try String(contentsOf: url, encoding: encoding)
}

class func readFileByLine(_ url: URL, _ handler: (_ line: String) -> Void) throws {
let file: FileReader = .init(fileURL: url)
class func readFileByLine(_ path: String, _ handler: (_ line: String) -> Void) throws {
let file: FileReader = .init(filePath: path)
try file.open()
defer { file.close() }
while let line: String = try file.readLine() {
handler(line)
}
}

class func readFileByLine(_ path: String, _ handler: (_ line: String) -> Void) throws {
class func readFileByLine(_ url: URL, _ handler: (_ line: String) -> Void) throws {
try readFileByLine(url.path, handler)
}

class func readFileByLine(_ path: String, _ handler: (_ line: String) -> Bool) throws {
let file: FileReader = .init(filePath: path)
try file.open()
defer { file.close() }
while let line: String = try file.readLine() {
handler(line)
if !handler(line) { break }
}
}

class func readFileByLine(_ url: URL, _ handler: (_ line: String) -> Bool) throws {
try readFileByLine(url.path, handler)
}
}

0 comments on commit bfb4be0

Please sign in to comment.