-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from snofla/feature/uuid-to-uint128-vice-versa
Allow conversions UUID <-> UInt128
- Loading branch information
Showing
5 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// UInt128+UUID.swift | ||
// UInt128 | ||
// | ||
// Created by Alfons Hoogervorst on 15/08/2023. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
|
||
extension UInt128 { | ||
|
||
/// Creates a UInt128 from a UUID. Since a UUID is always 128 bits, | ||
/// no truncation is needed. | ||
public init(uuid: UUID) { | ||
let val = uuid.uuid | ||
let lowerBits: UInt64 = UInt64(val.0) << (0 * 8) | | ||
UInt64(val.1) << (1 * 8) | | ||
UInt64(val.2) << (2 * 8) | | ||
UInt64(val.3) << (3 * 8) | | ||
UInt64(val.4) << (4 * 8) | | ||
UInt64(val.5) << (5 * 8) | | ||
UInt64(val.6) << (6 * 8) | | ||
UInt64(val.7) << (7 * 8) | ||
let upperBits: UInt64 = UInt64(val.8) << (0 * 8) | | ||
UInt64(val.9) << (1 * 8) | | ||
UInt64(val.10) << (2 * 8) | | ||
UInt64(val.11) << (3 * 8) | | ||
UInt64(val.12) << (4 * 8) | | ||
UInt64(val.13) << (5 * 8) | | ||
UInt64(val.14) << (6 * 8) | | ||
UInt64(val.15) << (7 * 8) | ||
self = .init(upperBits: upperBits, lowerBits: lowerBits) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// UUID+UInt128.swift | ||
// UInt128 | ||
// | ||
// Created by Alfons Hoogervorst on 15/08/2023. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
|
||
|
||
extension UUID { | ||
|
||
/// Creates a UUID representation of a UInt128. Since a UUID is always | ||
/// 128 bits, no truncation occurs. | ||
public init(_ longInt: UInt128) { | ||
let lowerBits = longInt.value.lowerBits | ||
let upperBits = longInt.value.upperBits | ||
let val: uuid_t = ( | ||
UInt8((lowerBits >> (0 * 8)) & 0xFF), | ||
UInt8((lowerBits >> (1 * 8)) & 0xFF), | ||
UInt8((lowerBits >> (2 * 8)) & 0xFF), | ||
UInt8((lowerBits >> (3 * 8)) & 0xFF), | ||
UInt8((lowerBits >> (4 * 8)) & 0xFF), | ||
UInt8((lowerBits >> (5 * 8)) & 0xFF), | ||
UInt8((lowerBits >> (6 * 8)) & 0xFF), | ||
UInt8((lowerBits >> (7 * 8)) & 0xFF), | ||
UInt8((upperBits >> (0 * 8)) & 0xFF), | ||
UInt8((upperBits >> (1 * 8)) & 0xFF), | ||
UInt8((upperBits >> (2 * 8)) & 0xFF), | ||
UInt8((upperBits >> (3 * 8)) & 0xFF), | ||
UInt8((upperBits >> (4 * 8)) & 0xFF), | ||
UInt8((upperBits >> (5 * 8)) & 0xFF), | ||
UInt8((upperBits >> (6 * 8)) & 0xFF), | ||
UInt8((upperBits >> (7 * 8)) & 0xFF) | ||
) | ||
self = .init(uuid: val) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters