0001    //
0002    //  String+Extensions.swift
0003    //  HttpBasicAuth
0004    //
0005    //  This source file is part of the HttpBasicAuth open source project
0006    //
0007    //  Created by John Becker on 1/13/16.
0008    //  Copyright © 2016 Beckersoft. All rights reserved.
0009    //  Licensed under MIT License
0010    //
0011    //  See LICENSE.txt for license information.
0012    //
0013    
0014    import Foundation
0015    
0016    /// String extensions
0017    internal extension String {
0018        
0019        /**
0020         Returns strings matching the regular expression pattern.
0021         
0022         - Parameter pattern: The regular expression pattern.
0023         
0024         - Returns: An array of matching strings.
0025         */
0026        func regexMatches
BasicAuth.swift:51
            encodedAuth = header.regexMatches(credentialsRegEx).last,
BasicAuth.swift:55
            let userPass = decodedAuth.regexMatches(userPassRegEx)
(pattern:String) -> [String] { 0027 0028 let regex = try! NSRegularExpression(pattern: pattern, 0029 options:[.CaseInsensitive]) 0030 0031 0032 let textCheckingResults = regex.matchesInString(self, 0033 options: [], 0034 range: NSMakeRange(0, self.utf16.count)) 0035 0036 var matches:[String] = [] 0037 for match in textCheckingResults { 0038 for index in 0 ..< match.numberOfRanges { 0039 if let range = 0040 self.rangeFromNSRange(match.rangeAtIndex(index)) { 0041 matches.append(self.substringWithRange(range)) 0042 } 0043 } 0044 } 0045 return matches 0046 } 0047 0048 /** 0049 Converts NSRange to Range<String.Index>. 0050 0051 - Parameter nsRange: The NSRange in need of conversion. 0052 0053 - Returns: A Range<String.Index>? converted based on the string. 0054 */ 0055 func rangeFromNSRange
String+Extensions.swift:40
                    self.rangeFromNSRange(match.rangeAtIndex(index)) {
(nsRange:NSRange) -> Range<String.Index>? { 0056 let from16 = utf16.startIndex.advancedBy(nsRange.location, 0057 limit: utf16.endIndex) 0058 let to16 = from16.advancedBy(nsRange.length, limit: utf16.endIndex) 0059 if let from = String.Index(from16, within: self), 0060 to = String.Index(to16, within: self) { 0061 return from ..< to 0062 } 0063 return nil 0064 } 0065 0066 /** 0067 Base64 encodes strings using UTF8 string encoding by default. 0068 0069 - Parameter encoding: The encoding to be used. Defaults to UTF8. 0070 0071 - Returns: A base64 encoded string. 0072 */ 0073 func base64Encode(encoding:NSStringEncoding = NSUTF8StringEncoding) 0074 -> String? { 0075 if let encodedData = self.dataUsingEncoding(encoding) { 0076 return encodedData.base64EncodedStringWithOptions([]) 0077 } 0078 return nil 0079 } 0080 0081 /** 0082 Base64 decodes encoded strings using UTF8 string encoding by default. 0083 0084 - Parameter encoding: The encoding to be used. Defaults to UTF8. 0085 0086 - Returns: A regular string. 0087 */ 0088 func base64Decode
BasicAuth.swift:53
            decodedAuth = encodedAuth.base64Decode() {
(encoding:NSStringEncoding = NSUTF8StringEncoding) 0089 -> String? { 0090 if let data = NSData(base64EncodedString: self, options:[]) { 0091 return String(data: data, encoding: encoding) 0092 } 0093 return nil 0094 } 0095 }