ndb: add subscript and count for TagsSequence

These are helpful
This commit is contained in:
William Casarin
2023-07-22 16:58:17 -07:00
parent 1854e10486
commit c72666b352
2 changed files with 23 additions and 0 deletions

View File

@@ -40,6 +40,14 @@ struct TagIterator: IteratorProtocol {
return el
}
subscript(index: Int) -> NdbTagElem? {
if index >= tag.pointee.count {
return nil
}
return NdbTagElem(note: note, tag: tag, index: Int32(index))
}
var index: Int32
let note: NdbNote
var tag: UnsafeMutablePointer<ndb_tag>

View File

@@ -40,6 +40,21 @@ struct TagsIterator: IteratorProtocol {
struct TagsSequence: Sequence {
let note: NdbNote
var count: UInt16 {
note.note.pointee.tags.count
}
subscript(index: Int) -> Iterator.Element? {
var i = 0
for element in self {
if i == index {
return element
}
i += 1
}
return nil
}
func makeIterator() -> TagsIterator {
return .init(note: note)
}