mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[regex] Use Swift in the parser Add in a strawperson use of Swift by the parser, for future regex support.
21 lines
570 B
Swift
21 lines
570 B
Swift
import ExperimentalRegexBridging
|
|
|
|
public func experimental_regex_strawperson(
|
|
_ ptr: UnsafePointer<CChar>?
|
|
) -> UnsafePointer<CChar>? {
|
|
guard let s = ptr else { return nil }
|
|
|
|
let str = "Hello, \(String(cString:s))"
|
|
let count = str.utf8.count + 1
|
|
return str.withCString {
|
|
assert($0[count-1] == 0)
|
|
let ptr = UnsafeMutablePointer<CChar>.allocate(capacity: count)
|
|
ptr.initialize(from: $0, count: count)
|
|
return UnsafePointer(ptr)
|
|
}
|
|
}
|
|
|
|
public func registerParser() {
|
|
Parser_registerParseRegexStrawperson({ experimental_regex_strawperson($0) })
|
|
}
|