0001 var _module_nsstring = true 0002 0003 #if os(Linux) 0004 import Foundation 0005 import Glibc 0006 0007 private let O = "0".ord, A = "A".ord, percent = "%".ord 0008 0009 private func unhex( char: Int8 ) -> Int8 { 0010 return char < A ? char - O : char - A + 10 0011 } 0012 0013 extension String { 0014 0015 var ord: Int8 { 0016 return Int8(utf8.first!) 0017 } 0018 0019 var stringByRemovingPercentEncoding: String? { 0020 var arr = [Int8]( count: 100000, repeatedValue: 0 ) 0021 var out = UnsafeMutablePointer<Int8>( arr ) 0022 0023 withCString { (bytes) in 0024 var bytes = UnsafeMutablePointer<Int8>(bytes) 0025 0026 while out < &arr + arr.count { 0027 let start = strchr( bytes, Int32(percent) ) - UnsafeMutablePointer<Int8>( bytes ) 0028 0029 let extralen = start < 0 ? Int(strlen( bytes )) : start + 1 0030 let required = out - UnsafeMutablePointer<Int8>(arr) + extralen 0031 if required > arr.count { 0032 var newarr = [Int8]( count: Int(Double(required) * 1.5), repeatedValue: 0 ) 0033 strcpy( &newarr, arr ) 0034 arr = newarr 0035 out = &arr + Int(strlen( arr )) 0036 } 0037 0038 if start < 0 { 0039 strcat( out, bytes ) 0040 break 0041 } 0042 0043 bytes[start] = 0 0044 strcat( out, bytes ) 0045 bytes += start + 3 0046 out += start + 1 0047 out[-1] = (unhex( bytes[-2] ) << 4) + unhex( bytes[-1] ) 0048 } 0049 } 0050 0051 return String.fromCString( arr ) 0052 } 0053 0054 func stringByAddingPercentEscapesUsingEncoding( encoding: UInt ) -> String? { 0055 return self 0056 } 0057 0058 func stringByTrimmingCharactersInSet( cset: NSCharacterSet ) -> String { 0059 return self 0060 } 0061 0062 func componentsSeparatedByString( sep: String ) -> [String] { 0063 var out = [String]() 0064 0065 withCString { (bytes) in 0066 sep.withCString { (sbytes) in 0067 var bytes = UnsafeMutablePointer<Int8>( bytes ) 0068 0069 while true { 0070 let start = strstr( bytes, sbytes ) - UnsafeMutablePointer<Int8>( bytes ) 0071 if start < 0 { 0072 out.append( String.fromCString( bytes )! ) 0073 break 0074 } 0075 bytes[start] = 0 0076 out.append( String.fromCString( bytes )! ) 0077 bytes += start + Int(strlen( sbytes )) 0078 } 0079 } 0080 } 0081 0082 return out 0083 } 0084 0085 func rangeOfString( str: String ) -> Range<Int>? { 0086 var start = -1 0087 withCString { (bytes) in 0088 str.withCString { (sbytes) in 0089 start = strstr( bytes, sbytes ) - UnsafeMutablePointer<Int8>( bytes ) 0090 } 0091 } 0092 return start < 0 ? nil : start..<start+str.utf8.count 0093 } 0094 0095 func substringToIndex( index: Int ) -> String { 0096 var out = self 0097 withCString { (bytes) in 0098 let bytes = UnsafeMutablePointer<Int8>(bytes) 0099 bytes[index] = 0 0100 out = String.fromCString( bytes )! 0101 } 0102 return out 0103 } 0104 0105 func substringFromIndex( index: Int ) -> String { 0106 var out = self 0107 withCString { (bytes) in 0108 out = String.fromCString( bytes+index )! 0109 } 0110 return out 0111 } 0112 0113 } 0114 #endif 0115