Code refactoring

This commit is contained in:
Yasuhiro Hatta 2016-06-23 00:52:17 +09:00
parent 1f2efdfd0f
commit f9df2e5c9d
5 changed files with 57 additions and 41 deletions

View File

@ -8,15 +8,15 @@
import Foundation import Foundation
struct AnyIterator<T>: IteratorProtocol { internal struct AnyIterator<T>: IteratorProtocol {
private var _base_next: (() -> T?) private var _base_next: (() -> T?)
init<U: IteratorProtocol where U.Element == T>(base: inout U) { internal init<U: IteratorProtocol where U.Element == T>(base: inout U) {
_base_next = { base.next() } _base_next = { base.next() }
} }
mutating func next() -> T? { internal mutating func next() -> T? {
return _base_next() return _base_next()
} }

View File

@ -43,7 +43,7 @@ internal class BinaryReader {
private let bufferSize = 4 private let bufferSize = 4
private var bufferOffset = 0 private var bufferOffset = 0
init(stream: InputStream, encoding: String.Encoding = .utf8, closeOnDeinit: Bool = true) { internal init(stream: InputStream, encoding: String.Encoding = .utf8, closeOnDeinit: Bool = true) {
var encoding = encoding var encoding = encoding
if stream.streamStatus == .notOpen { if stream.streamStatus == .notOpen {
@ -80,7 +80,7 @@ internal class BinaryReader {
return stream.read(buffer + i, maxLength: maxLength - i) return stream.read(buffer + i, maxLength: maxLength - i)
} }
func readUInt8() throws -> UInt8 { internal func readUInt8() throws -> UInt8 {
// if stream.streamStatus == .Closed { // if stream.streamStatus == .Closed {
// // ObjectDisposedException // // ObjectDisposedException
// throw NSError(domain: "", code: 0, userInfo: nil) // throw NSError(domain: "", code: 0, userInfo: nil)
@ -103,7 +103,7 @@ internal class BinaryReader {
return buffer[0] return buffer[0]
} }
func readUInt16() throws -> UInt16 { internal func readUInt16() throws -> UInt16 {
let bufferSize = 2 let bufferSize = 2
var buffer = [UInt8](repeating: 0, count: bufferSize) var buffer = [UInt8](repeating: 0, count: bufferSize)
let length = readStream(&buffer, maxLength: bufferSize) let length = readStream(&buffer, maxLength: bufferSize)
@ -126,7 +126,7 @@ internal class BinaryReader {
} }
} }
func readUInt32() throws -> UInt32 { internal func readUInt32() throws -> UInt32 {
let bufferSize = 4 let bufferSize = 4
var buffer = [UInt8](repeating: 0, count: bufferSize) var buffer = [UInt8](repeating: 0, count: bufferSize)
let length = readStream(&buffer, maxLength: bufferSize) let length = readStream(&buffer, maxLength: bufferSize)
@ -153,21 +153,21 @@ internal class BinaryReader {
extension BinaryReader { extension BinaryReader {
struct UInt8Iterator: Sequence, IteratorProtocol { internal struct UInt8Iterator: Sequence, IteratorProtocol {
let reader: BinaryReader private let reader: BinaryReader
init(reader: BinaryReader) { private init(reader: BinaryReader) {
self.reader = reader self.reader = reader
} }
mutating func next() -> UInt8? { internal mutating func next() -> UInt8? {
return try? reader.readUInt8() return try? reader.readUInt8()
} }
} }
func makeUInt8Iterator() -> UInt8Iterator { internal func makeUInt8Iterator() -> UInt8Iterator {
return UInt8Iterator(reader: self) return UInt8Iterator(reader: self)
} }
@ -175,21 +175,21 @@ extension BinaryReader {
extension BinaryReader { extension BinaryReader {
struct UInt16Iterator: Sequence, IteratorProtocol { internal struct UInt16Iterator: Sequence, IteratorProtocol {
let reader: BinaryReader private let reader: BinaryReader
init(reader: BinaryReader) { private init(reader: BinaryReader) {
self.reader = reader self.reader = reader
} }
mutating func next() -> UInt16? { internal mutating func next() -> UInt16? {
return try? reader.readUInt16() return try? reader.readUInt16()
} }
} }
func makeUInt16Iterator() -> UInt16Iterator { internal func makeUInt16Iterator() -> UInt16Iterator {
return UInt16Iterator(reader: self) return UInt16Iterator(reader: self)
} }
@ -197,21 +197,21 @@ extension BinaryReader {
extension BinaryReader { extension BinaryReader {
struct UInt32Iterator: Sequence, IteratorProtocol { internal struct UInt32Iterator: Sequence, IteratorProtocol {
let reader: BinaryReader private let reader: BinaryReader
init(reader: BinaryReader) { private init(reader: BinaryReader) {
self.reader = reader self.reader = reader
} }
mutating func next() -> UInt32? { internal mutating func next() -> UInt32? {
return try? reader.readUInt32() return try? reader.readUInt32()
} }
} }
func makeUInt32Iterator() -> UInt32Iterator { internal func makeUInt32Iterator() -> UInt32Iterator {
return UInt32Iterator(reader: self) return UInt32Iterator(reader: self)
} }

View File

@ -97,20 +97,36 @@ public struct CSV: IteratorProtocol, Sequence {
innerStream = stream innerStream = stream
} }
/**
Close stream.
*/
// public mutating func close() {
// if !closed {
// if let stream = innerStream {
// stream.close()
// }
// closed = true
// }
// }
// MARK: IteratorProtocol // 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]? { public mutating func next() -> [String]? {
return readRow() return readRow()
} }

View File

@ -8,7 +8,7 @@
import Foundation import Foundation
enum Endian { internal enum Endian {
case big case big
case little case little
case unknown case unknown
@ -16,7 +16,7 @@ enum Endian {
extension String.Encoding { extension String.Encoding {
var endian: Endian { internal var endian: Endian {
switch self { switch self {
case String.Encoding.utf16: return .big case String.Encoding.utf16: return .big
case String.Encoding.utf16BigEndian: return .big case String.Encoding.utf16BigEndian: return .big

View File

@ -8,21 +8,21 @@
import Foundation import Foundation
struct UnicodeIterator< internal struct UnicodeIterator<
Input: IteratorProtocol, Input: IteratorProtocol,
InputEncoding: UnicodeCodec InputEncoding: UnicodeCodec
where InputEncoding.CodeUnit == Input.Element> where InputEncoding.CodeUnit == Input.Element>
: IteratorProtocol { : IteratorProtocol {
var input: Input private var input: Input
var inputEncoding: InputEncoding private var inputEncoding: InputEncoding
init(input: Input, inputEncoding: InputEncoding.Type) { internal init(input: Input, inputEncoding: InputEncoding.Type) {
self.input = input self.input = input
self.inputEncoding = inputEncoding.init() self.inputEncoding = inputEncoding.init()
} }
mutating func next() -> UnicodeScalar? { internal mutating func next() -> UnicodeScalar? {
switch inputEncoding.decode(&input) { switch inputEncoding.decode(&input) {
case .scalarValue(let c): return c case .scalarValue(let c): return c
case .emptyInput: return nil case .emptyInput: return nil