0001    var _module_nsstring = true
0002    
0003    #if os(Linux)
0004    
0005    import Foundation
0006    import Glibc
0007    
0008    private let O = "0".ord, A = "A".ord, percent = "%".ord
0009    
0010    private func unhex( char: Int8 ) -> Int8 {
0011        return char < A ? char - O : char - A + 10
0012    }
0013        
0014    
0015    extension String {
0016    
0017        var ord: Int8 {
0018            return Int8(utf8.first!)
0019        }
0020    
0021        var stringByRemovingPercentEncoding: String? {
0022            var arr = [Int8]( count: 100000, repeatedValue: 0 )
0023            var out = UnsafeMutablePointer<Int8>( arr )
0024    
0025            withCString { (bytes) in
0026                var bytes = UnsafeMutablePointer<Int8>(bytes)
0027    
0028                while out < &arr + arr.count {
0029                    let start = strchr( bytes, Int32(percent) ) - UnsafeMutablePointer<Int8>( bytes )
0030    
0031                    let extralen = start < 0 ? Int(strlen( bytes )) : start + 1
0032                    let required = out - UnsafeMutablePointer<Int8>(arr) + extralen
0033                    if required > arr.count {
0034                        var newarr = [Int8]( count: Int(Double(required) * 1.5), repeatedValue: 0 )
0035                        strcpy( &newarr, arr )
0036                        arr = newarr
0037                        out = &arr + Int(strlen( arr ))
0038                    }
0039    
0040                    if start < 0 {
0041                        strcat( out, bytes )
0042                        break
0043                    }
0044    
0045                    bytes[start] = 0
0046                    strcat( out, bytes )
0047                    bytes += start + 3
0048                    out += start + 1
0049                    out[-1] = (unhex( bytes[-2] ) << 4) + unhex( bytes[-1] )
0050                }
0051            }
0052            
0053            return String.fromCString( arr )
0054        }
0055    
0056        func stringByAddingPercentEscapesUsingEncoding( encoding: UInt ) -> String? {
0057            return self
0058        }
0059    
0060        func stringByTrimmingCharactersInSet( cset: NSCharacterSet ) -> String {
0061            return self
0062        }
0063        
0064        func componentsSeparatedByString( sep: String ) -> [String] {
0065            var out = [String]()
0066    
0067            withCString { (bytes) in
0068                sep.withCString { (sbytes) in
0069                    var bytes = UnsafeMutablePointer<Int8>( bytes )
0070    
0071                    while true {
0072                        let start = strstr( bytes, sbytes ) - UnsafeMutablePointer<Int8>( bytes )
0073                        if start < 0 {
0074                            out.append( String.fromCString( bytes )! )
0075                            break
0076                        }
0077                        bytes[start] = 0
0078                        out.append( String.fromCString( bytes )! )
0079                        bytes += start + Int(strlen( sbytes ))
0080                    }
0081                }
0082            }
0083    
0084            return out
0085        }
0086    
0087        func rangeOfString( str: String ) -> Range<Int>? {
0088            var start = -1
0089            withCString { (bytes) in
0090                str.withCString { (sbytes) in
0091                    start = strstr( bytes, sbytes ) - UnsafeMutablePointer<Int8>( bytes )
0092                }
0093            }
0094            return start < 0 ? nil : start..<start+str.utf8.count
0095        }
0096    
0097        func substringToIndex( index: Int ) -> String {
0098            var out = self
0099            withCString { (bytes) in
0100                let bytes = UnsafeMutablePointer<Int8>(bytes)
0101                bytes[index] = 0
0102                out = String.fromCString( bytes )!
0103            }
0104            return out
0105        }
0106        
0107        func substringFromIndex( index: Int ) -> String {
0108            var out = self
0109            withCString { (bytes) in
0110                out = String.fromCString( bytes+index )!
0111            }
0112            return out
0113        }
0114        
0115    }
0116    #endif
0117    
0118    
0119    var _module_dispatch = true
0120    
0121    #if os(Linux)
0122    import Glibc
0123    
0124    let DISPATCH_QUEUE_CONCURRENT = 0, DISPATCH_QUEUE_PRIORITY_HIGH = 0, DISPATCH_QUEUE_PRIORITY_LOW = 0, DISPATCH_QUEUE_PRIORITY_BACKGROUND = 0
0125    
0126    func dispatch_get_global_queue( type: Int, _ flags: Int ) -> Int {
0127        return 0
0128    }
0129    
0130    func dispatch_queue_create( name: String, _ type: Int ) -> Int {
0131        return 0
0132    }
0133    
0134    func dispatch_sync( queue: Int, _ block: () -> () ) {
0135        block()
0136    }
0137    
0138    private class pthreadBlock {
0139    
0140        let block: () -> ()
0141    
0142        init( block: () -> () ) {
0143            self.block = block
0144        }
0145    }
0146    
0147    private func pthreadRunner( arg: UnsafeMutablePointer<Void> ) -> UnsafeMutablePointer<Void> {
0148        let unmanaged = Unmanaged<pthreadBlock>.fromOpaque( COpaquePointer( arg ) )
0149        unmanaged.takeUnretainedValue().block()
0150        unmanaged.release()
0151        return arg
0152    }
0153    
0154    func dispatch_async( queue: Int, _ block: () -> () ) {
0155        let holder = Unmanaged.passRetained( pthreadBlock( block: block ) )
0156        let pointer = UnsafeMutablePointer<Void>( holder.toOpaque() )
0157        #if os(Linux)
0158        var pthread: pthread_t = 0
0159        #else
0160        var pthread: pthread_t = nil
0161        #endif
0162        if pthread_create( &pthread, nil, pthreadRunner, pointer ) == 0 {
0163            pthread_detach( pthread )
0164        }
0165        else {
0166            print( "pthread_create() error" )
0167        }
0168    }
0169    
0170    let DISPATCH_TIME_NOW = 0, NSEC_PER_SEC = 1_000_000_000
0171    
0172    func dispatch_time( now: Int, _ nsec: Int64 ) -> Int64 {
0173        return nsec
0174    }
0175    
0176    func dispatch_after( delay: Int64, _ queue: Int, _ block: () -> () ) {
0177        dispatch_async( queue, {
0178            sleep( UInt32(Int(delay)/NSEC_PER_SEC) )
0179            block()
0180        } )
0181    }
0182    
0183    #endif
0184