From acc4be2a3594782da4a363fc8f00dbfd64dd1931 Mon Sep 17 00:00:00 2001 From: Yasuhiro Hatta Date: Tue, 18 Oct 2016 00:34:12 +0900 Subject: [PATCH] Code cleanup --- CSV.xcodeproj/project.pbxproj | 10 ----- Sources/AnyIterator.swift | 22 ---------- Sources/CSV+init.swift | 8 +++- Sources/CSV+subscript.swift | 2 + Sources/CSV.swift | 75 +++++++++++++---------------------- Sources/CSVError.swift | 4 +- Sources/Endian.swift | 2 + 7 files changed, 40 insertions(+), 83 deletions(-) delete mode 100644 Sources/AnyIterator.swift diff --git a/CSV.xcodeproj/project.pbxproj b/CSV.xcodeproj/project.pbxproj index 0fbaa74..fcb29bb 100755 --- a/CSV.xcodeproj/project.pbxproj +++ b/CSV.xcodeproj/project.pbxproj @@ -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 = ""; }; 0E0F160D1D197DB800C92580 /* Endian.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Endian.swift; sourceTree = ""; }; 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 */, diff --git a/Sources/AnyIterator.swift b/Sources/AnyIterator.swift deleted file mode 100644 index 9ff541e..0000000 --- a/Sources/AnyIterator.swift +++ /dev/null @@ -1,22 +0,0 @@ -// -// AnyIterator.swift -// CSV -// -// Created by Yasuhiro Hatta on 2016/06/21. -// Copyright © 2016 yaslab. All rights reserved. -// - -internal struct AnyIterator: IteratorProtocol { - - private var _base_next: (() -> T?) - - internal init(base: U) where U.Element == T { - var base = base - _base_next = { base.next() } - } - - internal mutating func next() -> T? { - return _base_next() - } - -} diff --git a/Sources/CSV+init.swift b/Sources/CSV+init.swift index 8a5076b..31e3f8b 100755 --- a/Sources/CSV+init.swift +++ b/Sources/CSV+init.swift @@ -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, diff --git a/Sources/CSV+subscript.swift b/Sources/CSV+subscript.swift index eed1ee8..0f4be01 100755 --- a/Sources/CSV+subscript.swift +++ b/Sources/CSV+subscript.swift @@ -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 { diff --git a/Sources/CSV.swift b/Sources/CSV.swift index 7b010bc..2d66557 100755 --- a/Sources/CSV.swift +++ b/Sources/CSV.swift @@ -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 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() + } + } diff --git a/Sources/CSVError.swift b/Sources/CSVError.swift index 9361e06..21e18b2 100644 --- a/Sources/CSVError.swift +++ b/Sources/CSVError.swift @@ -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 diff --git a/Sources/Endian.swift b/Sources/Endian.swift index 6d40b0c..c2abced 100644 --- a/Sources/Endian.swift +++ b/Sources/Endian.swift @@ -6,6 +6,8 @@ // Copyright © 2016 yaslab. All rights reserved. // +// TODO: Documentation +/// No overview available. public enum Endian { case big case little