Skip to content

Commit 62fbf7c

Browse files
committed
Use Swift 2-isms where applicable
We should use guard-let wherever we have an early exit and can manage. Let the compiler enforce our requirements. Signed-off-by: Stephen Celis <stephen@stephencelis.com>
1 parent 832364f commit 62fbf7c

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

Source/Extensions/FTS4.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ extension Connection {
143143
public func registerTokenizer(submoduleName: String, next: String -> (String, Range<String.Index>)?) throws {
144144
try check(_SQLiteRegisterTokenizer(handle, Tokenizer.moduleName, submoduleName) { input, offset, length in
145145
let string = String.fromCString(input)!
146-
if let (token, range) = next(string) {
147-
let view = string.utf8
148-
offset.memory += string.substringToIndex(range.startIndex).utf8.count
149-
length.memory = Int32(range.startIndex.samePositionIn(view).distanceTo(range.endIndex.samePositionIn(view)))
150-
return token
151-
}
152-
return nil
146+
147+
guard let (token, range) = next(string) else { return nil }
148+
149+
let view = string.utf8
150+
offset.memory += string.substringToIndex(range.startIndex).utf8.count
151+
length.memory = Int32(range.startIndex.samePositionIn(view).distanceTo(range.endIndex.samePositionIn(view)))
152+
return token
153153
})
154154
}
155155

Source/Helpers.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ extension String {
101101
}
102102

103103
@warn_unused_result func transcode(literal: Binding?) -> String {
104-
if let literal = literal {
105-
if let literal = literal as? NSData {
106-
let buf = UnsafeBufferPointer(start: UnsafePointer<UInt8>(literal.bytes), count: literal.length)
107-
let hex = buf.map { String(format: "%02x", $0) }.joinWithSeparator("")
108-
return "x'\(hex)'"
109-
}
110-
if let literal = literal as? String { return literal.quote("'") }
111-
return "\(literal)"
104+
guard let literal = literal else { return "NULL" }
105+
106+
switch literal {
107+
case let blob as Blob:
108+
return blob.description
109+
case let string as String:
110+
return string.quote("'")
111+
case let binding:
112+
return "\(binding)"
112113
}
113-
return "NULL"
114114
}
115115

116116
@warn_unused_result func value<A: Value>(v: Binding) -> A {

Source/Typed/Query.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -989,19 +989,24 @@ public struct Row {
989989
}
990990
public func get<V: Value>(column: Expression<V?>) -> V? {
991991
func valueAtIndex(idx: Int) -> V? {
992-
if let value = values[idx] as? V.Datatype { return (V.fromDatatypeValue(value) as! V) }
993-
return nil
992+
guard let value = values[idx] as? V.Datatype else { return nil }
993+
return (V.fromDatatypeValue(value) as? V)!
994994
}
995995

996-
if let idx = columnNames[column.template] { return valueAtIndex(idx) }
997-
998-
let similar = Array(columnNames.keys).filter { $0.hasSuffix(".\(column.template)") }
999-
if similar.count == 1 { return valueAtIndex(columnNames[similar[0]]!) }
996+
guard let idx = columnNames[column.template] else {
997+
let similar = Array(columnNames.keys).filter { $0.hasSuffix(".\(column.template)") }
1000998

1001-
if similar.count > 1 {
1002-
fatalError("ambiguous column '\(column.template)' (please disambiguate: \(similar))")
999+
switch similar.count {
1000+
case 0:
1001+
fatalError("no such column '\(column.template)' in columns: \(columnNames.keys.sort())")
1002+
case 1:
1003+
return valueAtIndex(columnNames[similar[0]]!)
1004+
default:
1005+
fatalError("ambiguous column '\(column.template)' (please disambiguate: \(similar))")
1006+
}
10031007
}
1004-
fatalError("no such column '\(column.template)' in columns: \(columnNames.keys.sort())")
1008+
1009+
return valueAtIndex(idx)
10051010
}
10061011

10071012
// FIXME: rdar://problem/18673897 // subscript<T>…

0 commit comments

Comments
 (0)