Don't use no copy string initializer
This commit is contained in:
parent
798644bf8b
commit
a20339a1f2
|
@ -2,10 +2,11 @@ import Foundation
|
||||||
import Splash
|
import Splash
|
||||||
|
|
||||||
@_cdecl("highlight_swift")
|
@_cdecl("highlight_swift")
|
||||||
public func highlight(codePtr: UnsafeRawPointer, codeLen: UInt64, htmlLenPtr: UnsafeMutablePointer<UInt64>) -> UnsafeMutableRawPointer {
|
public func highlight(codePtr: UnsafePointer<UInt8>, codeLen: UInt64, htmlLenPtr: UnsafeMutablePointer<UInt64>) -> UnsafeMutableRawPointer {
|
||||||
// don't free, the underlying data is owned by rust
|
let buf = UnsafeBufferPointer(start: codePtr, count: Int(codeLen))
|
||||||
let code = String(bytesNoCopy: UnsafeMutableRawPointer(mutating: codePtr), length: Int(codeLen), encoding: .utf8, freeWhenDone: false)!
|
let data = Data(buffer: buf)
|
||||||
|
let code = String(data: data, encoding: .utf8)!
|
||||||
|
|
||||||
let highligher = SyntaxHighlighter(format: MyOutputFormat())
|
let highligher = SyntaxHighlighter(format: MyOutputFormat())
|
||||||
var html = highligher.highlight(code)
|
var html = highligher.highlight(code)
|
||||||
let outPtr = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: html.utf8.count)
|
let outPtr = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: html.utf8.count)
|
||||||
|
|
|
@ -18,7 +18,7 @@ class HighlightTests: XCTestCase {
|
||||||
var len: UInt64 = 0
|
var len: UInt64 = 0
|
||||||
let result = code.withUTF8 { codePtr in
|
let result = code.withUTF8 { codePtr in
|
||||||
withUnsafeMutablePointer(to: &len) { lenPtr 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))
|
let start = UnsafePointer(result.assumingMemoryBound(to: UInt8.self))
|
||||||
|
|
Loading…
Reference in New Issue