Skip to content

Commit

Permalink
Update to Swift 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
CodaFi committed Sep 20, 2017
1 parent cb3d355 commit 4ff161a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 47 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ matrix:
include:
- os: osx
language: objective-c
osx_image: xcode8.3
osx_image: xcode9
script:
- swift test
- os: linux
Expand All @@ -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:
Expand Down
18 changes: 17 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -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"]),
]
)
18 changes: 9 additions & 9 deletions Sources/FileCheck/CheckString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String.Index>(
let skippedRegion = String(buffer[
Range<String.Index>(
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).
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String.Index>(
let skippedRegion = String(buffer[
Range<String.Index>(
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
Expand Down
12 changes: 6 additions & 6 deletions Sources/FileCheck/FileCheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ private func findFirstMatch(in inbuffer : UnsafeBufferPointer<CChar>, 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
Expand Down Expand Up @@ -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[..<buffer.index(buffer.startIndex, offsetBy: NSMaxRange(range))])
buffer = String(buffer[buffer.index(buffer.startIndex, offsetBy: NSMaxRange(range))...])
j += 1
}

Expand All @@ -496,7 +496,7 @@ private func check(input b : String, against checkStrings : [CheckString], optio
break
}
variableTable = mutVarTable
checkRegion = checkRegion.substring(from: checkRegion.index(checkRegion.startIndex, offsetBy: NSMaxRange(range)))
checkRegion = String(checkRegion[checkRegion.index(checkRegion.startIndex, offsetBy: NSMaxRange(range))...])
}

if j == checkStrings.count {
Expand Down
50 changes: 23 additions & 27 deletions Sources/FileCheck/Pattern.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ final class Pattern {
// RegEx matches.
if patternStr.range(of: "{{")?.lowerBound == patternStr.startIndex {
// This is the start of a regex match. Scan for the }}.
patternStr = patternStr.substring(from: patternStr.index(patternStr.startIndex, offsetBy: 2))
patternStr = String(patternStr[patternStr.index(patternStr.startIndex, offsetBy: 2)...])
guard let end = self.findRegexVarEnd(patternStr, brackets: (open: "{", close: "}"), terminator: "}}") else {
let loc = CheckLoc.inBuffer(pattern.baseAddress!, buf)
diagnose(.error, at: loc, with: "found start of regex string with no end '}}'", options: options)
Expand All @@ -126,15 +126,15 @@ final class Pattern {
regExPattern += "("
curParen += 1

let substr = patternStr.substring(to: end)
let substr = patternStr[..<end]
let (res, paren) = self.addRegExToRegEx(substr, curParen)
curParen = paren
if res {
return nil
}
regExPattern += ")"

patternStr = patternStr.substring(from: patternStr.index(end, offsetBy: 2))
patternStr = String(patternStr[patternStr.index(end, offsetBy: 2)...])
continue
}

Expand All @@ -146,23 +146,23 @@ final class Pattern {
if patternStr.hasPrefix("[[") {
// Find the closing bracket pair ending the match. End is going to be an
// offset relative to the beginning of the match string.
let regVar = patternStr.substring(from: patternStr.index(patternStr.startIndex, offsetBy: 2))
let regVar = String(patternStr[patternStr.index(patternStr.startIndex, offsetBy: 2)...])
guard let end = self.findRegexVarEnd(regVar, brackets: (open: "[", close: "]"), terminator: "]]") else {
let loc = CheckLoc.inBuffer(pattern.baseAddress!, buf)
diagnose(.error, at: loc, with: "invalid named regex reference, no ]] found", options: options)
return nil
}

let matchStr = regVar.substring(to: end)
patternStr = patternStr.substring(from: patternStr.index(end, offsetBy: 4))
let matchStr = regVar[..<end]
patternStr = String(patternStr[patternStr.index(end, offsetBy: 4)...])

// Get the regex name (e.g. "foo").
let nameEnd = matchStr.range(of: ":")
let name : String
if let end = nameEnd?.lowerBound {
name = matchStr.substring(to: end)
name = String(matchStr[..<end])
} else {
name = matchStr
name = String(matchStr)
}

if name.isEmpty {
Expand Down Expand Up @@ -218,7 +218,7 @@ final class Pattern {
self.regExPattern += "("
curParen += 1

let (res, paren) = self.addRegExToRegEx(matchStr.substring(from: matchStr.index(after: ne.lowerBound)), curParen)
let (res, paren) = self.addRegExToRegEx(matchStr[matchStr.index(after: ne.lowerBound)...], curParen)
curParen = paren
if res {
return nil
Expand All @@ -230,8 +230,8 @@ final class Pattern {
// Handle fixed string matches.
// Find the end, which is the start of the next regex.
if let fixedMatchEnd = mino(patternStr.range(of: "{{")?.lowerBound, patternStr.range(of: "[[")?.lowerBound) {
self.regExPattern += NSRegularExpression.escapedPattern(for: patternStr.substring(to: fixedMatchEnd))
patternStr = patternStr.substring(from: fixedMatchEnd)
self.regExPattern += NSRegularExpression.escapedPattern(for: String(patternStr[..<fixedMatchEnd]))
patternStr = String(patternStr[fixedMatchEnd...])
} else {
// No more matches, time to quit.
self.regExPattern += NSRegularExpression.escapedPattern(for: patternStr)
Expand Down Expand Up @@ -260,13 +260,13 @@ final class Pattern {
if !expr.hasPrefix("@LINE") {
return nil
}
expr = expr.substring(from: expr.index(expr.startIndex, offsetBy: "@LINE".utf8.count))
expr = String(expr[expr.index(expr.startIndex, offsetBy: "@LINE".utf8.count)...])
guard let firstC = expr.characters.first else {
return "\(self.lineNumber)"
}

if firstC == "+" {
expr = expr.substring(from: expr.index(after: expr.startIndex))
expr = String(expr[expr.index(after: expr.startIndex)...])
} else if firstC != "-" {
return nil
}
Expand Down Expand Up @@ -350,19 +350,15 @@ final class Pattern {
var mutTable = variableTable
for (v, index) in self.variableDefs {
assert(index < fullMatch.numberOfRanges, "Internal paren error")
#if os(macOS)
let r = fullMatch.rangeAt(index)
#else
let r = fullMatch.range(at: index)
#endif
mutTable[v] = buffer.substring(
with: Range<String.Index>(
let r = fullMatch.range(at: index)
mutTable[v] = String(buffer[
Range<String.Index>(
uncheckedBounds: (
buffer.index(buffer.startIndex, offsetBy: r.location),
buffer.index(buffer.startIndex, offsetBy: NSMaxRange(r))
)
)
)
])
}

return (fullMatch.range, mutTable)
Expand All @@ -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 {
Expand All @@ -405,17 +401,17 @@ 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)
}
}

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 {
Expand All @@ -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)
}
Expand All @@ -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
Expand Down

0 comments on commit 4ff161a

Please sign in to comment.