From 50562b515a771cfc443557ee8e2dceee59207d52 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Wed, 29 Jan 2020 02:42:50 +0200 Subject: [PATCH] Add bitseqs.bitsLen(openarray) --- stew/bitseqs.nim | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stew/bitseqs.nim b/stew/bitseqs.nim index 3f5dd42..3092d68 100644 --- a/stew/bitseqs.nim +++ b/stew/bitseqs.nim @@ -24,13 +24,16 @@ type BitArray*[bits: static int] = object bytes*: array[(bits + 7) div 8, byte] -func len*(s: BitSeq): int = +func bitsLen*(bytes: openarray[byte]): int = let - bytesCount = s.Bytes.len - lastByte = s.Bytes[bytesCount - 1] + bytesCount = bytes.len + lastByte = bytes[bytesCount - 1] markerPos = log2trunc(lastByte) - Bytes(s).len * 8 - (8 - markerPos) + bytesCount * 8 - (8 - markerPos) + +template len*(s: BitSeq): int = + bitsLen(Bytes s) template len*(a: BitArray): int = a.bits