Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

T3HighlightsModule.swift

29 lines1.0 KB

1import ExpoModulesCore
2​
3public final class T3HighlightsModule: Module {
4 private let lock = NSLock()
5​
6 public func definition() -> ModuleDefinition {
7 Name("T3Highlights")
8​
9 Function("tokenize") { (source: String, language: Int) -> ArrayBuffer? in
10 var source = source
11 return source.withUTF8 { utf8 in
12 self.lock.withLock { Self.tokenize(utf8, language: language) }
13 }
14 }
15 }
16​
17 private static func tokenize(_ utf8: UnsafeBufferPointer<UInt8>, language: Int) -> ArrayBuffer? {
18 guard let languageId = UInt8(exactly: language) else { return nil }
19 var records: UnsafePointer<UInt32>?
20 let count = t3_highlights_tokenize(utf8.baseAddress, utf8.count, languageId, &records)
21 guard count >= 0 else { return nil }
22 let byteCount = Int(count) * MemoryLayout<UInt32>.size
23 let buffer = ArrayBuffer(size: byteCount, initializeToZero: false)
24 if let records, byteCount > 0 {
25 buffer.withUnsafeMutableBytes { $0.copyMemory(from: UnsafeRawBufferPointer(start: records, count: byteCount)) }
26 }
27 return buffer
28 }