Created
February 24, 2016 17:55
-
-
Save aciidgh/5a5920b86621b08347f0 to your computer and use it in GitHub Desktop.
unsafeString
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
//func ptrFromAddress<T>(p:UnsafeMutablePointer<T>) -> UnsafeMutablePointer<T> { return p } | |
//var buf = [CChar]() | |
////put some stuff in the buffer | |
//buf.append(CChar(0)) //CRITICAL or you'll overrun the buffer on the next line | |
// | |
//var mystr = "Dd" | |
//var pl: UnsafePointer<CChar> = UnsafePointer<CChar>(bitPattern: 0) | |
// | |
//func foo(inout errorMessage: UnsafeMutablePointer<CChar>) -> UnsafeMutablePointer<CChar> { | |
// return errorMessage | |
//} | |
// | |
////foo(&mystr) | |
// | |
//var str = "SDsds" | |
//let a = Array(str.utf8) | |
//a | |
//:hello | |
var str = "Hello, playground" | |
var chars = str.utf8.map({$0}) | |
let ptr = UnsafeMutableBufferPointer.init(start: &chars, count: chars.count+1) | |
let first = ptr.baseAddress | |
ptr[1] = "R".utf8.first ?? 0 | |
ptr[ptr.endIndex-1] = 0 | |
let result = String.fromCString(UnsafeMutablePointer<CChar>(ptr.baseAddress))! | |
print(result) | |
// | |
//var str2 = "" | |
//transcode(UTF8.self, UTF32.self, ptr.generate(), { str2.append(UnicodeScalar($0)) }, stopOnError: true) | |
// | |
//print(str2) | |
//// | |
// | |
//var str = "Hello, playground" | |
//var chars = str.utf8.map({$0}) | |
//let ptr = UnsafeMutableBufferPointer.init(start: &chars, count: chars.count) | |
// | |
// | |
// | |
//let first = UnsafeMutablePointer<CChar>(ptr.baseAddress) | |
// | |
//String.fromCString(first) | |
//withUnsafeMutablePointer(buf) { ptr in | |
// String.fromCString(ptr) | |
//} | |
//let myStr = buf.wi { String.fromCString($0) } | |
//let myStr = buf.withUnsafePointerToElements { } | |
//var myStr = "ddddd" | |
// | |
//let myStrPtr = ptrFromAddress(&myStr) | |
// | |
// | |
//let cchar = UnsafeMutablePointer<CChar>(myStrPtr) | |
// | |
//let resultBack = String.fromCString(myStrPtr) | |
// | |
// | |
//let argc: CInt = 5 | |
// | |
//let argv = ["hello", "how", "are", "you", "ok"].map { (var str) in ptrFromAddress(&str) } | |
// | |
//var firstString = UnsafeMutablePointer<CChar>(argv[0]) | |
////argv[0].memory | |
////firstString.memory | |
////String.fromCString(firstString) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment