From 334e9de097fc4633aad1e5da51b531ee5f21e323 Mon Sep 17 00:00:00 2001 From: Youming Lin Date: Wed, 13 Sep 2017 13:22:49 -0500 Subject: [PATCH] Swift 4 (#36) Support Swift 4 --- .travis.yml | 4 +- Package@swift-4.0.swift | 42 ++++++++++++++++++ .../HTMLEntities/String+HTMLEntities.swift | 7 ++- .../LinuxSafeguardTest.swift | 4 +- Tests/LinuxMain.swift | 43 +++++++++++++------ 5 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 Package@swift-4.0.swift diff --git a/.travis.yml b/.travis.yml index d220896..e0aac5b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,14 +22,14 @@ matrix: - os: linux dist: trusty sudo: required - env: SWIFT_SNAPSHOT=4.0-DEVELOPMENT-SNAPSHOT-2017-09-04-a + env: SWIFT_SNAPSHOT=4.0-DEVELOPMENT-SNAPSHOT-2017-09-10-a - os: osx osx_image: xcode8.3 sudo: required - os: osx osx_image: xcode9 sudo: required - env: SWIFT_SNAPSHOT=4.0-DEVELOPMENT-SNAPSHOT-2017-09-04-a + env: SWIFT_SNAPSHOT=4.0-DEVELOPMENT-SNAPSHOT-2017-09-10-a before_install: - git clone https://github.com/IBM-Swift/Package-Builder.git diff --git a/Package@swift-4.0.swift b/Package@swift-4.0.swift new file mode 100644 index 0000000..c73ddd1 --- /dev/null +++ b/Package@swift-4.0.swift @@ -0,0 +1,42 @@ +// 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: "HTMLEntities", + products: [ + // Products define the executables and libraries produced by a package, and make them visible to other packages. + .library( + name: "HTMLEntities", + targets: ["HTMLEntities"] + ) + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define 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: "HTMLEntities" + ), + .testTarget( + name: "HTMLEntitiesTests", + dependencies: ["HTMLEntities"] + ) + ] +) diff --git a/Sources/HTMLEntities/String+HTMLEntities.swift b/Sources/HTMLEntities/String+HTMLEntities.swift index 867c0cf..a00351e 100644 --- a/Sources/HTMLEntities/String+HTMLEntities.swift +++ b/Sources/HTMLEntities/String+HTMLEntities.swift @@ -462,7 +462,12 @@ fileprivate func decode(entity: String, entityPrefix: String, strict: Bool) thro } let upperIndex = entity.index(entity.startIndex, offsetBy: length) - let reference = entity[entity.startIndex..=3.2) + let reference = String(entity[..=3.2) import XCTest class LinuxSafeguardTest: XCTestCase { diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 3642c12..0494510 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -19,20 +19,37 @@ import XCTest @testable import HTMLEntitiesTests // Implementation taken from http://stackoverflow.com/a/24029847 -extension MutableCollection where Indices.Iterator.Element == Index { - mutating func shuffle() { - let c = count - guard c > 1 else { return } +#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) - swap(&self[firstUnshuffled], &self[i]) + 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] { @@ -43,5 +60,5 @@ extension Sequence { } XCTMain([ - testCase(HTMLEntitiesTests.allTests.shuffled()), -].shuffled()) + testCase(HTMLEntitiesTests.allTests.shuffled()), + ].shuffled())