NdbTagIterator.swift (1194B)
1 // 2 // NdbTagIterators.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-07-21. 6 // 7 8 import Foundation 9 10 struct TagSequence: Sequence { 11 let note: NdbNote 12 let tag: UnsafeMutablePointer<ndb_tag> 13 14 var count: UInt16 { 15 tag.pointee.count 16 } 17 18 func strings() -> [String] { 19 return self.map { $0.string() } 20 } 21 22 subscript(index: Int) -> NdbTagElem { 23 precondition(index < count, "Index out of bounds") 24 25 return NdbTagElem(note: note, tag: tag, index: Int32(index)) 26 } 27 28 func makeIterator() -> TagIterator { 29 return TagIterator(note: note, tag: tag) 30 } 31 } 32 33 struct TagIterator: IteratorProtocol { 34 typealias Element = NdbTagElem 35 36 mutating func next() -> NdbTagElem? { 37 guard index < tag.pointee.count else { return nil } 38 let el = NdbTagElem(note: note, tag: tag, index: index) 39 40 index += 1 41 42 return el 43 } 44 45 var index: Int32 46 let note: NdbNote 47 var tag: UnsafeMutablePointer<ndb_tag> 48 49 var count: UInt16 { 50 tag.pointee.count 51 } 52 53 init(note: NdbNote, tag: UnsafeMutablePointer<ndb_tag>) { 54 self.note = note 55 self.tag = tag 56 self.index = 0 57 } 58 }