Varint.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Sources/SwiftProtobuf/Varint.swift - Varint encoding/decoding helpers
  2. //
  3. // Copyright (c) 2014 - 2016 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// Helper functions to varint-encode and decode integers.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. /// Contains helper methods to varint-encode and decode integers.
  15. internal enum Varint {
  16. /// Computes the number of bytes that would be needed to store a 32-bit varint.
  17. ///
  18. /// - Parameter value: The number whose varint size should be calculated.
  19. /// - Returns: The size, in bytes, of the 32-bit varint.
  20. static func encodedSize(of value: UInt32) -> Int {
  21. if (value & (~0 << 7)) == 0 {
  22. return 1
  23. }
  24. if (value & (~0 << 14)) == 0 {
  25. return 2
  26. }
  27. if (value & (~0 << 21)) == 0 {
  28. return 3
  29. }
  30. if (value & (~0 << 28)) == 0 {
  31. return 4
  32. }
  33. return 5
  34. }
  35. /// Computes the number of bytes that would be needed to store a signed 32-bit varint, if it were
  36. /// treated as an unsigned integer with the same bit pattern.
  37. ///
  38. /// - Parameter value: The number whose varint size should be calculated.
  39. /// - Returns: The size, in bytes, of the 32-bit varint.
  40. static func encodedSize(of value: Int32) -> Int {
  41. if value >= 0 {
  42. return encodedSize(of: UInt32(bitPattern: value))
  43. } else {
  44. // Must sign-extend.
  45. return encodedSize(of: Int64(value))
  46. }
  47. }
  48. /// Computes the number of bytes that would be needed to store a 64-bit varint.
  49. ///
  50. /// - Parameter value: The number whose varint size should be calculated.
  51. /// - Returns: The size, in bytes, of the 64-bit varint.
  52. static func encodedSize(of value: Int64) -> Int {
  53. // Handle two common special cases up front.
  54. if (value & (~0 << 7)) == 0 {
  55. return 1
  56. }
  57. if value < 0 {
  58. return 10
  59. }
  60. // Divide and conquer the remaining eight cases.
  61. var value = value
  62. var n = 2
  63. if (value & (~0 << 35)) != 0 {
  64. n += 4
  65. value >>= 28
  66. }
  67. if (value & (~0 << 21)) != 0 {
  68. n += 2
  69. value >>= 14
  70. }
  71. if (value & (~0 << 14)) != 0 {
  72. n += 1
  73. }
  74. return n
  75. }
  76. /// Computes the number of bytes that would be needed to store an unsigned 64-bit varint, if it
  77. /// were treated as a signed integer with the same bit pattern.
  78. ///
  79. /// - Parameter value: The number whose varint size should be calculated.
  80. /// - Returns: The size, in bytes, of the 64-bit varint.
  81. static func encodedSize(of value: UInt64) -> Int {
  82. encodedSize(of: Int64(bitPattern: value))
  83. }
  84. /// Counts the number of distinct varints in a packed byte buffer.
  85. static func countVarintsInBuffer(start: UnsafeRawPointer, count: Int) -> Int {
  86. // We don't need to decode all the varints to count how many there
  87. // are. Just observe that every varint has exactly one byte with
  88. // value < 128. So we just count those...
  89. var n = 0
  90. var ints = 0
  91. while n < count {
  92. if start.load(fromByteOffset: n, as: UInt8.self) < 128 {
  93. ints += 1
  94. }
  95. n += 1
  96. }
  97. return ints
  98. }
  99. }