bytes.go 924 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package serial
  2. type Bytes interface {
  3. Bytes() []byte
  4. }
  5. type BytesLiteral []byte
  6. func (this BytesLiteral) Value() []byte {
  7. return []byte(this)
  8. }
  9. func (this BytesLiteral) Uint32Value() uint32 {
  10. value := this.Value()
  11. return uint32(value[0])<<24 +
  12. uint32(value[1])<<16 +
  13. uint32(value[2])<<8 +
  14. uint32(value[3])
  15. }
  16. func (this BytesLiteral) Int64Value() int64 {
  17. value := this.Value()
  18. return int64(value[0])<<56 +
  19. int64(value[1])<<48 +
  20. int64(value[2])<<40 +
  21. int64(value[3])<<32 +
  22. int64(value[4])<<24 +
  23. int64(value[5])<<16 +
  24. int64(value[6])<<8 +
  25. int64(value[7])
  26. }
  27. // String returns a string presentation of this ByteLiteral
  28. func (this BytesLiteral) String() string {
  29. return string(this.Value())
  30. }
  31. // All returns true if all bytes in the ByteLiteral are the same as given value.
  32. func (this BytesLiteral) All(v byte) bool {
  33. for _, b := range this {
  34. if b != v {
  35. return false
  36. }
  37. }
  38. return true
  39. }