You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.3 KiB

9 years ago
9 years ago
  1. package common
  2. import (
  3. "bytes"
  4. )
  5. // Fingerprint returns the first 6 bytes of a byte slice.
  6. // If the slice is less than 6 bytes, the fingerprint
  7. // contains trailing zeroes.
  8. func Fingerprint(slice []byte) []byte {
  9. fingerprint := make([]byte, 6)
  10. copy(fingerprint, slice)
  11. return fingerprint
  12. }
  13. func IsZeros(slice []byte) bool {
  14. for _, byt := range slice {
  15. if byt != byte(0) {
  16. return false
  17. }
  18. }
  19. return true
  20. }
  21. func RightPadBytes(slice []byte, l int) []byte {
  22. if l < len(slice) {
  23. return slice
  24. }
  25. padded := make([]byte, l)
  26. copy(padded[0:len(slice)], slice)
  27. return padded
  28. }
  29. func LeftPadBytes(slice []byte, l int) []byte {
  30. if l < len(slice) {
  31. return slice
  32. }
  33. padded := make([]byte, l)
  34. copy(padded[l-len(slice):], slice)
  35. return padded
  36. }
  37. func TrimmedString(b []byte) string {
  38. trimSet := string([]byte{0})
  39. return string(bytes.TrimLeft(b, trimSet))
  40. }
  41. // PrefixEndBytes returns the end byteslice for a noninclusive range
  42. // that would include all byte slices for which the input is the prefix
  43. func PrefixEndBytes(prefix []byte) []byte {
  44. if prefix == nil {
  45. return nil
  46. }
  47. end := make([]byte, len(prefix))
  48. copy(end, prefix)
  49. finished := false
  50. for !finished {
  51. if end[len(end)-1] != byte(255) {
  52. end[len(end)-1]++
  53. finished = true
  54. } else {
  55. end = end[:len(end)-1]
  56. if len(end) == 0 {
  57. end = nil
  58. finished = true
  59. }
  60. }
  61. }
  62. return end
  63. }