Skip to content

Commit 334e9de

Browse files
authored
Swift 4 (#36)
Support Swift 4
1 parent f1266d2 commit 334e9de

File tree

5 files changed

+83
-17
lines changed

5 files changed

+83
-17
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ matrix:
2222
- os: linux
2323
dist: trusty
2424
sudo: required
25-
env: SWIFT_SNAPSHOT=4.0-DEVELOPMENT-SNAPSHOT-2017-09-04-a
25+
env: SWIFT_SNAPSHOT=4.0-DEVELOPMENT-SNAPSHOT-2017-09-10-a
2626
- os: osx
2727
osx_image: xcode8.3
2828
sudo: required
2929
- os: osx
3030
osx_image: xcode9
3131
sudo: required
32-
env: SWIFT_SNAPSHOT=4.0-DEVELOPMENT-SNAPSHOT-2017-09-04-a
32+
env: SWIFT_SNAPSHOT=4.0-DEVELOPMENT-SNAPSHOT-2017-09-10-a
3333

3434
before_install:
3535
- git clone https://github.com/IBM-Swift/Package-Builder.git

[email protected]

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// swift-tools-version:4.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
/**
5+
* Copyright IBM Corporation 2016, 2017
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
**/
19+
20+
import PackageDescription
21+
22+
let package = Package(
23+
name: "HTMLEntities",
24+
products: [
25+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
26+
.library(
27+
name: "HTMLEntities",
28+
targets: ["HTMLEntities"]
29+
)
30+
],
31+
targets: [
32+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
33+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
34+
.target(
35+
name: "HTMLEntities"
36+
),
37+
.testTarget(
38+
name: "HTMLEntitiesTests",
39+
dependencies: ["HTMLEntities"]
40+
)
41+
]
42+
)

Sources/HTMLEntities/String+HTMLEntities.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,12 @@ fileprivate func decode(entity: String, entityPrefix: String, strict: Bool) thro
462462
}
463463

464464
let upperIndex = entity.index(entity.startIndex, offsetBy: length)
465-
let reference = entity[entity.startIndex..<upperIndex]
465+
466+
#if swift(>=3.2)
467+
let reference = String(entity[..<upperIndex])
468+
#else
469+
let reference = entity[entity.startIndex..<upperIndex]
470+
#endif
466471

467472
if let c = legacyNamedCharactersDecodeMap[reference] {
468473
if strict {

Tests/HTMLEntitiesTests/LinuxSafeguardTest.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
//
2121
// Code adapted from https://oleb.net/blog/2017/03/keeping-xctest-in-sync/
2222

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

2628
class LinuxSafeguardTest: XCTestCase {

Tests/LinuxMain.swift

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,37 @@ import XCTest
1919
@testable import HTMLEntitiesTests
2020

2121
// Implementation taken from http://stackoverflow.com/a/24029847
22-
extension MutableCollection where Indices.Iterator.Element == Index {
23-
mutating func shuffle() {
24-
let c = count
25-
guard c > 1 else { return }
22+
#if swift(>=3.2)
23+
extension MutableCollection {
24+
mutating func shuffle() {
25+
let c = count
26+
guard c > 1 else { return }
2627

27-
srand(UInt32(time(nil)))
28-
for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
29-
let d: IndexDistance = numericCast(random() % numericCast(unshuffledCount))
30-
guard d != 0 else { continue }
31-
let i = index(firstUnshuffled, offsetBy: d)
32-
swap(&self[firstUnshuffled], &self[i])
28+
srand(UInt32(time(nil)))
29+
for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
30+
let d: IndexDistance = numericCast(random() % numericCast(unshuffledCount))
31+
guard d != 0 else { continue }
32+
let i = index(firstUnshuffled, offsetBy: d)
33+
swapAt(firstUnshuffled, i)
34+
}
3335
}
3436
}
35-
}
37+
#else
38+
extension MutableCollection where Indices.Iterator.Element == Index {
39+
mutating func shuffle() {
40+
let c = count
41+
guard c > 1 else { return }
42+
43+
srand(UInt32(time(nil)))
44+
for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
45+
let d: IndexDistance = numericCast(random() % numericCast(unshuffledCount))
46+
guard d != 0 else { continue }
47+
let i = index(firstUnshuffled, offsetBy: d)
48+
swap(&self[firstUnshuffled], &self[i])
49+
}
50+
}
51+
}
52+
#endif
3653

3754
extension Sequence {
3855
func shuffled() -> [Iterator.Element] {
@@ -43,5 +60,5 @@ extension Sequence {
4360
}
4461

4562
XCTMain([
46-
testCase(HTMLEntitiesTests.allTests.shuffled()),
47-
].shuffled())
63+
testCase(HTMLEntitiesTests.allTests.shuffled()),
64+
].shuffled())

0 commit comments

Comments
 (0)