From 312fd9a6f126be1befd8373a263669491b373188 Mon Sep 17 00:00:00 2001 From: Quan Vo Date: Mon, 25 Sep 2017 15:54:45 -0500 Subject: [PATCH] Add Swift 4 support (#20) Add Swift 4 support --- Package@swift-4.swift | 47 +++++++++++++++++++ Tests/KituraWebSocketTests/PrintLogger.swift | 6 ++- .../TestLinuxSafeguard.swift | 2 +- Tests/LinuxMain.swift | 42 ++++++++++++----- 4 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 Package@swift-4.swift diff --git a/Package@swift-4.swift b/Package@swift-4.swift new file mode 100644 index 0000000..d036116 --- /dev/null +++ b/Package@swift-4.swift @@ -0,0 +1,47 @@ +// swift-tools-version:4.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +/** + * Copyright IBM Corporation 2016, 2017 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +import PackageDescription + +let package = Package( + name: "Kitura-WebSocket", + products: [ + // Products define the executables and libraries produced by a package, and make them visible to other packages. + .library( + name: "Kitura-WebSocket", + targets: ["KituraWebSocket"]), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + .package(url: "https://github.com/IBM-Swift/Kitura-net.git", .upToNextMinor(from: "1.7.0")), + .package(url: "https://github.com/IBM-Swift/BlueCryptor.git", .upToNextMinor(from: "0.8.0")), + + ], + targets: [ + // Targets are the basic building blocks of a package. A target defines a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages which this package depends on. + .target( + name: "KituraWebSocket", + dependencies: ["KituraNet", "Cryptor"]), + .testTarget( + name: "KituraWebSocketTests", + dependencies: ["KituraWebSocket"]), + ] +) diff --git a/Tests/KituraWebSocketTests/PrintLogger.swift b/Tests/KituraWebSocketTests/PrintLogger.swift index 6634a62..abd47c2 100644 --- a/Tests/KituraWebSocketTests/PrintLogger.swift +++ b/Tests/KituraWebSocketTests/PrintLogger.swift @@ -74,6 +74,10 @@ public class PrintLogger: Logger { guard let range = path.range(of: "/", options: .backwards) else { return path } - return path.substring(from: range.upperBound) + #if swift(>=3.2) + return String(path[range.upperBound...]) + #else + return path.substring(from: range.upperBound) + #endif } } diff --git a/Tests/KituraWebSocketTests/TestLinuxSafeguard.swift b/Tests/KituraWebSocketTests/TestLinuxSafeguard.swift index c1ac048..bdb9042 100644 --- a/Tests/KituraWebSocketTests/TestLinuxSafeguard.swift +++ b/Tests/KituraWebSocketTests/TestLinuxSafeguard.swift @@ -14,7 +14,7 @@ * limitations under the License. **/ -#if os(OSX) +#if os(OSX) && !swift(>=3.2) import XCTest class TestLinuxSafeguard: XCTestCase { diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 0ea8615..5d8733f 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -16,24 +16,40 @@ import XCTest import Glibc - @testable import KituraWebSocketTests // http://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift -extension MutableCollection where Indices.Iterator.Element == Index { - mutating func shuffle() { - let c = count - guard c > 1 else { return } - - srand(UInt32(time(nil))) - for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) { - let d: IndexDistance = numericCast(random() % numericCast(unshuffledCount)) - guard d != 0 else { continue } - let i = index(firstUnshuffled, offsetBy: d) - swap(&self[firstUnshuffled], &self[i]) +#if swift(>=3.2) + extension MutableCollection { + mutating func shuffle() { + let c = count + guard c > 1 else { return } + + srand(UInt32(time(nil))) + for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) { + let d: IndexDistance = numericCast(random() % numericCast(unshuffledCount)) + guard d != 0 else { continue } + let i = index(firstUnshuffled, offsetBy: d) + swapAt(firstUnshuffled, i) + } } } -} +#else + extension MutableCollection where Indices.Iterator.Element == Index { + mutating func shuffle() { + let c = count + guard c > 1 else { return } + + srand(UInt32(time(nil))) + for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) { + let d: IndexDistance = numericCast(random() % numericCast(unshuffledCount)) + guard d != 0 else { continue } + let i = index(firstUnshuffled, offsetBy: d) + swap(&self[firstUnshuffled], &self[i]) + } + } + } +#endif extension Sequence { func shuffled() -> [Iterator.Element] {