From c72666b352f655d39432b8340408a61db3231ee8 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Sat, 22 Jul 2023 16:58:17 -0700 Subject: [PATCH] ndb: add subscript and count for TagsSequence These are helpful --- nostrdb/NdbTagIterator.swift | 8 ++++++++ nostrdb/NdbTagsIterator.swift | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/nostrdb/NdbTagIterator.swift b/nostrdb/NdbTagIterator.swift index 795c698a..41f8ca9b 100644 --- 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 diff --git a/nostrdb/NdbTagsIterator.swift b/nostrdb/NdbTagsIterator.swift index 00ada3a6..4191deb4 100644 --- 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) }