|
|
|
@ -2,17 +2,18 @@ import Foundation
|
|
|
|
|
import Splash
|
|
|
|
|
|
|
|
|
|
@_cdecl("highlight_swift")
|
|
|
|
|
public func highlight(codePtr: UnsafeRawPointer, codeLen: UInt64, outPtr: UnsafeMutableRawPointer, maxLen: UInt64) -> UInt64 {
|
|
|
|
|
public func highlight(codePtr: UnsafeRawPointer, codeLen: UInt64, htmlLenPtr: UnsafeMutablePointer<UInt64>) -> UnsafeMutableRawPointer {
|
|
|
|
|
// don't free, the underlying data is owned by rust
|
|
|
|
|
let code = String(bytesNoCopy: UnsafeMutableRawPointer(mutating: codePtr), length: Int(codeLen), encoding: .utf8, freeWhenDone: false)!
|
|
|
|
|
|
|
|
|
|
let highligher = SyntaxHighlighter(format: MyOutputFormat())
|
|
|
|
|
var html = highligher.highlight(code)
|
|
|
|
|
precondition(html.utf8.count <= maxLen)
|
|
|
|
|
return html.withUTF8 { buf in
|
|
|
|
|
buf.copyBytes(to: UnsafeMutableRawBufferPointer(start: outPtr, count: Int(maxLen)))
|
|
|
|
|
return UInt64(buf.count)
|
|
|
|
|
let outPtr = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: html.utf8.count)
|
|
|
|
|
_ = html.withUTF8 { buf in
|
|
|
|
|
buf.copyBytes(to: outPtr, count: buf.count)
|
|
|
|
|
}
|
|
|
|
|
htmlLenPtr.pointee = UInt64(html.utf8.count)
|
|
|
|
|
return UnsafeMutableRawPointer(outPtr.baseAddress!)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct MyOutputFormat: OutputFormat {
|
|
|
|
|