Skip to content

Commit a34056b

Browse files
committed
Initial Commit
1 parent a935fc0 commit a34056b

10 files changed

Lines changed: 2159 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.dfx

dfx.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"canisters": {
3+
4+
"pipelinify_testRunner": {
5+
"main": "tests/_pipelinifyTest-Runner.mo",
6+
"type": "motoko"
7+
},
8+
"pipelinify_testConsumer": {
9+
"main": "tests/_pipelinifyTest-Consumer.mo",
10+
"type": "motoko"
11+
},
12+
"pipelinify_testProcessor": {
13+
"main": "tests/_pipelinifyTest-Processor.mo",
14+
"type": "motoko"
15+
}
16+
},
17+
"defaults": {
18+
"build": {
19+
"args": "",
20+
"packtool": "vessel sources"
21+
}
22+
},
23+
"dfx": "0.8.3",
24+
"networks": {
25+
"local": {
26+
"bind": "127.0.0.1:8000",
27+
"type": "ephemeral"
28+
}
29+
},
30+
"version": 1
31+
}

package-set.dhall

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
let upstream = https://github.com/dfinity/vessel-package-set/releases/download/mo-0.6.4-20210624/package-set.dhall sha256:3f4cffd315d8ee5d2b4b5b00dc03b2e02732345b565340b7cb9cc0001444f525
2+
let Package =
3+
{ name : Text, version : Text, repo : Text, dependencies : List Text }
4+
5+
let additions =
6+
[
7+
{ name = "candy"
8+
, repo = "https://github.com/aramakme/candy_library.git"
9+
, version = "v0.1.1"
10+
, dependencies = ["base"]
11+
},
12+
{ name = "principal"
13+
, repo = "https://github.com/aviate-labs/principal.mo.git"
14+
, version = "v0.1.1"
15+
, dependencies = ["base"]
16+
}] : List Package
17+
18+
let overrides =
19+
[{
20+
name = "base",
21+
repo = "https://github.com/dfinity/motoko-base",
22+
version = "dfx-0.7.2",
23+
dependencies = [] : List Text
24+
}] : List Package
25+
26+
in upstream # additions # overrides

src/hex.mo

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)