int.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package nlenc
  2. import (
  3. "fmt"
  4. "unsafe"
  5. )
  6. // PutUint8 encodes a uint8 into b.
  7. // If b is not exactly 1 byte in length, PutUint8 will panic.
  8. func PutUint8(b []byte, v uint8) {
  9. if l := len(b); l != 1 {
  10. panic(fmt.Sprintf("PutUint8: unexpected byte slice length: %d", l))
  11. }
  12. b[0] = v
  13. }
  14. // PutUint16 encodes a uint16 into b using the host machine's native endianness.
  15. // If b is not exactly 2 bytes in length, PutUint16 will panic.
  16. func PutUint16(b []byte, v uint16) {
  17. if l := len(b); l != 2 {
  18. panic(fmt.Sprintf("PutUint16: unexpected byte slice length: %d", l))
  19. }
  20. *(*uint16)(unsafe.Pointer(&b[0])) = v
  21. }
  22. // PutUint32 encodes a uint32 into b using the host machine's native endianness.
  23. // If b is not exactly 4 bytes in length, PutUint32 will panic.
  24. func PutUint32(b []byte, v uint32) {
  25. if l := len(b); l != 4 {
  26. panic(fmt.Sprintf("PutUint32: unexpected byte slice length: %d", l))
  27. }
  28. *(*uint32)(unsafe.Pointer(&b[0])) = v
  29. }
  30. // PutUint64 encodes a uint64 into b using the host machine's native endianness.
  31. // If b is not exactly 8 bytes in length, PutUint64 will panic.
  32. func PutUint64(b []byte, v uint64) {
  33. if l := len(b); l != 8 {
  34. panic(fmt.Sprintf("PutUint64: unexpected byte slice length: %d", l))
  35. }
  36. *(*uint64)(unsafe.Pointer(&b[0])) = v
  37. }
  38. // PutInt32 encodes a int32 into b using the host machine's native endianness.
  39. // If b is not exactly 4 bytes in length, PutInt32 will panic.
  40. func PutInt32(b []byte, v int32) {
  41. if l := len(b); l != 4 {
  42. panic(fmt.Sprintf("PutInt32: unexpected byte slice length: %d", l))
  43. }
  44. *(*int32)(unsafe.Pointer(&b[0])) = v
  45. }
  46. // Uint8 decodes a uint8 from b.
  47. // If b is not exactly 1 byte in length, Uint8 will panic.
  48. func Uint8(b []byte) uint8 {
  49. if l := len(b); l != 1 {
  50. panic(fmt.Sprintf("Uint8: unexpected byte slice length: %d", l))
  51. }
  52. return b[0]
  53. }
  54. // Uint16 decodes a uint16 from b using the host machine's native endianness.
  55. // If b is not exactly 2 bytes in length, Uint16 will panic.
  56. func Uint16(b []byte) uint16 {
  57. if l := len(b); l != 2 {
  58. panic(fmt.Sprintf("Uint16: unexpected byte slice length: %d", l))
  59. }
  60. return *(*uint16)(unsafe.Pointer(&b[0]))
  61. }
  62. // Uint32 decodes a uint32 from b using the host machine's native endianness.
  63. // If b is not exactly 4 bytes in length, Uint32 will panic.
  64. func Uint32(b []byte) uint32 {
  65. if l := len(b); l != 4 {
  66. panic(fmt.Sprintf("Uint32: unexpected byte slice length: %d", l))
  67. }
  68. return *(*uint32)(unsafe.Pointer(&b[0]))
  69. }
  70. // Uint64 decodes a uint64 from b using the host machine's native endianness.
  71. // If b is not exactly 8 bytes in length, Uint64 will panic.
  72. func Uint64(b []byte) uint64 {
  73. if l := len(b); l != 8 {
  74. panic(fmt.Sprintf("Uint64: unexpected byte slice length: %d", l))
  75. }
  76. return *(*uint64)(unsafe.Pointer(&b[0]))
  77. }
  78. // Int32 decodes an int32 from b using the host machine's native endianness.
  79. // If b is not exactly 4 bytes in length, Int32 will panic.
  80. func Int32(b []byte) int32 {
  81. if l := len(b); l != 4 {
  82. panic(fmt.Sprintf("Int32: unexpected byte slice length: %d", l))
  83. }
  84. return *(*int32)(unsafe.Pointer(&b[0]))
  85. }
  86. // Uint8Bytes encodes a uint8 into a newly-allocated byte slice. It is a
  87. // shortcut for allocating a new byte slice and filling it using PutUint8.
  88. func Uint8Bytes(v uint8) []byte {
  89. b := make([]byte, 1)
  90. PutUint8(b, v)
  91. return b
  92. }
  93. // Uint16Bytes encodes a uint16 into a newly-allocated byte slice using the
  94. // host machine's native endianness. It is a shortcut for allocating a new
  95. // byte slice and filling it using PutUint16.
  96. func Uint16Bytes(v uint16) []byte {
  97. b := make([]byte, 2)
  98. PutUint16(b, v)
  99. return b
  100. }
  101. // Uint32Bytes encodes a uint32 into a newly-allocated byte slice using the
  102. // host machine's native endianness. It is a shortcut for allocating a new
  103. // byte slice and filling it using PutUint32.
  104. func Uint32Bytes(v uint32) []byte {
  105. b := make([]byte, 4)
  106. PutUint32(b, v)
  107. return b
  108. }
  109. // Uint64Bytes encodes a uint64 into a newly-allocated byte slice using the
  110. // host machine's native endianness. It is a shortcut for allocating a new
  111. // byte slice and filling it using PutUint64.
  112. func Uint64Bytes(v uint64) []byte {
  113. b := make([]byte, 8)
  114. PutUint64(b, v)
  115. return b
  116. }
  117. // Int32Bytes encodes a int32 into a newly-allocated byte slice using the
  118. // host machine's native endianness. It is a shortcut for allocating a new
  119. // byte slice and filling it using PutInt32.
  120. func Int32Bytes(v int32) []byte {
  121. b := make([]byte, 4)
  122. PutInt32(b, v)
  123. return b
  124. }