ndb: sync up a few remaining NdbNote tag differences

This commit is contained in:
William Casarin
2023-07-25 16:22:25 -07:00
parent 2f8aa29e92
commit 593d0e2abe
14 changed files with 93 additions and 60 deletions

View File

@@ -104,7 +104,7 @@ class NdbNote: Equatable, Hashable {
static let max_note_size: Int = 2 << 18
init?(content: String, keypair: Keypair, kind: Int = 1, tags: [[String]] = [], createdAt: UInt32 = UInt32(Date().timeIntervalSince1970)) {
init?(content: String, keypair: Keypair, kind: UInt32 = 1, tags: [[String]] = [], createdAt: UInt32 = UInt32(Date().timeIntervalSince1970)) {
var builder = ndb_builder()
let buflen = MAX_NOTE_SIZE

View File

@@ -83,6 +83,19 @@ struct NdbTagElem: Sequence, Hashable {
return str.str[0] == c.cchar && str.str[1] == 0
}
func matches_str(_ s: String) -> Bool {
if str.flag == NDB_PACKED_ID,
s.utf8.count == 64,
var decoded = hex_decode(s), decoded.count == 32
{
return memcmp(&decoded, str.id, 32) == 0
}
let len = strlen(str.str)
guard len == s.utf8.count else { return false }
return s.withCString { cstr in memcmp(str.str, cstr, len) == 0 }
}
var ndbstr: ndb_str {
return ndb_tag_str(note.note, tag, index)
}

View File

@@ -15,8 +15,12 @@ struct TagSequence: Sequence {
tag.pointee.count
}
subscript(index: Int) -> NdbTagElem? {
guard index < count else { return nil }
func strings() -> [String] {
return self.map { $0.string() }
}
subscript(index: Int) -> NdbTagElem {
precondition(index < count, "Index out of bounds")
return NdbTagElem(note: note, tag: tag, index: Int32(index))
}

View File

@@ -52,7 +52,7 @@ final class NdbTests: XCTestCase {
}
if tags == 7 {
XCTAssertEqual(tag[2]?.string(), "wss://nostr-pub.wellorder.net")
XCTAssertEqual(tag[2].string(), "wss://nostr-pub.wellorder.net")
}
for elem in tag {