Created
April 18, 2019 13:36
-
-
Save goa/46932b15c76fdeb988fc956bff505569 to your computer and use it in GitHub Desktop.
A small playground to test characteristics of the new Strings implementation in Swift 5.0 and compare them to NSString
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment