From 4ff161afd11eb81e13bcd9009d193f111454d232 Mon Sep 17 00:00:00 2001 From: Robert Widmann Date: Wed, 20 Sep 2017 13:02:11 -0400 Subject: [PATCH] Update to Swift 4.0 --- .travis.yml | 8 ++--- Package.swift | 18 ++++++++++- Sources/FileCheck/CheckString.swift | 18 +++++------ Sources/FileCheck/FileCheck.swift | 12 +++---- Sources/FileCheck/Pattern.swift | 50 +++++++++++++---------------- 5 files changed, 59 insertions(+), 47 deletions(-) diff --git a/.travis.yml b/.travis.yml index 825d7b5..f8afc66 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ matrix: include: - os: osx language: objective-c - osx_image: xcode8.3 + osx_image: xcode9 script: - swift test - os: linux @@ -15,9 +15,9 @@ matrix: env: before_install: - wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import - - - wget https://swift.org/builds/swift-3.1-release/ubuntu1404/swift-3.1-RELEASE/swift-3.1-RELEASE-ubuntu14.04.tar.gz - - tar xzf swift-3.1-RELEASE-ubuntu14.04.tar.gz - - export PATH=${PWD}/swift-3.1-RELEASE-ubuntu14.04/usr/bin:"${PATH}" + - wget https://swift.org/builds/swift-4.0-release/ubuntu1404/swift-4.0-RELEASE/swift-4.0-RELEASE-ubuntu14.04.tar.gz + - tar xzf swift-4.0-RELEASE-ubuntu14.04.tar.gz + - export PATH=${PWD}/swift-4.0-RELEASE-ubuntu14.04/usr/bin:"${PATH}" script: - swift test notifications: diff --git a/Package.swift b/Package.swift index 9394978..580eb3a 100644 --- a/Package.swift +++ b/Package.swift @@ -1,3 +1,19 @@ +// swift-tools-version:4.0 + import PackageDescription -let package = Package(name: "FileCheck") +let package = Package( + name: "FileCheck", + products: [ + .library( + name: "FileCheck", + targets: ["FileCheck"]), + ], + targets: [ + .target( + name: "FileCheck"), + .testTarget( + name: "FileCheckTests", + dependencies: ["FileCheck"]), + ] +) diff --git a/Sources/FileCheck/CheckString.swift b/Sources/FileCheck/CheckString.swift index e21d00b..93e47a3 100644 --- a/Sources/FileCheck/CheckString.swift +++ b/Sources/FileCheck/CheckString.swift @@ -78,7 +78,7 @@ struct CheckString { } // Match itself from the last position after matching CHECK-DAG. - let matchBuffer = buffer.substring(from: buffer.index(buffer.startIndex, offsetBy: lastPos)) + let matchBuffer = String(buffer[buffer.index(buffer.startIndex, offsetBy: lastPos)...]) guard let (range, mutVariableTable) = self.pattern.match(matchBuffer, initialTable) else { diagnoseFailedCheck(self.pattern, self.loc, self.prefix, variableTable, options, buffer) return nil @@ -90,15 +90,15 @@ struct CheckString { let finalTable : [String:String] if !isLabelScanMode { let startIdx = buffer.index(buffer.startIndex, offsetBy: lastPos) - let skippedRegion = buffer.substring( - with: Range( + let skippedRegion = String(buffer[ + Range( uncheckedBounds: ( startIdx, buffer.index(startIdx, offsetBy: matchPos) ) ) - ) - let rest = buffer.substring(from: buffer.index(buffer.startIndex, offsetBy: matchPos)) + ]) + let rest = String(buffer[buffer.index(buffer.startIndex, offsetBy: matchPos)...]) // If this check is a "CHECK-NEXT", verify that the previous match was on // the previous line (i.e. that there is one newline between them). @@ -247,7 +247,7 @@ struct CheckString { assert((pattern.type == .dag), "Expect CHECK-DAG!") // CHECK-DAG always matches from the start. - let matchBuffer = buffer.substring(from: buffer.index(buffer.startIndex, offsetBy: startPos)) + let matchBuffer = String(buffer[buffer.index(buffer.startIndex, offsetBy: startPos)...]) // With a group of CHECK-DAGs, a single mismatching means the match on // that group of CHECK-DAGs fails immediately. guard let (range, variableTable) = pattern.match(matchBuffer, finalTable) else { @@ -277,14 +277,14 @@ struct CheckString { // If there's CHECK-NOTs between two CHECK-DAGs or from CHECK to // CHECK-DAG, verify that there's no 'not' strings occurred in that // region. - let skippedRegion = buffer.substring( - with: Range( + let skippedRegion = String(buffer[ + Range( uncheckedBounds: ( buffer.index(buffer.startIndex, offsetBy: lastPos), buffer.index(buffer.startIndex, offsetBy: matchPos) ) ) - ) + ]) let (cond, mutVarTable) = self.checkNot(skippedRegion, notStrings, finalTable, options) if cond { return nil diff --git a/Sources/FileCheck/FileCheck.swift b/Sources/FileCheck/FileCheck.swift index 7223f57..a26c9f9 100644 --- a/Sources/FileCheck/FileCheck.swift +++ b/Sources/FileCheck/FileCheck.swift @@ -283,14 +283,14 @@ private func findFirstMatch(in inbuffer : UnsafeBufferPointer, among pref return ("", .none, lineNumber, buffer) } let skippedPrefix = substring(in: buffer, with: NSMakeRange(0, prefix.range.location)) - let prefixStr = str.substring( - with: Range( + let prefixStr = String(str[ + Range( uncheckedBounds: ( str.index(str.startIndex, offsetBy: prefix.range.location), str.index(str.startIndex, offsetBy: NSMaxRange(prefix.range)) ) ) - ) + ]) // HACK: Conversion between the buffer and `String` causes index // mismatches when searching for strings. We're instead going to do @@ -480,8 +480,8 @@ private func check(input b : String, against checkStrings : [CheckString], optio } variableTable = mutVariableTable - checkRegion = buffer.substring(to: buffer.index(buffer.startIndex, offsetBy: NSMaxRange(range))) - buffer = buffer.substring(from: buffer.index(buffer.startIndex, offsetBy: NSMaxRange(range))) + checkRegion = String(buffer[..( + let r = fullMatch.range(at: index) + mutTable[v] = String(buffer[ + Range( uncheckedBounds: ( buffer.index(buffer.startIndex, offsetBy: r.location), buffer.index(buffer.startIndex, offsetBy: NSMaxRange(r)) ) ) - ) + ]) } return (fullMatch.range, mutTable) @@ -386,7 +382,7 @@ final class Pattern { } if firstChar == "\\" { // Backslash escapes the next char within regexes, so skip them both. - string = string.substring(from: string.index(string.startIndex, offsetBy: 2)) + string = String(string[string.index(string.startIndex, offsetBy: 2)...]) offset = regVar.index(offset, offsetBy: 2) } else { switch firstChar { @@ -405,7 +401,7 @@ final class Pattern { default: break } - string = string.substring(from: string.index(after: string.startIndex)) + string = String(string[string.index(after: string.startIndex)...]) offset = regVar.index(after: offset) } } @@ -413,9 +409,9 @@ final class Pattern { return nil } - private func addRegExToRegEx(_ RS : String, _ cur : Int) -> (Bool, Int) { + private func addRegExToRegEx(_ RS : Substring, _ cur : Int) -> (Bool, Int) { do { - let r = try NSRegularExpression(pattern: RS, options: []) + let r = try NSRegularExpression(pattern: String(RS), options: []) self.regExPattern += RS return (false, cur + r.numberOfCaptureGroups) } catch let e { @@ -435,7 +431,7 @@ func countNumNewlinesBetween(_ r : String) -> (Int, String.Index?) { guard let EOL = range.range(of: "\n")?.lowerBound ?? range.range(of: "\r")?.lowerBound else { return (NumNewLines, firstNewLine) } - range = range.substring(from: EOL) + range = String(range[EOL...]) if range.isEmpty { return (NumNewLines, firstNewLine) } @@ -446,7 +442,7 @@ func countNumNewlinesBetween(_ r : String) -> (Int, String.Index?) { // if Range.utf8.count > 1 && (Range.utf8[1] == '\n' || Range[1] == '\r') && (Range[0] != Range[1]) { // Range = Range.substr(1) // } - range = range.substring(from: range.index(after: range.startIndex)) + range = String(range[range.index(after: range.startIndex)...]) if NumNewLines == 1 { firstNewLine = range.startIndex