-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
35 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,54 @@ | ||
import Foundation | ||
|
||
enum Base16 { | ||
enum DecodingResult: Equatable, Sendable { | ||
case emptyInput | ||
case error | ||
case hexValue(UInt8) | ||
} | ||
|
||
/// Decodes hex character into 4-bit data. | ||
static func decode<I: IteratorProtocol>(_ input: inout I) -> DecodingResult where I.Element == UInt8 { | ||
func hex(from char: UInt8) -> UInt8? { | ||
if 0x30 <= char, char <= 0x39 { // '0'-'9' | ||
return char - 0x30 | ||
} else if 0x41 <= char, char <= 0x46 { // 'A'-'F' | ||
return char - 0x41 + 10 | ||
} else if 0x61 <= char, char <= 0x66 { // 'a'-'f' | ||
return char - 0x61 + 10 | ||
} | ||
return nil | ||
} | ||
|
||
guard let next0 = input.next() else { | ||
return .emptyInput | ||
} | ||
guard let next1 = input.next() else { | ||
return .error | ||
} | ||
|
||
guard let upper = hex(from: next0), let lower = hex(from: next1) else { | ||
return .error | ||
} | ||
|
||
return .hexValue((upper << 4) | lower) | ||
} | ||
|
||
/// Encodes 8-bit data into hex characters. | ||
static func encode(_ input: UInt8, into process: ((upper: UInt8, lower: UInt8)) -> Void) { | ||
func char(from hex: UInt8) -> UInt8 { | ||
if 0 <= hex, hex <= 9 { | ||
return 0x30 + hex // '0'-'9' | ||
} else { | ||
return 0x61 + hex - 10 // 'a'-'f' | ||
} | ||
} | ||
|
||
process( | ||
( | ||
upper: char(from: input >> 4), | ||
lower: char(from: input & 0b1111) | ||
) | ||
) | ||
} | ||
} |
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
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