critbits: don't rely on terminating zero

This commit is contained in:
Andreas Rumpf
2018-04-29 07:42:47 +02:00
parent b899713832
commit c8b2e65dbb

View File

@@ -74,8 +74,9 @@ proc rawInsert[T](c: var CritBitTree[T], key: string): Node[T] =
var newByte = 0
block blockX:
while newbyte < key.len:
if newbyte >= it.key.len or it.key[newbyte] != key[newbyte]:
newotherbits = it.key[newbyte].ord xor key[newbyte].ord
let ch = if newbyte < it.key.len: it.key[newbyte] else: '\0'
if ch != key[newbyte]:
newotherbits = ch.ord xor key[newbyte].ord
break blockX
inc newbyte
if newbyte < it.key.len:
@@ -85,7 +86,7 @@ proc rawInsert[T](c: var CritBitTree[T], key: string): Node[T] =
while (newOtherBits and (newOtherBits-1)) != 0:
newOtherBits = newOtherBits and (newOtherBits-1)
newOtherBits = newOtherBits xor 255
let ch = it.key[newByte]
let ch = if newByte < it.key.len: it.key[newByte] else: '\0'
let dir = (1 + (ord(ch) or newOtherBits)) shr 8
var inner: Node[T]