From 1adc5643aa898495dd24c40cc822549c3a58a3ff Mon Sep 17 00:00:00 2001 From: lionsoul2014 Date: Fri, 5 Dec 2025 18:50:46 +0800 Subject: [PATCH] Add IP Add Sub funcs --- binding/golang/xdb/util.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/binding/golang/xdb/util.go b/binding/golang/xdb/util.go index 50eaf92..4b48113 100644 --- a/binding/golang/xdb/util.go +++ b/binding/golang/xdb/util.go @@ -55,6 +55,33 @@ func IPCompare(ip1, ip2 []byte) int { return bytes.Compare(ip1, ip2) } +func IPAddOne(ip []byte) []byte { + var r = make([]byte, len(ip)) + copy(r, ip) + for i := len(ip) - 1; i >= 0; i-- { + r[i]++ + if r[i] != 0 { // No overflow + break + } + } + + return r +} + +func IPSubOne(ip []byte) []byte { + var r = make([]byte, len(ip)) + copy(r, ip) + for i := len(ip) - 1; i >= 0; i-- { + if r[i] != 0 { // No borrow needed + r[i]-- + break + } + r[i] = 0xFF // borrow from the next byte + } + + return r +} + // Verify if the current Searcher could be used to search the specified xdb file. // Why do we need this check ? // The future features of the xdb impl may cause the current searcher not able to work properly.