0001    /**
0002    * Copyright IBM Corporation 2015
0003    *
0004    * Licensed under the Apache License, Version 2.0 (the "License");
0005    * you may not use this file except in compliance with the License.
0006    * You may obtain a copy of the License at
0007    *
0008    * http://www.apache.org/licenses/LICENSE-2.0
0009    *
0010    * Unless required by applicable law or agreed to in writing, software
0011    * distributed under the License is distributed on an "AS IS" BASIS,
0012    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013    * See the License for the specific language governing permissions and
0014    * limitations under the License.
0015    **/
0016    
0017    #if os(OSX) || os(iOS)
0018        import Darwin
0019    #elseif os(Linux)
0020        import Glibc
0021    #endif
0022    
0023    import Foundation
0024    
0025    // MARK: StringUtils
0026    
0027    public class StringUtils {
0028        
0029        ///
0030        /// Converts a Swift string to a UTF encoded NSData 
0031        ///
0032        /// - Parameter str: String
0033        /// 
0034        /// - Returns: NSData?
0035        ///
0036        public static func toUtf8String(str: String) -> NSData? {
0037            let nsstr:NSString = str.bridge()
0038            let data = nsstr.dataUsingEncoding(NSUTF8StringEncoding)
0039            return data
0040        }
0041        
0042        
0043        ///
0044        /// Converts a Swift string to a UTF encoded null terminated NSData
0045        ///
0046        /// - Parameter str: String
0047        ///
0048        /// - Returns: NSData?
0049        ///
0050        public static func toNullTerminatedUtf8String(str: String) -> NSData? {
0051            let nsstr:NSString = str.bridge()
0052            let cString = nsstr.cStringUsingEncoding(NSUTF8StringEncoding)
0053            let data = NSData(bytes: cString, length: Int(strlen(cString))+1)
0054            return data
0055        }
0056        
0057        
0058        ///
0059        /// Converts a UTF 8 encoded string to a Swift String
0060        ///
0061        /// - Parameter str: String
0062        ///
0063        /// - Returns: String?
0064        ///
0065        public static func fromUtf8String(data: NSData) -> String? {
0066            let str = NSString(data: data, encoding: NSUTF8StringEncoding)
0067            return str!.bridge()
0068        }
0069    }
0070    
0071    
0072    // MARK: String extensions
0073    //
0074    // Because that auto bridged Strings to NSStrings do not exist yet for Linux, a bridge method
0075    // must be called on the String. This bridge method does not exist on Mac OS X. Therefore, these
0076    // extensions are added to the String structure so that bridge can be called regardless of 
0077    // operating systems.
0078    //
0079    #if os(OSX) || os(iOS)
0080        
0081    public extension String {
0082        func bridge
StringUtils.swift:37
        let nsstr:NSString = str.bridge()
StringUtils.swift:51
        let nsstr:NSString = str.bridge()
() -> NSString { 0083 return self as NSString 0084 } 0085 } 0086 0087 public extension NSString { 0088 func bridge
StringUtils.swift:67
        return str!.bridge()
() -> String { 0089 return self as String 0090 } 0091 } 0092 0093 #endif 0094