Add some utility procedures to MultiAddress. (#94)

* Add some utility procedures to MultiAddress.

* Do not use result and add more operations to be more effective for compiler.
This commit is contained in:
Eugene Kabanov 2020-02-27 19:26:42 +02:00 committed by GitHub
parent 85d4baca95
commit 91d2f52f4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 0 deletions

View File

@ -569,6 +569,31 @@ iterator items*(ma: MultiAddress): MultiAddress =
res.data.finish()
yield res
proc contains*(ma: MultiAddress, codec: MultiCodec): bool {.inline.} =
## Returns ``true``, if address with MultiCodec ``codec`` present in
## MultiAddress ``ma``.
var res = false
for item in ma.items:
if item.protoCode() == codec:
res = true
break
result = res
proc `[]`*(ma: MultiAddress, codec: MultiCodec): MultiAddress {.inline.} =
## Returns partial MultiAddress with MultiCodec ``codec`` and present in
## MultiAddress ``ma``.
##
## Procedure will raise ``MultiAddressError`` if ``codec`` could not be
## found inside of ``ma``.
var res = MultiAddress(data: initVBuffer())
for item in ma.items:
if item.protoCode == codec:
res = item
break
if res.data.isEmpty():
raise newException(MultiAddressError, "Codec is not present in address")
result = res
proc `$`*(value: MultiAddress): string =
## Return string representation of MultiAddress ``value``.
var header: uint64