|
| 1 | +/** |
| 2 | + * Module : Hex.mo |
| 3 | + * Description : Hexadecimal encoding and decoding routines. |
| 4 | + * Copyright : 2020 Enzo Haussecker |
| 5 | + * License : Apache 2.0 with LLVM Exception |
| 6 | + * Maintainer : Enzo Haussecker <enzo@dfinity.org> |
| 7 | + * Stability : Stable |
| 8 | + */ |
| 9 | + |
| 10 | +import Array "mo:base/Array"; |
| 11 | +import Iter "mo:base/Iter"; |
| 12 | +import Option "mo:base/Option"; |
| 13 | +import Nat8 "mo:base/Nat8"; |
| 14 | +import Char "mo:base/Char"; |
| 15 | +import Result "mo:base/Result"; |
| 16 | + |
| 17 | +module { |
| 18 | + |
| 19 | + private type Result<Ok, Err> = Result.Result<Ok, Err>; |
| 20 | + |
| 21 | + private let base : Nat8 = 0x10; |
| 22 | + |
| 23 | + private let symbols = [ |
| 24 | + '0', '1', '2', '3', '4', '5', '6', '7', |
| 25 | + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', |
| 26 | + ]; |
| 27 | + |
| 28 | + /** |
| 29 | + * Define a type to indicate that the decoder has failed. |
| 30 | + */ |
| 31 | + public type DecodeError = { |
| 32 | + #msg : Text; |
| 33 | + }; |
| 34 | + |
| 35 | + /** |
| 36 | + * Encode an array of unsigned 8-bit integers in hexadecimal format. |
| 37 | + */ |
| 38 | + public func encode(array : [Nat8]) : Text { |
| 39 | + Array.foldLeft<Nat8, Text>(array, "", func (accum, w8) { |
| 40 | + accum # encodeW8(w8); |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + /** |
| 45 | + * Encode an unsigned 8-bit integer in hexadecimal format. |
| 46 | + */ |
| 47 | + private func encodeW8(w8 : Nat8) : Text { |
| 48 | + let c1 = symbols[Nat8.toNat(w8 / base)]; |
| 49 | + let c2 = symbols[Nat8.toNat(w8 % base)]; |
| 50 | + Char.toText(c1) # Char.toText(c2); |
| 51 | + }; |
| 52 | + |
| 53 | + /** |
| 54 | + * Decode an array of unsigned 8-bit integers in hexadecimal format. |
| 55 | + */ |
| 56 | + public func decode(text : Text) : Result<[Nat8], DecodeError> { |
| 57 | + let next = text.chars().next; |
| 58 | + func parse() : Result<Nat8, DecodeError> { |
| 59 | + Option.get<Result<Nat8, DecodeError>>( |
| 60 | + do ? { |
| 61 | + let c1 = next()!; |
| 62 | + let c2 = next()!; |
| 63 | + Result.chain<Nat8, Nat8, DecodeError>(decodeW4(c1), func (x1) { |
| 64 | + Result.chain<Nat8, Nat8, DecodeError>(decodeW4(c2), func (x2) { |
| 65 | + #ok (x1 * base + x2); |
| 66 | + }) |
| 67 | + }) |
| 68 | + }, |
| 69 | + #err (#msg "Not enough input!"), |
| 70 | + ); |
| 71 | + }; |
| 72 | + var i = 0; |
| 73 | + let n = text.size() / 2 + text.size() % 2; |
| 74 | + let array = Array.init<Nat8>(n, 0); |
| 75 | + while (i != n) { |
| 76 | + switch (parse()) { |
| 77 | + case (#ok w8) { |
| 78 | + array[i] := w8; |
| 79 | + i += 1; |
| 80 | + }; |
| 81 | + case (#err err) { |
| 82 | + return #err err; |
| 83 | + }; |
| 84 | + }; |
| 85 | + }; |
| 86 | + #ok (Array.freeze<Nat8>(array)); |
| 87 | + }; |
| 88 | + |
| 89 | + |
| 90 | + /** |
| 91 | + * Decode an unsigned 4-bit integer in hexadecimal format. |
| 92 | + */ |
| 93 | + private func decodeW4(char : Char) : Result<Nat8, DecodeError> { |
| 94 | + for (i in Iter.range(0, 15)) { |
| 95 | + if (symbols[i] == char) { |
| 96 | + return #ok (Nat8.fromNat(i)); |
| 97 | + }; |
| 98 | + }; |
| 99 | + let str = "Unexpected character: " # Char.toText(char); |
| 100 | + #err (#msg str); |
| 101 | + }; |
| 102 | +}; |
0 commit comments