Don't use no copy string initializer

This commit is contained in:
Shadowfacts 2022-12-10 15:42:28 -05:00
parent 798644bf8b
commit a20339a1f2
2 changed files with 6 additions and 5 deletions

View File

@ -2,9 +2,10 @@ import Foundation
import Splash
@_cdecl("highlight_swift")
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)!
public func highlight(codePtr: UnsafePointer<UInt8>, codeLen: UInt64, htmlLenPtr: UnsafeMutablePointer<UInt64>) -> UnsafeMutableRawPointer {
let buf = UnsafeBufferPointer(start: codePtr, count: Int(codeLen))
let data = Data(buffer: buf)
let code = String(data: data, encoding: .utf8)!
let highligher = SyntaxHighlighter(format: MyOutputFormat())
var html = highligher.highlight(code)

View File

@ -18,7 +18,7 @@ class HighlightTests: XCTestCase {
var len: UInt64 = 0
let result = code.withUTF8 { codePtr in
withUnsafeMutablePointer(to: &len) { lenPtr in
highlight(codePtr: UnsafeRawPointer(codePtr.baseAddress!), codeLen: UInt64(codePtr.count), htmlLenPtr: lenPtr)
highlight(codePtr: codePtr.baseAddress!, codeLen: UInt64(codePtr.count), htmlLenPtr: lenPtr)
}
}
let start = UnsafePointer(result.assumingMemoryBound(to: UInt8.self))