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.

47 lines
873 B

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. }