Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed character warning for Swift 3.2, Swift 4.0 & Swift 4.2 #42

Merged
merged 2 commits into from
Mar 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 52 additions & 18 deletions Sources/URITemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,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 @@ -84,7 +86,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 @@ -96,9 +100,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 @@ -111,11 +117,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 @@ -128,7 +137,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 @@ -154,9 +165,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 @@ -200,9 +213,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 @@ -218,8 +233,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 @@ -347,8 +367,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 @@ -467,7 +496,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