-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
64 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,5 +17,8 @@ | |
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
}, | ||
"properties" : { | ||
"template-rendering-intent" : "original" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// String+extension.swift | ||
// MonarchRouter | ||
// | ||
// Created by Eliah Snakin on 13.11.2019. | ||
// Copyright © 2019 nikans.com. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
|
||
// A set of operations for parametrized routes handling. | ||
extension String | ||
{ | ||
/// Returns whether a String matches a given regex. | ||
func matches(_ regex: String) -> Bool { | ||
return self.range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil | ||
} | ||
|
||
/// Returns substrings captured by regex groups. | ||
func capturedGroups(withRegex pattern: String) -> [String] { | ||
var results = [String]() | ||
|
||
var regex: NSRegularExpression | ||
do { | ||
regex = try NSRegularExpression(pattern: pattern, options: []) | ||
} catch { | ||
return results | ||
} | ||
|
||
let matches = regex.matches(in: self, options: [], range: NSRange(location:0, length: count)) | ||
|
||
guard let match = matches.first else { return results } | ||
|
||
let lastRangeIndex = match.numberOfRanges - 1 | ||
guard lastRangeIndex >= 1 else { return results } | ||
|
||
for i in 1...lastRangeIndex { | ||
let capturedGroupIndex = match.range(at: i) | ||
let matchedString = (self as NSString).substring(with: capturedGroupIndex) | ||
results.append(matchedString) | ||
} | ||
|
||
return results | ||
} | ||
} |