Code cleanup
This commit is contained in:
parent
078c5be1c6
commit
acc4be2a35
|
@ -7,10 +7,6 @@
|
|||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
0E0F16091D197D6000C92580 /* AnyIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0F16081D197D6000C92580 /* AnyIterator.swift */; };
|
||||
0E0F160A1D197D6000C92580 /* AnyIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0F16081D197D6000C92580 /* AnyIterator.swift */; };
|
||||
0E0F160B1D197D6000C92580 /* AnyIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0F16081D197D6000C92580 /* AnyIterator.swift */; };
|
||||
0E0F160C1D197D6000C92580 /* AnyIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0F16081D197D6000C92580 /* AnyIterator.swift */; };
|
||||
0E0F160E1D197DB800C92580 /* Endian.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0F160D1D197DB800C92580 /* Endian.swift */; };
|
||||
0E0F160F1D197DB800C92580 /* Endian.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0F160D1D197DB800C92580 /* Endian.swift */; };
|
||||
0E0F16101D197DB800C92580 /* Endian.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0E0F160D1D197DB800C92580 /* Endian.swift */; };
|
||||
|
@ -82,7 +78,6 @@
|
|||
/* End PBXContainerItemProxy section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
0E0F16081D197D6000C92580 /* AnyIterator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnyIterator.swift; sourceTree = "<group>"; };
|
||||
0E0F160D1D197DB800C92580 /* Endian.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Endian.swift; sourceTree = "<group>"; };
|
||||
0E7E8C811D0BC7BB0057A1C1 /* CSV.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CSV.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
0E7E8C8B1D0BC7BB0057A1C1 /* CSVTests-iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "CSVTests-iOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
|
@ -187,7 +182,6 @@
|
|||
0E7E8C9B1D0BC7F10057A1C1 /* Sources */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
0E0F16081D197D6000C92580 /* AnyIterator.swift */,
|
||||
0EA2AB7B1D183B45003EC967 /* BinaryReader.swift */,
|
||||
0E7E8C9D1D0BC7F10057A1C1 /* CSV.swift */,
|
||||
0E9317D31D0DB2F200AC20A0 /* CSV+init.swift */,
|
||||
|
@ -505,7 +499,6 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
0E9317D51D0DB2F200AC20A0 /* CSV+init.swift in Sources */,
|
||||
0E0F160A1D197D6000C92580 /* AnyIterator.swift in Sources */,
|
||||
0EA2AB821D183BA9003EC967 /* UnicodeIterator.swift in Sources */,
|
||||
0E9317DA1D0DB30800AC20A0 /* CSV+subscript.swift in Sources */,
|
||||
0E7E8CA11D0BC7F10057A1C1 /* CSV.swift in Sources */,
|
||||
|
@ -530,7 +523,6 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
0E9317D71D0DB2F200AC20A0 /* CSV+init.swift in Sources */,
|
||||
0E0F160C1D197D6000C92580 /* AnyIterator.swift in Sources */,
|
||||
0EA2AB841D183BA9003EC967 /* UnicodeIterator.swift in Sources */,
|
||||
0E9317DC1D0DB30800AC20A0 /* CSV+subscript.swift in Sources */,
|
||||
0E7E8CBE1D0BC9D70057A1C1 /* CSV.swift in Sources */,
|
||||
|
@ -545,7 +537,6 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
0E9317D41D0DB2F200AC20A0 /* CSV+init.swift in Sources */,
|
||||
0E0F16091D197D6000C92580 /* AnyIterator.swift in Sources */,
|
||||
0EA2AB811D183BA9003EC967 /* UnicodeIterator.swift in Sources */,
|
||||
0E9317D91D0DB30800AC20A0 /* CSV+subscript.swift in Sources */,
|
||||
0E7E8CE01D0BCA8E0057A1C1 /* CSV.swift in Sources */,
|
||||
|
@ -570,7 +561,6 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
0E9317D61D0DB2F200AC20A0 /* CSV+init.swift in Sources */,
|
||||
0E0F160B1D197D6000C92580 /* AnyIterator.swift in Sources */,
|
||||
0EA2AB831D183BA9003EC967 /* UnicodeIterator.swift in Sources */,
|
||||
0E9317DB1D0DB30800AC20A0 /* CSV+subscript.swift in Sources */,
|
||||
0E7E8D001D0BCDCF0057A1C1 /* CSV.swift in Sources */,
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
//
|
||||
// AnyIterator.swift
|
||||
// CSV
|
||||
//
|
||||
// Created by Yasuhiro Hatta on 2016/06/21.
|
||||
// Copyright © 2016 yaslab. All rights reserved.
|
||||
//
|
||||
|
||||
internal struct AnyIterator<T>: IteratorProtocol {
|
||||
|
||||
private var _base_next: (() -> T?)
|
||||
|
||||
internal init<U: IteratorProtocol>(base: U) where U.Element == T {
|
||||
var base = base
|
||||
_base_next = { base.next() }
|
||||
}
|
||||
|
||||
internal mutating func next() -> T? {
|
||||
return _base_next()
|
||||
}
|
||||
|
||||
}
|
|
@ -9,7 +9,9 @@
|
|||
import Foundation
|
||||
|
||||
extension CSV {
|
||||
|
||||
|
||||
// TODO: Documentation
|
||||
/// No overview available.
|
||||
public init(
|
||||
stream: InputStream,
|
||||
hasHeaderRow: Bool = defaultHasHeaderRow,
|
||||
|
@ -19,11 +21,13 @@ extension CSV {
|
|||
{
|
||||
try self.init(stream: stream, codecType: UTF8.self, hasHeaderRow: hasHeaderRow, trimFields: trimFields, delimiter: delimiter)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
extension CSV {
|
||||
|
||||
// TODO: Documentation
|
||||
/// No overview available.
|
||||
public init(
|
||||
string: String,
|
||||
hasHeaderRow: Bool = defaultHasHeaderRow,
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
extension CSV {
|
||||
|
||||
// TODO: Documentation
|
||||
/// No overview available.
|
||||
public subscript(key: String) -> String? {
|
||||
get {
|
||||
guard let headerRow = headerRow, let currentRow = currentRow else {
|
||||
|
|
|
@ -16,7 +16,21 @@ internal let defaultHasHeaderRow = false
|
|||
internal let defaultTrimFields = false
|
||||
internal let defaultDelimiter = UnicodeScalar(",")!
|
||||
|
||||
public struct CSV: IteratorProtocol, Sequence {
|
||||
extension CSV: Sequence { }
|
||||
|
||||
extension CSV: IteratorProtocol {
|
||||
|
||||
// TODO: Documentation
|
||||
/// No overview available.
|
||||
public mutating func next() -> [String]? {
|
||||
return readRow()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TODO: Documentation
|
||||
/// No overview available.
|
||||
public struct CSV {
|
||||
|
||||
private var iterator: AnyIterator<UnicodeScalar>
|
||||
private let trimFields: Bool
|
||||
|
@ -37,9 +51,10 @@ public struct CSV: IteratorProtocol, Sequence {
|
|||
hasHeaderRow: Bool,
|
||||
trimFields: Bool,
|
||||
delimiter: UnicodeScalar)
|
||||
throws where T.Element == UnicodeScalar
|
||||
throws
|
||||
where T.Element == UnicodeScalar
|
||||
{
|
||||
self.iterator = AnyIterator(base: iterator)
|
||||
self.iterator = AnyIterator(iterator)
|
||||
self.trimFields = trimFields
|
||||
self.delimiter = delimiter
|
||||
|
||||
|
@ -119,49 +134,7 @@ public struct CSV: IteratorProtocol, Sequence {
|
|||
try self.init(iterator: iterator, hasHeaderRow: hasHeaderRow, trimFields: trimFields, delimiter: delimiter)
|
||||
}
|
||||
|
||||
// MARK: IteratorProtocol
|
||||
|
||||
/// Advances and returns the next element of the underlying sequence, or
|
||||
/// `nil` if no next element exists.
|
||||
///
|
||||
/// Repeatedly calling this method returns, in order, all the elements of the
|
||||
/// underlying sequence. After the sequence has run out of elements, the
|
||||
/// `next()` method returns `nil`.
|
||||
///
|
||||
/// You must not call this method if it has previously returned `nil` or if
|
||||
/// any other copy of this iterator has been advanced with a call to its
|
||||
/// `next()` method.
|
||||
///
|
||||
/// The following example shows how an iterator can be used explicitly to
|
||||
/// emulate a `for`-`in` loop. First, retrieve a sequence's iterator, and
|
||||
/// then call the iterator's `next()` method until it returns `nil`.
|
||||
///
|
||||
/// let numbers = [2, 3, 5, 7]
|
||||
/// var numbersIterator = numbers.makeIterator()
|
||||
///
|
||||
/// while let num = numbersIterator.next() {
|
||||
/// print(num)
|
||||
/// }
|
||||
/// // Prints "2"
|
||||
/// // Prints "3"
|
||||
/// // Prints "5"
|
||||
/// // Prints "7"
|
||||
///
|
||||
/// - Returns: The next element in the underlying sequence if a next element
|
||||
/// exists; otherwise, `nil`.
|
||||
public mutating func next() -> [String]? {
|
||||
return readRow()
|
||||
}
|
||||
|
||||
internal mutating func moveNext() -> UnicodeScalar? {
|
||||
if back != nil {
|
||||
defer { back = nil }
|
||||
return back
|
||||
}
|
||||
return iterator.next()
|
||||
}
|
||||
|
||||
internal mutating func readRow() -> [String]? {
|
||||
fileprivate mutating func readRow() -> [String]? {
|
||||
currentRow = nil
|
||||
|
||||
var next = moveNext()
|
||||
|
@ -206,7 +179,7 @@ public struct CSV: IteratorProtocol, Sequence {
|
|||
return row
|
||||
}
|
||||
|
||||
internal mutating func readField(quoted: Bool) -> (String, Bool) {
|
||||
private mutating func readField(quoted: Bool) -> (String, Bool) {
|
||||
var field = ""
|
||||
|
||||
var next = moveNext()
|
||||
|
@ -276,4 +249,12 @@ public struct CSV: IteratorProtocol, Sequence {
|
|||
return (field, true)
|
||||
}
|
||||
|
||||
private mutating func moveNext() -> UnicodeScalar? {
|
||||
if back != nil {
|
||||
defer { back = nil }
|
||||
return back
|
||||
}
|
||||
return iterator.next()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
// Copyright © 2016 yaslab. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
// TODO: Documentation
|
||||
/// No overview available.
|
||||
public enum CSVError: Error {
|
||||
case cannotOpenFile
|
||||
case cannotReadFile
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
// Copyright © 2016 yaslab. All rights reserved.
|
||||
//
|
||||
|
||||
// TODO: Documentation
|
||||
/// No overview available.
|
||||
public enum Endian {
|
||||
case big
|
||||
case little
|
||||
|
|
Loading…
Reference in New Issue