damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit c72666b352f655d39432b8340408a61db3231ee8
parent 1854e104861dcb10b241614913b6ea5e8d09d2e4
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 22 Jul 2023 16:58:17 -0700

ndb: add subscript and count for TagsSequence

These are helpful

Diffstat:
Mnostrdb/NdbTagIterator.swift | 8++++++++
Mnostrdb/NdbTagsIterator.swift | 15+++++++++++++++
2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/nostrdb/NdbTagIterator.swift b/nostrdb/NdbTagIterator.swift @@ -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> diff --git a/nostrdb/NdbTagsIterator.swift b/nostrdb/NdbTagsIterator.swift @@ -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) }