mpt: refactor lcp to be possible to use range

It took some time to understand why changing a regular `for` to a `range`
one leads to behavior changes; let it be more clear and explicit. Also, a
correct code is always better than a correct code with `nolint`.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2024-09-04 04:09:43 +03:00
parent 32f91dd726
commit f8549a4fb8

View file

@ -13,17 +13,13 @@ func lcp(a, b []byte) []byte {
return lcp(b, a) return lcp(b, a)
} }
var i int for i := range b {
//nolint:intrange // if slices are the same (or one is a prefix for another
// one), `range` loop does not assign the latest index to the `i` var, and
// the func loses the latest element
for i = 0; i < len(b); i++ {
if a[i] != b[i] { if a[i] != b[i] {
break return b[:i]
} }
} }
return a[:i] return b
} }
func lcpMany(kv []keyValue) []byte { func lcpMany(kv []keyValue) []byte {