Switch to Swift 5

This commit is contained in:
Milen Dzhumerov 2019-07-27 14:45:43 +01:00
parent 861f0cc605
commit c34dbe8b88
6 changed files with 35 additions and 15 deletions

View File

@ -49,5 +49,5 @@ let package = Package(
]
)
],
swiftLanguageVersions: [4]
swiftLanguageVersions: [5]
)

View File

@ -27,6 +27,11 @@ You can also use the `convert` command to create a binary header map from JSON:
You can discover all the commands and options by using `hmap --help`.
## Requirements
- hmap requires Swift 5.
- Starting from Xcode 10.2, Swift 5 command line tools require the Swift 5 runtime libraries which are included in macOS Majave 10.4.4. If you're running an earlier version of macOS, you need to install the "Swift 5 Runtime Support for Command Line Tools" available from [More Downloads for Apple Developers](https://developer.apple.com/download/more/).
# Building from Source
## Xcode
@ -47,8 +52,8 @@ If you would like to build from the command line, run:
To produce a release build suitable for distribution, run:
swift build -c release -Xswiftc -static-stdlib
swift build -c release
To verify that all tests pass, run:
swift test
swift test

View File

@ -185,10 +185,10 @@ extension BinaryHeaderMap {
guard preambleSize <= begin, begin < data.count else { return nil }
let nullByteIndex = data.withUnsafeBytes {
(bytes: UnsafePointer<UInt8>) -> Int? in
(bytes: UnsafeRawBufferPointer) -> Int? in
for i in begin..<data.count {
if bytes.advanced(by: i).pointee == 0x0 {
for i in begin..<bytes.count {
if bytes[i] == 0x0 {
return i
}
}
@ -211,8 +211,10 @@ extension BinaryHeaderMap {
return nil
}
return try data.withUnsafeBytes {
return try body($0.advanced(by: offset))
return try data.withUnsafeBytes { byteBuffer in
let basePointer = byteBuffer.baseAddress?.bindMemory(to: UInt8.self, capacity: byteBuffer.count)
let offsetPointer = basePointer?.advanced(by: offset)
return try offsetPointer.flatMap { try body($0) }
}
}
}
@ -252,9 +254,14 @@ func parseHeaderMap(data: Data) throws -> DataHeaderParseResult {
throw HeaderMapParseError.missingDataHeader
}
return try data.withUnsafeBytes { (headerBytes: UnsafePointer<UInt8>) in
return try data.withUnsafeBytes { (headerRawBytes: UnsafeRawBufferPointer) in
typealias H = BinaryHeaderMap.DataHeader
guard let baseRawPointer = headerRawBytes.baseAddress else {
throw HeaderMapParseError.missingDataHeader
}
let headerBytes = baseRawPointer.bindMemory(to: UInt8.self, capacity: headerRawBytes.count)
let decoder = ByteBufferDecoder(bytes: headerBytes, byteSwap: false)
let magicValue = decoder.advanceByteSwappable(

View File

@ -41,6 +41,7 @@ public enum HeaderMapCreateError: LocalizedError {
case stringWithoutOffsetInTable
case hashTableFull
case unhashableKey
case emptyDataBuffer
}
extension HeaderMapError {
@ -84,6 +85,8 @@ extension HeaderMapCreateError {
return "Header map is full"
case .unhashableKey:
return "Key cannot be hashed"
case .emptyDataBuffer:
return "Failed to allocate data buffer"
}
}
}

View File

@ -136,7 +136,13 @@ fileprivate func makeBucketSection(
let bucketCount = numberOfBuckets(forEntryCount: entries.count)
var bytes = Data(count: bucketCount * BinaryHeaderMap.Bucket.packedSize)
try bytes.withUnsafeMutableBytes {
(bytePointer: UnsafeMutablePointer<UInt8>) in
(rawBytes: UnsafeMutableRawBufferPointer) in
guard let rawBasePointer = rawBytes.baseAddress else {
throw HeaderMapCreateError.emptyDataBuffer
}
let bytePointer = rawBasePointer.bindMemory(to: UInt8.self, capacity: rawBytes.count)
try entries.forEach { (entry) in
guard let keyHash = entry.key.headerMapHash else {

View File

@ -29,7 +29,7 @@ extension String {
}
let lowercasedBytes = charBytes.map { $0.asciiLowercased() }
return Data(bytes: lowercasedBytes)
return Data(lowercasedBytes)
}
func clangLowercased() throws -> String? {
@ -42,12 +42,11 @@ extension String {
else { return nil }
return characterBytes.withUnsafeBytes {
(charBlockPointer: UnsafePointer<UInt8>) -> UInt32 in
(charBytes: UnsafeRawBufferPointer) -> UInt32 in
var result = UInt32(0)
for i in 0 ..< characterBytes.count {
let charPointer = charBlockPointer.advanced(by: i)
result += UInt32(charPointer.pointee) * 13
for i in 0 ..< charBytes.count {
result += UInt32(charBytes[i]) * 13
}
return result
}