Skip to content

Commit bea4c98

Browse files
committed
Replace Foundation String format with Swift version
1 parent 52607b9 commit bea4c98

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

SQLite Tests/ValueTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import XCTest
2+
import SQLite
3+
4+
class ValueTests: SQLiteTestCase {
5+
6+
func test_blob_toHex() {
7+
let blob = Blob(
8+
data:[0,10,20,30,40,50,60,70,80,90,100,150,250,255] as [UInt8]
9+
)
10+
XCTAssertEqual(blob.toHex(), "000a141e28323c46505a6496faff")
11+
}
12+
13+
}

SQLite.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
8E7D32B61B8FB67C003C1892 /* ValueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E7D32B51B8FB67C003C1892 /* ValueTests.swift */; };
1011
DC109CE11A0C4D970070988E /* Schema.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC109CE01A0C4D970070988E /* Schema.swift */; };
1112
DC109CE41A0C4F5D0070988E /* SchemaTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC109CE31A0C4F5D0070988E /* SchemaTests.swift */; };
1213
DC2393C81ABE35F8003FF113 /* SQLite-Bridging.h in Headers */ = {isa = PBXBuildFile; fileRef = DC2393C61ABE35F8003FF113 /* SQLite-Bridging.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -99,6 +100,7 @@
99100
/* End PBXCopyFilesBuildPhase section */
100101

101102
/* Begin PBXFileReference section */
103+
8E7D32B51B8FB67C003C1892 /* ValueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ValueTests.swift; sourceTree = "<group>"; };
102104
DC109CE01A0C4D970070988E /* Schema.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Schema.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
103105
DC109CE31A0C4F5D0070988E /* SchemaTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SchemaTests.swift; sourceTree = "<group>"; };
104106
DC2393C61ABE35F8003FF113 /* SQLite-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "SQLite-Bridging.h"; sourceTree = "<group>"; };
@@ -199,6 +201,7 @@
199201
DC109CE31A0C4F5D0070988E /* SchemaTests.swift */,
200202
DCAFEAD61AABEFA7000C21A1 /* FTSTests.swift */,
201203
DCBE28441ABDF2A80042A3FC /* RTreeTests.swift */,
204+
8E7D32B51B8FB67C003C1892 /* ValueTests.swift */,
202205
DC37740319C8CBB3004FCF85 /* Supporting Files */,
203206
);
204207
path = "SQLite Tests";
@@ -505,6 +508,7 @@
505508
DC475EA219F219AF00788FBD /* ExpressionTests.swift in Sources */,
506509
DCAD429A19E2EE50004A51DF /* QueryTests.swift in Sources */,
507510
DC109CE41A0C4F5D0070988E /* SchemaTests.swift in Sources */,
511+
8E7D32B61B8FB67C003C1892 /* ValueTests.swift in Sources */,
508512
DCC6B3A71A91974B00734B78 /* FunctionsTests.swift in Sources */,
509513
DCBE28451ABDF2A80042A3FC /* RTreeTests.swift in Sources */,
510514
);

SQLite/Value.swift

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public protocol Value {
4545

4646
}
4747

48-
public struct Blob: Equatable {
48+
private let hexChars: [Character] = Array("0123456789abcdef")
49+
50+
public struct Blob: Equatable, Printable {
4951

5052
public let data: [UInt8]
5153

@@ -59,13 +61,30 @@ public struct Blob: Equatable {
5961
count: length
6062
))
6163
}
64+
65+
public func toHex() -> String {
66+
var string: String = ""
67+
string.reserveCapacity(data.count*2)
68+
for byte in data {
69+
let a = hexChars[Int(byte >> 4)]
70+
let b = hexChars[Int(byte & 0xF)]
71+
string.append(a)
72+
string.append(b)
73+
}
74+
return string
75+
}
76+
77+
public var description: String {
78+
return "x'\(toHex())'"
79+
}
6280

6381
}
6482

6583
public func ==(lhs: Blob, rhs: Blob) -> Bool {
6684
return lhs.data == rhs.data
6785
}
6886

87+
6988
extension Blob {
7089
public var bytes: UnsafePointer<Void> {
7190
return UnsafePointer<Void>(data)
@@ -90,16 +109,6 @@ extension Blob: Binding, Value {
90109

91110
}
92111

93-
extension Blob: Printable {
94-
95-
public var description: String {
96-
let buf = UnsafeBufferPointer(start: UnsafePointer<UInt8>(bytes), count: length)
97-
let hex = join("", map(buf) { String(format: "%02x", $0) })
98-
return "x'\(hex)'"
99-
}
100-
101-
}
102-
103112
extension Double: Number, Value {
104113

105114
public static var declaredDatatype = "REAL"

0 commit comments

Comments
 (0)