slice.go 758 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package trie
  2. import (
  3. "bytes"
  4. "math"
  5. )
  6. // Helper function for comparing slices
  7. func CompareIntSlice(a, b []int) bool {
  8. if len(a) != len(b) {
  9. return false
  10. }
  11. for i, v := range a {
  12. if v != b[i] {
  13. return false
  14. }
  15. }
  16. return true
  17. }
  18. // Returns the amount of nibbles that match each other from 0 ...
  19. func MatchingNibbleLength(a, b []byte) int {
  20. var i, length = 0, int(math.Min(float64(len(a)), float64(len(b))))
  21. for i < length {
  22. if a[i] != b[i] {
  23. break
  24. }
  25. i++
  26. }
  27. return i
  28. }
  29. func HasTerm(s []byte) bool {
  30. return s[len(s)-1] == 16
  31. }
  32. func RemTerm(s []byte) []byte {
  33. if HasTerm(s) {
  34. return s[:len(s)-1]
  35. }
  36. return s
  37. }
  38. func BeginsWith(a, b []byte) bool {
  39. if len(b) > len(a) {
  40. return false
  41. }
  42. return bytes.Equal(a[:len(b)], b)
  43. }