leaf_alts.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2021 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Stuff that exists in std, but we can't use due to being a dependency
  5. // of net, for go/build deps_test policy reasons.
  6. package netip
  7. func stringsLastIndexByte(s string, b byte) int {
  8. for i := len(s) - 1; i >= 0; i-- {
  9. if s[i] == b {
  10. return i
  11. }
  12. }
  13. return -1
  14. }
  15. func beUint64(b []byte) uint64 {
  16. _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
  17. return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
  18. uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
  19. }
  20. func bePutUint64(b []byte, v uint64) {
  21. _ = b[7] // early bounds check to guarantee safety of writes below
  22. b[0] = byte(v >> 56)
  23. b[1] = byte(v >> 48)
  24. b[2] = byte(v >> 40)
  25. b[3] = byte(v >> 32)
  26. b[4] = byte(v >> 24)
  27. b[5] = byte(v >> 16)
  28. b[6] = byte(v >> 8)
  29. b[7] = byte(v)
  30. }
  31. func bePutUint32(b []byte, v uint32) {
  32. _ = b[3] // early bounds check to guarantee safety of writes below
  33. b[0] = byte(v >> 24)
  34. b[1] = byte(v >> 16)
  35. b[2] = byte(v >> 8)
  36. b[3] = byte(v)
  37. }
  38. func leUint16(b []byte) uint16 {
  39. _ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
  40. return uint16(b[0]) | uint16(b[1])<<8
  41. }
  42. func lePutUint16(b []byte, v uint16) {
  43. _ = b[1] // early bounds check to guarantee safety of writes below
  44. b[0] = byte(v)
  45. b[1] = byte(v >> 8)
  46. }