spiegel-keyman/ios/engine/KMEI/KeymanEngine/Classes/String+Helpers.swift
2017-10-20 09:49:53 +07:00

36 lines
1.1 KiB
Swift

//
// String+Helpers.swift
// KeymanEngine
//
// Created by Gabriel Wong on 2017-10-12.
// Copyright © 2017 SIL International. All rights reserved.
//
extension String {
var hasFontExtension: Bool {
return self.hasSuffix(".ttf") || self.hasSuffix(".otf")
}
var hasJavaScriptExtension: Bool {
return self.hasSuffix(".js")
}
/// - Parameter separator: The separator between code units.
/// - Returns: This String interpreted as hex-encoded UTF-16 code units.
func stringFromUTF16CodeUnits(separatedBy separator: String = ",") -> String? {
// TODO: Use one scanner for the whole String instead of splitting by comma first
let hexStrings = components(separatedBy: separator)
var codeUnits: [UInt16] = []
for hexString in hexStrings {
let scanner = Scanner(string: hexString)
var codeUnit: UInt32 = 0
if scanner.scanHexInt32(&codeUnit) && scanner.isAtEnd {
codeUnits.append(UInt16(codeUnit))
} else {
return nil
}
}
return String(utf16CodeUnits: codeUnits, count: codeUnits.count)
}
}