iter: make safer by using NdbNote instead of unsafe pointers

If we have an owned note, we could lose track of the lifetime and then
crash. Let's make sure we always have an NdbNote instead
This commit is contained in:
William Casarin
2023-07-22 16:56:13 -07:00
parent af7ea7024f
commit 58e2fb40ef
4 changed files with 35 additions and 13 deletions

View File

@@ -8,22 +8,22 @@
import Foundation
struct NdbTagElem {
private let note: UnsafeMutablePointer<ndb_note>
private let note: NdbNote
private let tag: UnsafeMutablePointer<ndb_tag>
let index: Int32
init(note: UnsafeMutablePointer<ndb_note>, tag: UnsafeMutablePointer<ndb_tag>, index: Int32) {
init(note: NdbNote, tag: UnsafeMutablePointer<ndb_tag>, index: Int32) {
self.note = note
self.tag = tag
self.index = index
}
func matches_char(c: AsciiCharacter) -> Bool {
return ndb_tag_matches_char(note, tag, index, c.cchar) == 1
func matches_char(_ c: AsciiCharacter) -> Bool {
return ndb_tag_matches_char(note.note, tag, index, c.cchar) == 1
}
func string() -> String {
return String(cString: ndb_tag_str(note, tag, index), encoding: .utf8) ?? ""
return String(cString: ndb_tag_str(note.note, tag, index), encoding: .utf8) ?? ""
}
}