ZigZag.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Sources/SwiftProtobuf/ZigZag.swift - ZigZag 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 ZigZag encode and decode signed integers.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. /// Contains helper methods to ZigZag encode and decode signed integers.
  15. internal enum ZigZag {
  16. /// Return a 32-bit ZigZag-encoded value.
  17. ///
  18. /// ZigZag encodes signed integers into values that can be efficiently encoded with varint.
  19. /// (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, always
  20. /// taking 10 bytes on the wire.)
  21. ///
  22. /// - Parameter value: A signed 32-bit integer.
  23. /// - Returns: An unsigned 32-bit integer representing the ZigZag-encoded value.
  24. static func encoded(_ value: Int32) -> UInt32 {
  25. UInt32(bitPattern: (value << 1) ^ (value >> 31))
  26. }
  27. /// Return a 64-bit ZigZag-encoded value.
  28. ///
  29. /// ZigZag encodes signed integers into values that can be efficiently encoded with varint.
  30. /// (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, always
  31. /// taking 10 bytes on the wire.)
  32. ///
  33. /// - Parameter value: A signed 64-bit integer.
  34. /// - Returns: An unsigned 64-bit integer representing the ZigZag-encoded value.
  35. static func encoded(_ value: Int64) -> UInt64 {
  36. UInt64(bitPattern: (value << 1) ^ (value >> 63))
  37. }
  38. /// Return a 32-bit ZigZag-decoded value.
  39. ///
  40. /// ZigZag enocdes signed integers into values that can be efficiently encoded with varint.
  41. /// (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, always
  42. /// taking 10 bytes on the wire.)
  43. ///
  44. /// - Parameter value: An unsigned 32-bit ZagZag-encoded integer.
  45. /// - Returns: The signed 32-bit decoded value.
  46. static func decoded(_ value: UInt32) -> Int32 {
  47. Int32(value >> 1) ^ -Int32(value & 1)
  48. }
  49. /// Return a 64-bit ZigZag-decoded value.
  50. ///
  51. /// ZigZag enocdes signed integers into values that can be efficiently encoded with varint.
  52. /// (Otherwise, negative values must be sign-extended to 64 bits to be varint encoded, always
  53. /// taking 10 bytes on the wire.)
  54. ///
  55. /// - Parameter value: An unsigned 64-bit ZigZag-encoded integer.
  56. /// - Returns: The signed 64-bit decoded value.
  57. static func decoded(_ value: UInt64) -> Int64 {
  58. Int64(value >> 1) ^ -Int64(value & 1)
  59. }
  60. }