update code to Swift 4.1

This commit is contained in:
Yonas Kolb
2018-09-10 20:59:02 +10:00
parent adb443229d
commit 420c0eacd7
7 changed files with 18 additions and 18 deletions

View File

@@ -23,7 +23,7 @@ class ForNode : NodeType {
throw TemplateSyntaxError("'for' statements should use the syntax: `for <x> in <y> [where <condition>]`.")
}
let loopVariables = components[1].characters
let loopVariables = components[1]
.split(separator: ",")
.map(String.init)
.map { $0.trim(character: " ") }

View File

@@ -64,7 +64,7 @@ class ExtendsNode : NodeType {
throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")
}
let blockNodes = parsedNodes.flatMap { $0 as? BlockNode }
let blockNodes = parsedNodes.compactMap { $0 as? BlockNode }
let nodes = blockNodes.reduce([String: BlockNode]()) { (accumulator, node) -> [String: BlockNode] in
var dict = accumulator
@@ -159,8 +159,8 @@ class BlockNode : NodeType {
}
// child node is a block node from child template that extends this node (has the same name)
func childContext(_ child: BlockNode, blockContext: BlockContext, context: Context) throws -> [String: Any?] {
var childContext: [String: Any?] = [BlockContext.contextKey: blockContext]
func childContext(_ child: BlockNode, blockContext: BlockContext, context: Context) throws -> [String: Any] {
var childContext: [String: Any] = [BlockContext.contextKey: blockContext]
if let blockSuperNode = child.nodes.first(where: {
if case .variable(let variable, _)? = $0.token, variable == "block.super" { return true }

View File

@@ -24,7 +24,7 @@ final class KeyPath {
subscriptLevel = 0
}
for c in variable.characters {
for c in variable {
switch c {
case "." where subscriptLevel == 0:
try foundSeparator()

View File

@@ -108,7 +108,7 @@ public class TokenParser {
let filtersWithDistance = allFilters
.map({ (filterName: $0, distance: $0.levenshteinDistance(name)) })
// do not suggest filters which names are shorter than the distance
.filter({ $0.filterName.characters.count > $0.distance })
.filter({ $0.filterName.count > $0.distance })
guard let minDistance = filtersWithDistance.min(by: { $0.distance < $1.distance })?.distance else {
return []
}
@@ -167,10 +167,10 @@ extension String {
// initialize v0 (the previous row of distances)
// this row is A[0][i]: edit distance for an empty s
// the distance is just the number of characters to delete from t
last = [Int](0...target.characters.count)
current = [Int](repeating: 0, count: target.characters.count + 1)
last = [Int](0...target.count)
current = [Int](repeating: 0, count: target.count + 1)
for i in 0..<self.characters.count {
for i in 0..<self.count {
// calculate v1 (current row distances) from the previous row v0
// first element of v1 is A[i+1][0]
@@ -178,7 +178,7 @@ extension String {
current[0] = i + 1
// use formula to fill in the rest of the row
for j in 0..<target.characters.count {
for j in 0..<target.count {
current[j+1] = Swift.min(
last[j+1] + 1,
current[j] + 1,
@@ -190,7 +190,7 @@ extension String {
last = current
}
return current[target.characters.count]
return current[target.count]
}
}

View File

@@ -13,7 +13,7 @@ extension String {
let specialCharacters = ",|:"
func appendWord(_ word: String) {
if components.count > 0 {
if let precedingChar = components.last?.characters.last, specialCharacters.characters.contains(precedingChar) {
if let precedingChar = components.last?.last, specialCharacters.contains(precedingChar) {
components[components.count-1] += word
} else if specialCharacters.contains(word) {
components[components.count-1] += word
@@ -25,7 +25,7 @@ extension String {
}
}
for character in self.characters {
for character in self {
if character == "'" { singleQuoteCount += 1 }
else if character == "\"" { doubleQuoteCount += 1 }

View File

@@ -9,7 +9,7 @@ class FilterExpression : Resolvable {
let variable: Variable
init(token: String, parser: TokenParser) throws {
let bits = token.characters.split(separator: "|").map({ String($0).trim(character: " ") })
let bits = token.split(separator: "|").map({ String($0).trim(character: " ") })
if bits.isEmpty {
throw TemplateSyntaxError("Variable tags must include at least 1 argument")
}
@@ -60,7 +60,7 @@ public struct Variable : Equatable, Resolvable {
if (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) {
// String literal
return String(variable[variable.characters.index(after: variable.startIndex) ..< variable.characters.index(before: variable.endIndex)])
return String(variable[variable.index(after: variable.startIndex) ..< variable.index(before: variable.endIndex)])
}
// Number literal

View File

@@ -189,7 +189,7 @@ func testForNode() {
let template = Template(templateString: templateString)
let result = try template.render(context)
let sortedResult = result.characters.split(separator: ",").map(String.init).sorted(by: <)
let sortedResult = result.split(separator: ",").map(String.init).sorted(by: <)
try expect(sortedResult) == ["one: I", "two: II"]
}
@@ -202,7 +202,7 @@ func testForNode() {
let node = ForNode(resolvable: Variable("dict"), loopVariables: ["key"], nodes: nodes, emptyNodes: emptyNodes, where: nil)
let result = try node.render(context)
let sortedResult = result.characters.split(separator: ",").map(String.init).sorted(by: <)
let sortedResult = result.split(separator: ",").map(String.init).sorted(by: <)
try expect(sortedResult) == ["one", "two"]
}
@@ -218,7 +218,7 @@ func testForNode() {
let result = try node.render(context)
let sortedResult = result.characters.split(separator: ",").map(String.init).sorted(by: <)
let sortedResult = result.split(separator: ",").map(String.init).sorted(by: <)
try expect(sortedResult) == ["one=I", "two=II"]
}