mirror of
git://jb55.com/damus
synced 2024-09-19 11:43:44 +00:00
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:
parent
af7ea7024f
commit
58e2fb40ef
@ -34,7 +34,7 @@ struct NdbNote {
|
||||
}
|
||||
|
||||
func tags() -> TagsSequence {
|
||||
return .init(note: note)
|
||||
return .init(note: self)
|
||||
}
|
||||
|
||||
static func owned_from_json(json: String, bufsize: Int = 2 << 18) -> NdbNote? {
|
||||
|
@ -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) ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,9 +8,21 @@
|
||||
import Foundation
|
||||
|
||||
struct TagSequence: Sequence {
|
||||
let note: UnsafeMutablePointer<ndb_note>
|
||||
let note: NdbNote
|
||||
let tag: UnsafeMutablePointer<ndb_tag>
|
||||
|
||||
var count: UInt16 {
|
||||
tag.pointee.count
|
||||
}
|
||||
|
||||
subscript(index: Int) -> NdbTagElem? {
|
||||
if index >= tag.pointee.count {
|
||||
return nil
|
||||
}
|
||||
|
||||
return NdbTagElem(note: note, tag: tag, index: Int32(index))
|
||||
}
|
||||
|
||||
func makeIterator() -> TagIterator {
|
||||
return TagIterator(note: note, tag: tag)
|
||||
}
|
||||
@ -29,10 +41,14 @@ struct TagIterator: IteratorProtocol {
|
||||
}
|
||||
|
||||
var index: Int32
|
||||
let note: UnsafeMutablePointer<ndb_note>
|
||||
let note: NdbNote
|
||||
var tag: UnsafeMutablePointer<ndb_tag>
|
||||
|
||||
init(note: UnsafeMutablePointer<ndb_note>, tag: UnsafeMutablePointer<ndb_tag>) {
|
||||
var count: UInt16 {
|
||||
tag.pointee.count
|
||||
}
|
||||
|
||||
init(note: NdbNote, tag: UnsafeMutablePointer<ndb_tag>) {
|
||||
self.note = note
|
||||
self.tag = tag
|
||||
self.index = 0
|
||||
|
@ -12,11 +12,12 @@ struct TagsIterator: IteratorProtocol {
|
||||
|
||||
var done: Bool
|
||||
var iter: ndb_iterator
|
||||
var note: NdbNote
|
||||
|
||||
mutating func next() -> TagSequence? {
|
||||
guard !done else { return nil }
|
||||
|
||||
let tag_seq = TagSequence(note: iter.note, tag: self.iter.tag)
|
||||
let tag_seq = TagSequence(note: note, tag: self.iter.tag)
|
||||
|
||||
let ok = ndb_tags_iterate_next(&self.iter)
|
||||
done = ok == 0
|
||||
@ -24,15 +25,20 @@ struct TagsIterator: IteratorProtocol {
|
||||
return tag_seq
|
||||
}
|
||||
|
||||
init(note: UnsafeMutablePointer<ndb_note>) {
|
||||
var count: UInt16 {
|
||||
return iter.tag.pointee.count
|
||||
}
|
||||
|
||||
init(note: NdbNote) {
|
||||
self.iter = ndb_iterator()
|
||||
let res = ndb_tags_iterate_start(note, &self.iter)
|
||||
let res = ndb_tags_iterate_start(note.note, &self.iter)
|
||||
self.done = res == 0
|
||||
self.note = note
|
||||
}
|
||||
}
|
||||
|
||||
struct TagsSequence: Sequence {
|
||||
let note: UnsafeMutablePointer<ndb_note>
|
||||
let note: NdbNote
|
||||
|
||||
func makeIterator() -> TagsIterator {
|
||||
return .init(note: note)
|
||||
|
Loading…
Reference in New Issue
Block a user