|
/// Below are two test functions and a collection of Unicode characters with various characteristics, |
|
/// to test the Swift 5.0 String infrastructure and compare results with NSString. |
|
|
|
import Foundation |
|
|
|
func stringPrint(_ string: String) { |
|
print("String `\(string)` is \(string.data(using: .utf8)?.count ?? 0) byte(s) long and contains \(string.count) character(s).") |
|
} |
|
|
|
func nsStringPrint(_ string: NSString) { |
|
let data = string.data(using: 4) |
|
var stringFromData: NSString = "" |
|
if let data = data { |
|
stringFromData = NSString(data: data, encoding: 4) ?? "" |
|
} |
|
|
|
print("String `\(string)` is \(data?.count ?? 0) byte(s) long and contains \(string.length) character(s). NSString from data: \(stringFromData)") |
|
} |
|
|
|
|
|
func stringPrint(_ string: String) { |
|
print("String `\(string)` is \(string.data(using: .utf8)?.count ?? 0) byte(s) long and contains \(string.count) character(s).") |
|
} |
|
|
|
func nsStringPrint(_ string: NSString) { |
|
let data = string.data(using: 4) |
|
var stringFromData: NSString = "" |
|
if let data = data { |
|
stringFromData = NSString(data: data, encoding: 4) ?? "" |
|
} |
|
|
|
print("String `\(string)` is \(data?.count ?? 0) byte(s) long and contains \(string.length) character(s). String from data: \(stringFromData)") |
|
} |
|
|
|
|
|
let oneByte = "a" |
|
stringPrint(oneByte) |
|
|
|
let twoBytes = "α" |
|
stringPrint(twoBytes) |
|
|
|
let twoBytes1 = "ΐ" |
|
stringPrint(twoBytes1) |
|
|
|
let twoBytes2 = "å" |
|
stringPrint(twoBytes2) |
|
|
|
let twoBytes3 = "և" |
|
stringPrint(twoBytes3) |
|
|
|
let threeBytes0 = "熊" |
|
stringPrint(threeBytes0) |
|
|
|
let threeByte1 = "学" |
|
stringPrint(threeByte1) |
|
|
|
let threeByte2 = "他" |
|
stringPrint(threeByte2) |
|
|
|
let threeBytes3 = "ᅠ" |
|
stringPrint(threeBytes3) |
|
|
|
let threeBytes4 = "ẞ" |
|
stringPrint(threeBytes4) |
|
|
|
let fourBytes = "𠻗" |
|
stringPrint(fourBytes) |
|
|
|
let fourBytes2 = "𠻗𠻗" |
|
stringPrint(fourBytes2) |
|
|
|
let sixBytesTwoCharacters = "ᅠᾉ" |
|
stringPrint(sixBytesTwoCharacters) |
|
|
|
let sixBytesTwoCharacters1 = "ᅠᾋ" |
|
stringPrint(sixBytesTwoCharacters1) |
|
|
|
let sixBytesTwoCharacters2 = "ᅠᾣ" |
|
stringPrint(sixBytesTwoCharacters2) |
|
|
|
let thirtySixBytesTwoCharacters = "ᅠE̴̷̬͎̱̘͇͍̾ͦ͊͒͊̓̓̐" |
|
stringPrint(thirtySixBytesTwoCharacters) |
|
|
|
let nsString: NSString = "a" |
|
nsStringPrint(nsString) |
|
|
|
let nsString2: NSString = "ᾋ" |
|
nsStringPrint(nsString2) |
|
|
|
let nsString3: NSString = "E̴̷̬͎̱̘͇͍̾ͦ͊͒͊̓̓̐" |
|
nsStringPrint(nsString3) |