-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathByteSwap.swift
72 lines (65 loc) · 2.18 KB
/
ByteSwap.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
/// A Bluetooth value that is stored in the CPU native endianess format.
public protocol ByteSwap {
/// A representation of this integer with the byte order swapped.
var byteSwapped: Self { get }
}
public extension ByteSwap {
/// Creates an instance from its little-endian representation, changing the
/// byte order if necessary.
///
/// - Parameter value: A value to use as the little-endian representation of
/// the new instance.
init(littleEndian value: Self) {
#if _endian(little)
self = value
#else
self = value.byteSwapped
#endif
}
/// Creates an instance from its big-endian representation, changing the byte
/// order if necessary.
///
/// - Parameter value: A value to use as the big-endian representation of the
/// new instance.
init(bigEndian value: Self) {
#if _endian(big)
self = value
#else
self = value.byteSwapped
#endif
}
/// The little-endian representation of this value.
///
/// If necessary, the byte order of this value is reversed from the typical
/// byte order of this address. On a little-endian platform, for any
/// address `x`, `x == x.littleEndian`.
var littleEndian: Self {
#if _endian(little)
return self
#else
return byteSwapped
#endif
}
/// The big-endian representation of this value.
///
/// If necessary, the byte order of this value is reversed from the typical
/// byte order of this address. On a big-endian platform, for any
/// address `x`, `x == x.bigEndian`.
var bigEndian: Self {
#if _endian(big)
return self
#else
return byteSwapped
#endif
}
}