Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
skilesare committed Jan 22, 2022
1 parent a935fc0 commit a34056b
Show file tree
Hide file tree
Showing 10 changed files with 2,159 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.dfx
31 changes: 31 additions & 0 deletions dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"canisters": {

"pipelinify_testRunner": {
"main": "tests/_pipelinifyTest-Runner.mo",
"type": "motoko"
},
"pipelinify_testConsumer": {
"main": "tests/_pipelinifyTest-Consumer.mo",
"type": "motoko"
},
"pipelinify_testProcessor": {
"main": "tests/_pipelinifyTest-Processor.mo",
"type": "motoko"
}
},
"defaults": {
"build": {
"args": "",
"packtool": "vessel sources"
}
},
"dfx": "0.8.3",
"networks": {
"local": {
"bind": "127.0.0.1:8000",
"type": "ephemeral"
}
},
"version": 1
}
26 changes: 26 additions & 0 deletions package-set.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
let upstream = https://github.com/dfinity/vessel-package-set/releases/download/mo-0.6.4-20210624/package-set.dhall sha256:3f4cffd315d8ee5d2b4b5b00dc03b2e02732345b565340b7cb9cc0001444f525
let Package =
{ name : Text, version : Text, repo : Text, dependencies : List Text }

let additions =
[
{ name = "candy"
, repo = "https://github.com/aramakme/candy_library.git"
, version = "v0.1.1"
, dependencies = ["base"]
},
{ name = "principal"
, repo = "https://github.com/aviate-labs/principal.mo.git"
, version = "v0.1.1"
, dependencies = ["base"]
}] : List Package

let overrides =
[{
name = "base",
repo = "https://github.com/dfinity/motoko-base",
version = "dfx-0.7.2",
dependencies = [] : List Text
}] : List Package

in upstream # additions # overrides
102 changes: 102 additions & 0 deletions src/hex.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Module : Hex.mo
* Description : Hexadecimal encoding and decoding routines.
* Copyright : 2020 Enzo Haussecker
* License : Apache 2.0 with LLVM Exception
* Maintainer : Enzo Haussecker <[email protected]>
* Stability : Stable
*/

import Array "mo:base/Array";
import Iter "mo:base/Iter";
import Option "mo:base/Option";
import Nat8 "mo:base/Nat8";
import Char "mo:base/Char";
import Result "mo:base/Result";

module {

private type Result<Ok, Err> = Result.Result<Ok, Err>;

private let base : Nat8 = 0x10;

private let symbols = [
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
];

/**
* Define a type to indicate that the decoder has failed.
*/
public type DecodeError = {
#msg : Text;
};

/**
* Encode an array of unsigned 8-bit integers in hexadecimal format.
*/
public func encode(array : [Nat8]) : Text {
Array.foldLeft<Nat8, Text>(array, "", func (accum, w8) {
accum # encodeW8(w8);
});
};

/**
* Encode an unsigned 8-bit integer in hexadecimal format.
*/
private func encodeW8(w8 : Nat8) : Text {
let c1 = symbols[Nat8.toNat(w8 / base)];
let c2 = symbols[Nat8.toNat(w8 % base)];
Char.toText(c1) # Char.toText(c2);
};

/**
* Decode an array of unsigned 8-bit integers in hexadecimal format.
*/
public func decode(text : Text) : Result<[Nat8], DecodeError> {
let next = text.chars().next;
func parse() : Result<Nat8, DecodeError> {
Option.get<Result<Nat8, DecodeError>>(
do ? {
let c1 = next()!;
let c2 = next()!;
Result.chain<Nat8, Nat8, DecodeError>(decodeW4(c1), func (x1) {
Result.chain<Nat8, Nat8, DecodeError>(decodeW4(c2), func (x2) {
#ok (x1 * base + x2);
})
})
},
#err (#msg "Not enough input!"),
);
};
var i = 0;
let n = text.size() / 2 + text.size() % 2;
let array = Array.init<Nat8>(n, 0);
while (i != n) {
switch (parse()) {
case (#ok w8) {
array[i] := w8;
i += 1;
};
case (#err err) {
return #err err;
};
};
};
#ok (Array.freeze<Nat8>(array));
};


/**
* Decode an unsigned 4-bit integer in hexadecimal format.
*/
private func decodeW4(char : Char) : Result<Nat8, DecodeError> {
for (i in Iter.range(0, 15)) {
if (symbols[i] == char) {
return #ok (Nat8.fromNat(i));
};
};
let str = "Unexpected character: " # Char.toText(char);
#err (#msg str);
};
};
Loading

0 comments on commit a34056b

Please sign in to comment.