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