Created
April 15, 2021 12:05
-
-
Save ikhvorost/052eda8bea233b0afd1c4c346247196f to your computer and use it in GitHub Desktop.
Swift Demangle
This file contains 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
typealias Swift_Demangle = @convention(c) (_ mangledName: UnsafePointer<UInt8>?, | |
_ mangledNameLength: Int, | |
_ outputBuffer: UnsafeMutablePointer<UInt8>?, | |
_ outputBufferSize: UnsafeMutablePointer<Int>?, | |
_ flags: UInt32) -> UnsafeMutablePointer<Int8>? | |
func swift_demangle(_ mangled: String) -> String? { | |
let RTLD_DEFAULT = dlopen(nil, RTLD_NOW) | |
if let sym = dlsym(RTLD_DEFAULT, "swift_demangle") { | |
let f = unsafeBitCast(sym, to: Swift_Demangle.self) | |
if let cString = f(mangled, mangled.count, nil, nil, 0) { | |
defer { cString.deallocate() } | |
return String(cString: cString) | |
} | |
} | |
return nil | |
} | |
// How to use | |
if let s = swift_demangle("$s20MyPlayground_Sources4TestC4testSSyF") { | |
print(s) // MyPlayground_Sources.Test.test() -> Swift.String | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment