Created
January 21, 2015 03:57
-
-
Save alexbosworth/3e2977df329c792cae86 to your computer and use it in GitHub Desktop.
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
/** Get formatted links from links header | |
*/ | |
func getLinksFromHeader(linksString: String?) -> [String: String] { | |
var res = [String: String]() | |
if linksString == nil { return res } | |
var err: NSError? | |
let relRegex = NSRegularExpression(pattern: "rel=\\\"?([^\\\"]+)\\\"?", options: .CaseInsensitive, error: &err) | |
var trimChars = NSCharacterSet.whitespaceCharacterSet().mutableCopy() as NSMutableCharacterSet | |
trimChars.addCharactersInString("<>") | |
for link in linksString!.componentsSeparatedByString(",") { | |
let semicolonPosition = link.rangeOfString(";") | |
if semicolonPosition == nil { continue } | |
let target = link.substringToIndex(semicolonPosition!.startIndex).stringByTrimmingCharactersInSet(trimChars) | |
if target.utf16Count == 0 { continue } | |
let match = relRegex?.firstMatchInString(link, options: .allZeros, range: NSMakeRange(0, link.utf16Count)) | |
if let m = match { | |
let type = (link as NSString).substringWithRange(m.rangeAtIndex(1)) | |
res[type] = target | |
} | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment