Skip to content

Instantly share code, notes, and snippets.

@norio-nomura
Last active April 5, 2016 10:40
Show Gist options
  • Save norio-nomura/3355d31b613b1956f23600c637f55b75 to your computer and use it in GitHub Desktop.
Save norio-nomura/3355d31b613b1956f23600c637f55b75 to your computer and use it in GitHub Desktop.
StringインスタンスをStaticStringとして使う `func withStaticString<T>(@NoEscape f: StaticString -> T)`
import Foundation
extension String {
func withStaticString(@noescape f: StaticString -> Void) {
withCString {
let rawPointer = $0._rawValue
let byteSize = lengthOfBytesUsingEncoding(NSUTF8StringEncoding)._builtinWordValue
let isASCII = true._getBuiltinLogicValue()
let staticString = StaticString(_builtinStringLiteral: rawPointer, byteSize: byteSize, isASCII: isASCII)
f(staticString)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment