Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit c64cdcc

Browse files
committed
updated for Swift 3.0
swift-DEVELOPMENT-SNAPSHOT-2016-05-03
1 parent f826431 commit c64cdcc

6 files changed

Lines changed: 60 additions & 27 deletions

File tree

Sources/FoundationUnitTests/NSFileManagerTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class FileManagerTests: XCTestCase {
2626

2727
func testGetCurrentDirectory() {
2828

29-
XCTAssert(FileManager.currentDirectory == NSFileManager.defaultManager().currentDirectoryPath)
29+
XCTAssert(FileManager.currentDirectory == NSFileManager.default().currentDirectoryPath)
3030
}
3131

3232
func testFileExists() {
@@ -35,7 +35,7 @@ class FileManagerTests: XCTestCase {
3535

3636
let path = NSTemporaryDirectory() + "/" + fileName
3737

38-
XCTAssert(NSFileManager.defaultManager().createFile(atPath: path, contents: NSData(), attributes: nil))
38+
XCTAssert(NSFileManager.default().createFile(atPath: path, contents: NSData(), attributes: nil))
3939

4040
XCTAssert(FileManager.fileExists(at: path))
4141

@@ -44,9 +44,9 @@ class FileManagerTests: XCTestCase {
4444

4545
func testDirectoryExists() {
4646

47-
let path = try! NSFileManager.defaultManager().urlForDirectory( NSSearchPathDirectory.userDirectory, in: NSSearchPathDomainMask.allDomainsMask, appropriateFor: nil, create: false).path!
47+
let path = try! NSFileManager.default().urlForDirectory( NSSearchPathDirectory.userDirectory, in: NSSearchPathDomainMask.allDomainsMask, appropriateFor: nil, create: false).path!
4848

49-
assert(NSFileManager.defaultManager().fileExists(atPath: path, isDirectory: nil),
49+
assert(NSFileManager.default().fileExists(atPath: path, isDirectory: nil),
5050
"Setting non existent directory as test parameter")
5151

5252
XCTAssert(FileManager.directoryExists(at: path))
@@ -66,7 +66,7 @@ class FileManagerTests: XCTestCase {
6666

6767
let path = NSTemporaryDirectory() + "/" + fileName + ".txt"
6868

69-
XCTAssert(NSFileManager.defaultManager().createFile(atPath: path, contents: data.toFoundation(), attributes: nil))
69+
XCTAssert(NSFileManager.default().createFile(atPath: path, contents: data.toFoundation(), attributes: nil))
7070

7171
// read file
7272

@@ -92,7 +92,7 @@ class FileManagerTests: XCTestCase {
9292
let path = NSTemporaryDirectory() + fileName + ".txt"
9393

9494
// create empty file
95-
XCTAssert(NSFileManager.defaultManager().createFile(atPath: path, contents: NSData(), attributes: nil))
95+
XCTAssert(NSFileManager.default().createFile(atPath: path, contents: NSData(), attributes: nil))
9696

9797
// write file
9898
do { try FileManager.set(contents: data, at: path) }
@@ -126,9 +126,9 @@ class FileManagerTests: XCTestCase {
126126

127127
// read data
128128

129-
XCTAssert(NSFileManager.defaultManager().fileExists(atPath: path))
129+
XCTAssert(NSFileManager.default().fileExists(atPath: path))
130130

131-
NSFileManager.defaultManager()
131+
NSFileManager.default()
132132

133133
guard let readData = NSData(contentsOfFile: path)
134134
else { XCTFail("Could not read data"); return }

Sources/SwiftFoundation/POSIXRegularExpression.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public extension POSIXRegularExpression {
8282

8383
let range = Int(subexpressionMatch.rm_so) ..< Int(subexpressionMatch.rm_eo)
8484

85-
match.subexpressionRanges.append(RegularExpressionMatch.Range.Found(range))
85+
match.subexpressionRanges.append(RegularExpressionMatch.Range.Found(Range(range)))
8686
}
8787
}
8888

Sources/SwiftFoundation/Range.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
// Copyright © 2016 PureSwift. All rights reserved.
77
//
88

9-
public extension Range where Element: Integer {
9+
public extension Range where Bound: Integer {
1010

1111
func isSubset(_ other: Range) -> Bool {
1212

13-
return self.startIndex >= other.startIndex
14-
&& self.startIndex <= other.endIndex
15-
&& self.endIndex >= other.startIndex
16-
&& self.endIndex <= other.endIndex
13+
return self.lowerBound >= other.lowerBound
14+
&& self.lowerBound <= other.upperBound
15+
&& self.upperBound >= other.lowerBound
16+
&& self.upperBound <= other.upperBound
1717
}
1818
}

Sources/SwiftFoundation/RegularExpression.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// RegularExpression.swift
33
// SwiftFoundation
44
//
@@ -135,6 +135,7 @@ public struct RegularExpressionMatch {
135135
}
136136

137137
public extension String {
138+
138139
func substring(range: RegularExpressionMatch.Range) -> String? {
139140
switch range {
140141
case .NotFound:
@@ -143,9 +144,4 @@ public extension String {
143144
return substring(range: r)
144145
}
145146
}
146-
147-
func substring(range: Range<Int>) -> String? {
148-
let indexRange = utf8.startIndex.advanced(by: range.startIndex) ..< utf8.startIndex.advanced(by: range.endIndex)
149-
return String(utf8[indexRange])
150-
}
151147
}

Sources/SwiftFoundation/String.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ public extension String {
4646

4747
return Data(byteValue: [] + utf8)
4848
}
49+
50+
func substring(range: Range<Int>) -> String? {
51+
let indexRange = utf8.index(utf8.startIndex, offsetBy: range.lowerBound) ..< utf8.index(utf8.startIndex, offsetBy: range.upperBound)
52+
return String(utf8[indexRange])
53+
}
4954
}

Sources/UnitTests/RangeTests.swift

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,46 @@ final class RangeTests: XCTestCase {
1515

1616
func testSubset() {
1717

18-
let superset = 1 ... 10
18+
let superset = Range(1 ... 10)
1919

20-
XCTAssert((1...10).isSubset(superset))
21-
XCTAssert((1...9).isSubset(superset))
22-
XCTAssert((2...10).isSubset(superset))
23-
XCTAssert((2...9).isSubset(superset))
20+
XCTAssert(Range(1...10).isSubset(superset))
21+
XCTAssert(Range(1...9).isSubset(superset))
22+
XCTAssert(Range(2...10).isSubset(superset))
23+
XCTAssert(Range(2...9).isSubset(superset))
2424

25-
XCTAssert((0...10).isSubset(superset) == false)
26-
XCTAssert((0...11).isSubset(superset) == false)
25+
XCTAssert((Range(0...10).isSubset(superset)) == false)
26+
XCTAssert((Range(0...11).isSubset(superset)) == false)
27+
28+
29+
}
30+
31+
func testSubsetContainsElement() {
32+
33+
let superset = Range(1 ... 10)
34+
35+
XCTAssert(Range(1...10).verifyElements(of: superset))
36+
XCTAssert(Range(1...9).verifyElements(of: superset))
37+
XCTAssert(Range(2...10).verifyElements(of: superset))
38+
XCTAssert(Range(2...9).verifyElements(of: superset))
39+
40+
XCTAssert((Range(0...10).verifyElements(of: superset)) == false)
41+
XCTAssert((Range(0...11).verifyElements(of: superset)) == false)
2742
}
2843
}
44+
45+
private extension Range where Bound: Integer {
46+
47+
func verifyElements(of superset: Range<Int>) -> Bool {
48+
49+
/// dont try this at home kids
50+
let range = unsafeBitCast(self, to: Range<Int>.self)
51+
52+
for element in range.lowerBound ..< range.upperBound {
53+
54+
guard superset.contains(element)
55+
else { return false }
56+
}
57+
58+
return true
59+
}
60+
}

0 commit comments

Comments
 (0)