Skip to content

Commit

Permalink
Swift 4 (#36)
Browse files Browse the repository at this point in the history
Support Swift 4
  • Loading branch information
youming-lin authored Sep 13, 2017
1 parent f1266d2 commit 334e9de
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -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"]
)
]
)
7 changes: 6 additions & 1 deletion Sources/HTMLEntities/String+HTMLEntities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<upperIndex]

#if swift(>=3.2)
let reference = String(entity[..<upperIndex])
#else
let reference = entity[entity.startIndex..<upperIndex]
#endif

if let c = legacyNamedCharactersDecodeMap[reference] {
if strict {
Expand Down
4 changes: 3 additions & 1 deletion Tests/HTMLEntitiesTests/LinuxSafeguardTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
//
// Code adapted from https://oleb.net/blog/2017/03/keeping-xctest-in-sync/

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
// Test disabled on Swift 4 for now due to
// https://bugs.swift.org/browse/SR-5684
#if os(OSX) && !swift(>=3.2)
import XCTest

class LinuxSafeguardTest: XCTestCase {
Expand Down
43 changes: 30 additions & 13 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -43,5 +60,5 @@ extension Sequence {
}

XCTMain([
testCase(HTMLEntitiesTests.allTests.shuffled()),
].shuffled())
testCase(HTMLEntitiesTests.allTests.shuffled()),
].shuffled())

0 comments on commit 334e9de

Please sign in to comment.