Swift 3.0

This commit is contained in:
Yasuhiro Hatta 2016-06-20 23:38:48 +09:00
parent b29dfe4e94
commit 27fbe635b9
5 changed files with 61 additions and 63 deletions

View File

@ -9,25 +9,25 @@
import CoreFoundation
internal func ReadBigInt16(base: UnsafePointer<Void>, byteOffset: Int) -> UInt16 {
let bytes = UnsafePointer<UInt8>(base).advancedBy(byteOffset)
let bytes = UnsafePointer<UInt8>(base).advanced(by: byteOffset)
let int16Array = UnsafePointer<UInt16>(bytes)
return CFSwapInt16BigToHost(int16Array[0])
}
internal func ReadBigInt32(base: UnsafePointer<Void>, byteOffset: Int) -> UInt32 {
let bytes = UnsafePointer<UInt8>(base).advancedBy(byteOffset)
let bytes = UnsafePointer<UInt8>(base).advanced(by: byteOffset)
let int32Array = UnsafePointer<UInt32>(bytes)
return CFSwapInt32BigToHost(int32Array[0])
}
internal func ReadLittleInt16(base: UnsafePointer<Void>, byteOffset: Int) -> UInt16 {
let bytes = UnsafePointer<UInt8>(base).advancedBy(byteOffset)
let bytes = UnsafePointer<UInt8>(base).advanced(by: byteOffset)
let int16Array = UnsafePointer<UInt16>(bytes)
return CFSwapInt16LittleToHost(int16Array[0])
}
internal func ReadLittleInt32(base: UnsafePointer<Void>, byteOffset: Int) -> UInt32 {
let bytes = UnsafePointer<UInt8>(base).advancedBy(byteOffset)
let bytes = UnsafePointer<UInt8>(base).advanced(by: byteOffset)
let int32Array = UnsafePointer<UInt32>(bytes)
return CFSwapInt32LittleToHost(int32Array[0])
}

View File

@ -13,12 +13,12 @@ extension CSV {
public convenience init(
path: String,
hasHeaderRow: Bool = defaultHasHeaderRow,
encoding: NSStringEncoding = defaultEncoding,
encoding: String.Encoding = defaultEncoding,
delimiter: CChar = defaultDelimiter,
bufferSize: Int = defaultBufferSize)
throws
{
guard let stream = NSInputStream(fileAtPath: path) else {
guard let stream = InputStream(fileAtPath: path) else {
throw CSVError.StreamError
}
try self.init(
@ -30,14 +30,14 @@ extension CSV {
}
public convenience init(
url: NSURL,
url: URL,
hasHeaderRow: Bool = defaultHasHeaderRow,
encoding: NSStringEncoding = defaultEncoding,
encoding: String.Encoding = defaultEncoding,
delimiter: CChar = defaultDelimiter,
bufferSize: Int = defaultBufferSize)
throws
{
guard let stream = NSInputStream(URL: url) else {
guard let stream = InputStream(url: url) else {
throw CSVError.StreamError
}
try self.init(
@ -56,11 +56,11 @@ extension CSV {
throws
{
let encoding = defaultEncoding
guard let data = string.dataUsingEncoding(encoding) else {
guard let data = string.data(using: encoding) else {
throw CSVError.StringEncodingMismatch
}
try self.init(
stream: NSInputStream(data: data),
stream: InputStream(data: data),
hasHeaderRow: hasHeaderRow,
encoding: encoding,
delimiter: delimiter,

View File

@ -15,7 +15,7 @@ extension CSV {
guard let headerRow = headerRow else {
return nil
}
guard let index = headerRow.indexOf(key) else {
guard let index = headerRow.index(of: key) else {
return nil
}
guard let currentRow = currentRow else {

View File

@ -13,7 +13,7 @@ private let CR: UInt32 = 0x0d //'\r'
private let DQUOTE: UInt32 = 0x22 //'"'
internal let defaultHasHeaderRow = false
internal let defaultEncoding = NSUTF8StringEncoding
internal let defaultEncoding: String.Encoding = .utf8
internal let defaultDelimiter: CChar = 0x2c //','
internal let defaultBufferSize = 8192
@ -23,10 +23,10 @@ internal let utf16LittleEndianBOM: [UInt8] = [0xff, 0xfe]
internal let utf32BigEndianBOM: [UInt8] = [0x00, 0x00, 0xfe, 0xff]
internal let utf32LittleEndianBOM: [UInt8] = [0xff, 0xfe, 0x00, 0x00]
public class CSV: SequenceType, GeneratorType {
public class CSV: Sequence, IteratorProtocol {
internal let stream: NSInputStream
internal let encoding: NSStringEncoding
internal let stream: InputStream
internal let encoding: String.Encoding
internal let delimiter: UInt32
internal let bufferSize: Int
@ -36,7 +36,7 @@ public class CSV: SequenceType, GeneratorType {
internal let charWidth: Int
internal let fieldBuffer: NSMutableData
internal var fieldBuffer: Data
internal var closed: Bool = false
@ -63,9 +63,9 @@ public class CSV: SequenceType, GeneratorType {
- parameter bufferSize: Size in bytes to be read at a time from the stream. Default: `8192`.
*/
public init(
stream: NSInputStream,
stream: InputStream,
hasHeaderRow: Bool = defaultHasHeaderRow,
encoding: NSStringEncoding = defaultEncoding,
encoding: String.Encoding = defaultEncoding,
delimiter: CChar = defaultDelimiter,
bufferSize: Int = defaultBufferSize)
throws
@ -94,12 +94,12 @@ public class CSV: SequenceType, GeneratorType {
self.buffer = UnsafeMutablePointer<UInt8>(b)
self.bufferOffset = 0
self.fieldBuffer = NSMutableData()
self.fieldBuffer = Data()
if stream.streamStatus == .NotOpen {
if stream.streamStatus == .notOpen {
stream.open()
}
if stream.streamStatus != .Open {
if stream.streamStatus != .open {
throw CSVError.StreamError
}
@ -108,45 +108,45 @@ public class CSV: SequenceType, GeneratorType {
var e = encoding
switch encoding {
case NSUTF16StringEncoding,
NSUTF16BigEndianStringEncoding,
NSUTF16LittleEndianStringEncoding:
case String.Encoding.utf16,
String.Encoding.utf16BigEndian,
String.Encoding.utf16LittleEndian:
charWidth = 2
if encoding == NSUTF16StringEncoding {
let nativeEndian = IsBigEndian()
? NSUTF16BigEndianStringEncoding
: NSUTF16LittleEndianStringEncoding
if encoding == .utf16 {
let nativeEndian: String.Encoding = IsBigEndian()
? .utf16BigEndian
: .utf16LittleEndian
e = nativeEndian
if lastReadCount >= charWidth {
if memcmp(buffer, utf16BigEndianBOM, charWidth) == 0 {
e = NSUTF16BigEndianStringEncoding
e = .utf16BigEndian
self.bufferOffset += charWidth
}
else if memcmp(buffer, utf16LittleEndianBOM, charWidth) == 0 {
e = NSUTF16LittleEndianStringEncoding
e = .utf16LittleEndian
self.bufferOffset += charWidth
}
}
}
case NSUTF32StringEncoding,
NSUTF32BigEndianStringEncoding,
NSUTF32LittleEndianStringEncoding:
case String.Encoding.utf32,
String.Encoding.utf32BigEndian,
String.Encoding.utf32LittleEndian:
charWidth = 4
if encoding == NSUTF32StringEncoding {
let nativeEndian = IsBigEndian()
? NSUTF32BigEndianStringEncoding
: NSUTF32LittleEndianStringEncoding
if encoding == .utf32 {
let nativeEndian: String.Encoding = IsBigEndian()
? .utf32BigEndian
: .utf32LittleEndian
e = nativeEndian
if lastReadCount >= charWidth {
if memcmp(buffer, utf32BigEndianBOM, charWidth) == 0 {
e = NSUTF32BigEndianStringEncoding
e = .utf32BigEndian
self.bufferOffset += charWidth
}
else if memcmp(buffer, utf32LittleEndianBOM, charWidth) == 0 {
e = NSUTF32LittleEndianStringEncoding
e = .utf32LittleEndian
self.bufferOffset += charWidth
}
}
@ -154,7 +154,7 @@ public class CSV: SequenceType, GeneratorType {
default:
charWidth = 1
if encoding == NSUTF8StringEncoding {
if encoding == .utf8 {
let bomSize = 3
if lastReadCount >= bomSize {
if memcmp(buffer, utf8BOM, charWidth) == 0 {
@ -196,7 +196,7 @@ public class CSV: SequenceType, GeneratorType {
// MARK: GeneratorType
public func next() -> [String]? {
fieldBuffer.length = 0
fieldBuffer.count = 0
currentRow = nil
if closed {
@ -218,7 +218,7 @@ public class CSV: SequenceType, GeneratorType {
while true {
if bufferOffset >= lastReadCount {
if charLength > 0 {
fieldBuffer.appendBytes(buffer + fieldStart, length: charWidth * charLength)
fieldBuffer.append(buffer + fieldStart, count: charWidth * charLength)
}
bufferOffset = 0
fieldStart = 0
@ -239,19 +239,19 @@ public class CSV: SequenceType, GeneratorType {
var c: UInt32 = 0
switch encoding {
case NSUTF16BigEndianStringEncoding:
let _c = ReadBigInt16(buffer, byteOffset: bufferOffset)
case String.Encoding.utf16BigEndian:
let _c = ReadBigInt16(base: buffer, byteOffset: bufferOffset)
c = UInt32(_c)
case NSUTF16LittleEndianStringEncoding:
let _c = ReadLittleInt16(buffer, byteOffset: bufferOffset)
case String.Encoding.utf16LittleEndian:
let _c = ReadLittleInt16(base: buffer, byteOffset: bufferOffset)
c = UInt32(_c)
case NSUTF32BigEndianStringEncoding:
c = ReadBigInt32(buffer, byteOffset: bufferOffset)
case String.Encoding.utf32BigEndian:
c = ReadBigInt32(base: buffer, byteOffset: bufferOffset)
case NSUTF32LittleEndianStringEncoding:
c = ReadLittleInt32(buffer, byteOffset: bufferOffset)
case String.Encoding.utf32LittleEndian:
c = ReadLittleInt32(base: buffer, byteOffset: bufferOffset)
default: // multi-byte character encodings
let _c = (buffer + bufferOffset)[0]
@ -271,7 +271,7 @@ public class CSV: SequenceType, GeneratorType {
}
if !escaping && prev == CR && c != LF {
fieldBuffer.appendBytes(buffer + fieldStart, length: charWidth * charLength)
fieldBuffer.append(buffer + fieldStart, count: charWidth * charLength)
break
}
@ -283,22 +283,22 @@ public class CSV: SequenceType, GeneratorType {
continue
}
if c == LF {
fieldBuffer.appendBytes(buffer + fieldStart, length: charWidth * charLength)
fieldBuffer.append(buffer + fieldStart, count: charWidth * charLength)
break
}
}
//
if !escaping && c == delimiter {
fieldBuffer.appendBytes(buffer + fieldStart, length: charWidth * charLength)
fieldBuffer.append(buffer + fieldStart, count: charWidth * charLength)
guard let field = getField(quotationCount) else {
guard let field = getField(quotationCount: quotationCount) else {
return nil
}
fields.append(field)
// reset
fieldBuffer.length = 0
fieldBuffer.count = 0
quotationCount = 0
charLength = 0
@ -309,7 +309,7 @@ public class CSV: SequenceType, GeneratorType {
}
}
guard let field = getField(quotationCount) else {
guard let field = getField(quotationCount: quotationCount) else {
return nil
}
@ -343,15 +343,13 @@ public class CSV: SequenceType, GeneratorType {
&& field.hasPrefix("\"")
&& field.hasSuffix("\"")
{
//let start = field.index(field.startIndex, offsetBy: 1)
//let end = field.index(field.endIndex, offsetBy: -1)
let start = field.startIndex.advancedBy(1)
let end = field.endIndex.advancedBy(-1)
let start = field.index(field.startIndex, offsetBy: 1)
let end = field.index(field.endIndex, offsetBy: -1)
field = field[start..<end]
}
if quotationCount >= 4 {
field = field.stringByReplacingOccurrencesOfString("\"\"", withString: "\"")
field = field.replacingOccurrences(of: "\"\"", with: "\"")
}
return field

View File

@ -8,7 +8,7 @@
import Foundation
public enum CSVError: ErrorType {
public enum CSVError: ErrorProtocol {
case ParameterError
case StreamError
case HeaderReadError