0001    //
0002    //  NSString.swift
0003    //  NSLinux
0004    //
0005    //  Created by John Holdsworth on 11/06/2015.
0006    //  Copyright (c) 2015 John Holdsworth. All rights reserved.
0007    //
0008    //  $Id: //depot/NSLinux/Sources/NSString.swift#12 $
0009    //
0010    //  Repo: https://github.com/johnno1962/NSLinux
0011    //
0012    
0013    // Hastily put together NSString substitutes
0014    
0015    #if os(Linux)
0016    import Foundation
0017    import Glibc
0018    
0019    private let O = "0".ord, A = "A".ord, percent = "%".ord
0020    
0021    private func unhex( char: Int8 ) -> Int8 {
0022        return char < A ? char - O : char - A + 10
0023    }
0024    
0025    public extension String {
0026    
0027        public var ord: Int8 {
0028            return Int8(utf8.first!)
0029        }
0030    
0031        public func stringByAddingPercentEscapesUsingEncoding( encoding: UInt ) -> String? {
0032            return self
0033        }
0034    
0035    /* no longer required as implemented in Linux Swift Foundation
0036    
0037        public var stringByRemovingPercentEncoding: String? {
0038            var arr = [Int8]( count: 100000, repeatedValue: 0 )
0039            var out = UnsafeMutablePointer<Int8>( arr )
0040    
0041            withCString { (bytes) in
0042                var bytes = UnsafeMutablePointer<Int8>(bytes)
0043    
0044                while out < &arr + arr.count {
0045                    let start = strchr( bytes, Int32(percent) ) - UnsafeMutablePointer<Int8>( bytes )
0046    
0047                    let extralen = start < 0 ? Int(strlen( bytes )) : start + 1
0048                    let required = out - UnsafeMutablePointer<Int8>(arr) + extralen
0049                    if required > arr.count {
0050                        var newarr = [Int8]( count: Int(Double(required) * 1.5), repeatedValue: 0 )
0051                        strcpy( &newarr, arr )
0052                        arr = newarr
0053                        out = &arr + Int(strlen( arr ))
0054                    }
0055    
0056                    if start < 0 {
0057                        strcat( out, bytes )
0058                        break
0059                    }
0060    
0061                    bytes[start] = 0
0062                    strcat( out, bytes )
0063                    bytes += start + 3
0064                    out += start + 1
0065                    out[-1] = (unhex( bytes[-2] ) << 4) + unhex( bytes[-1] )
0066                }
0067            }
0068            
0069            return String.fromCString( arr )
0070        }
0071    
0072        public func stringByTrimmingCharactersInSet( cset: NSCharacterSet ) -> String {
0073            return self
0074        }
0075        
0076        public func componentsSeparatedByString( sep: String ) -> [String] {
0077            var out = [String]()
0078    
0079            withCString { (bytes) in
0080                sep.withCString { (sbytes) in
0081                    var bytes = UnsafeMutablePointer<Int8>( bytes )
0082    
0083                    while true {
0084                        let start = strstr( bytes, sbytes ) - UnsafeMutablePointer<Int8>( bytes )
0085                        if start < 0 {
0086                            out.append( String.fromCString( bytes )! )
0087                            break
0088                        }
0089                        bytes[start] = 0
0090                        out.append( String.fromCString( bytes )! )
0091                        bytes += start + Int(strlen( sbytes ))
0092                    }
0093                }
0094            }
0095    
0096            return out
0097        }
0098    */
0099    
0100        public func stringByReplacingOccurrencesOfString( str1: String, withString str2: String ) -> String {
0101            return componentsSeparatedByString( str1 ).joinWithSeparator( str2 )
0102        }
0103    
0104        public func rangeOfString( str: String ) -> Range<Int>? {
0105            var start = -1
0106            withCString { (bytes) in
0107                str.withCString { (sbytes) in
0108                    start = strstr( bytes, sbytes ) - UnsafeMutablePointer<Int8>( bytes )
0109                }
0110            }
0111            return start < 0 ? nil : start..<start+str.utf8.count
0112        }
0113    
0114        public func substringToIndex( index: Int ) -> String {
0115            var out = self
0116            withCString { (bytes) in
0117                let bytes = UnsafeMutablePointer<Int8>(bytes)
0118                bytes[index] = 0
0119                out = String.fromCString( bytes )!
0120            }
0121            return out
0122        }
0123        
0124        public func substringFromIndex( index: Int ) -> String {
0125            var out = self
0126            withCString { (bytes) in
0127                out = String.fromCString( bytes+index )!
0128            }
0129            return out
0130        }
0131        
0132    }
0133    #endif
0134