Skip to content

Commit

Permalink
refactor: use explicit Swift 4 syntax (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Renne authored and kylef committed Mar 25, 2019
1 parent a2f89b8 commit 9f2a9da
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions Sources/URITemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ public struct URITemplate : CustomStringConvertible, Equatable, Hashable, Expres
let expressions = regex.matches(template).map { expression -> String in
// Removes the { and } from the expression
#if swift(>=4.0)
return String(expression[expression.characters.index(after: expression.startIndex)..<expression.characters.index(before: expression.endIndex)])
return String(expression[expression.index(after: expression.startIndex)..<expression.index(before: expression.endIndex)])
#elseif swift(>=3.2)
return expression.substring(with: expression.index(after: expression.startIndex)..<expression.index(before: expression.endIndex))
#else
return expression.substring(with: expression.characters.index(after: expression.startIndex)..<expression.characters.index(before: expression.endIndex))
return expression.substring(with: expression.characters.index(after: expression.startIndex)..<expression.characters.index(before: expression.endIndex))
#endif
}

Expand All @@ -90,7 +92,9 @@ public struct URITemplate : CustomStringConvertible, Equatable, Hashable, Expres
if let op = op.op {
if expression.hasPrefix(op) {
#if swift(>=4.0)
expression = String(expression[expression.characters.index(after: expression.startIndex)...])
expression = String(expression[expression.index(after: expression.startIndex)...])
#elseif swift(>=3.2)
expression = expression.substring(from: expression.index(after: expression.startIndex))
#else
expression = expression.substring(from: expression.characters.index(after: expression.startIndex))
#endif
Expand All @@ -102,9 +106,11 @@ public struct URITemplate : CustomStringConvertible, Equatable, Hashable, Expres
return expression.components(separatedBy: ",").map { component in
if component.hasSuffix("*") {
#if swift(>=4.0)
return String(component[..<component.characters.index(before: component.endIndex)])
return String(component[..<component.index(before: component.endIndex)])
#elseif swift(>=3.2)
return component.substring(to: component.index(before: component.endIndex))
#else
return component.substring(to: component.characters.index(before: component.endIndex))
return component.substring(to: component.characters.index(before: component.endIndex))
#endif
} else {
return component
Expand All @@ -117,11 +123,14 @@ public struct URITemplate : CustomStringConvertible, Equatable, Hashable, Expres
public func expand(_ variables: [String: Any]) -> String {
return regex.substitute(template) { string in
#if swift(>=4.0)
var expression = String(string[string.characters.index(after: string.startIndex)..<string.characters.index(before: string.endIndex)])
let firstCharacter = String(expression[..<expression.characters.index(after: expression.startIndex)])
var expression = String(string[string.index(after: string.startIndex)..<string.index(before: string.endIndex)])
let firstCharacter = String(expression[..<expression.index(after: expression.startIndex)])
#elseif swift(>=3.2)
var expression = string.substring(with: string.index(after: string.startIndex)..<string.index(before: string.endIndex))
let firstCharacter = expression.substring(to: expression.index(after: expression.startIndex))
#else
var expression = string.substring(with: string.characters.index(after: string.startIndex)..<string.characters.index(before: string.endIndex))
let firstCharacter = expression.substring(to: expression.characters.index(after: expression.startIndex))
var expression = string.substring(with: string.characters.index(after: string.startIndex)..<string.characters.index(before: string.endIndex))
let firstCharacter = expression.substring(to: expression.characters.index(after: expression.startIndex))
#endif

var op = self.operators.filter {
Expand All @@ -134,7 +143,9 @@ public struct URITemplate : CustomStringConvertible, Equatable, Hashable, Expres

if (op != nil) {
#if swift(>=4.0)
expression = String(expression[expression.characters.index(after: expression.startIndex)...])
expression = String(expression[expression.index(after: expression.startIndex)...])
#elseif swift(>=3.2)
expression = expression.substring(from: expression.index(after: expression.startIndex))
#else
expression = expression.substring(from: expression.characters.index(after: expression.startIndex))
#endif
Expand All @@ -160,9 +171,11 @@ public struct URITemplate : CustomStringConvertible, Equatable, Hashable, Expres

if explode {
#if swift(>=4.0)
variable = String(variable[..<variable.characters.index(before: variable.endIndex)])
variable = String(variable[..<variable.index(before: variable.endIndex)])
#elseif swift(>=3.2)
variable = variable.substring(to: variable.index(before: variable.endIndex))
#else
variable = variable.substring(to: variable.characters.index(before: variable.endIndex))
variable = variable.substring(to: variable.characters.index(before: variable.endIndex))
#endif
}

Expand Down Expand Up @@ -206,9 +219,11 @@ public struct URITemplate : CustomStringConvertible, Equatable, Hashable, Expres

if op != nil {
#if swift(>=4.0)
expression = String(expression[expression.characters.index(after: expression.startIndex)..<expression.endIndex])
expression = String(expression[expression.index(after: expression.startIndex)..<expression.endIndex])
#elseif swift(>=3.2)
expression = expression.substring(with: expression.index(after: expression.startIndex)..<expression.endIndex)
#else
expression = expression.substring(with: expression.characters.index(after: expression.startIndex)..<expression.endIndex)
expression = expression.substring(with: expression.characters.index(after: expression.startIndex)..<expression.endIndex)
#endif
}

Expand All @@ -224,8 +239,13 @@ public struct URITemplate : CustomStringConvertible, Equatable, Hashable, Expres

let pattern = regex.substitute(self.template) { expression in
if expression.hasPrefix("{") && expression.hasSuffix("}") {
let startIndex = expression.characters.index(after: expression.startIndex)
let endIndex = expression.characters.index(before: expression.endIndex)
#if swift(>=3.2)
let startIndex = expression.index(after: expression.startIndex)
let endIndex = expression.index(before: expression.endIndex)
#else
let startIndex = expression.characters.index(after: expression.startIndex)
let endIndex = expression.characters.index(before: expression.endIndex)
#endif
#if swift(>=4.0)
return self.regexForExpression(String(expression[startIndex..<endIndex]))
#else
Expand Down Expand Up @@ -362,8 +382,17 @@ class BaseOperator {
// Point to overide to expanding a string
func expand(variable:String, value:String, prefix:Int?) -> String {
if let prefix = prefix {
if value.characters.count > prefix {
#if swift(>=3.2)
let valueCount = value.count
#else
let valueCount = value.characters.count
#endif
if valueCount > prefix {
#if swift(>=3.2)
let index = value.index(value.startIndex, offsetBy: prefix, limitedBy: value.endIndex)
#else
let index = value.characters.index(value.startIndex, offsetBy: prefix, limitedBy: value.endIndex)
#endif
#if swift(>=4.0)
return expand(value: String(value[..<index!]))
#else
Expand Down Expand Up @@ -482,7 +511,12 @@ class PathStyleParameterExpansion : BaseOperator, Operator {
}

override func expand(variable:String, value:String, prefix:Int?) -> String {
if value.characters.count > 0 {
#if swift(>=3.2)
let valueCount = value.count
#else
let valueCount = value.characters.count
#endif
if valueCount > 0 {
let expandedValue = super.expand(variable: variable, value: value, prefix: prefix)
return "\(variable)=\(expandedValue)"
}
Expand Down

0 comments on commit 9f2a9da

Please sign in to comment.