fastequal.go raw

   1  package utils
   2  
   3  import "next.orly.dev/pkg/nostr/utils/constraints"
   4  
   5  func FastEqual[A constraints.Bytes, B constraints.Bytes](a A, b B) (same bool) {
   6  	if len(a) != len(b) {
   7  		return
   8  	}
   9  	ab := []byte(a)
  10  	bb := []byte(b)
  11  	for i, v := range ab {
  12  		if v != bb[i] {
  13  			return
  14  		}
  15  	}
  16  	return true
  17  }
  18  
  19  func FastCompare[A constraints.Bytes, B constraints.Bytes](
  20  	a A, b B,
  21  ) (diff int) {
  22  	if len(a) != len(b) {
  23  		return
  24  	}
  25  	ab := []byte(a)
  26  	bb := []byte(b)
  27  	for i, v := range ab {
  28  		if v != bb[i] {
  29  			if v > bb[i] {
  30  				return 1
  31  			}
  32  			return -1
  33  		}
  34  	}
  35  	return 0
  36  }
  37