Skip to content

Commit

Permalink
Add ints.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Sep 24, 2021
1 parent ad88e94 commit 8a0b851
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Format Package

Implements conversions to and from string representations of basic data types.
Implements conversions to and from textual representations of basic data types.

## Supported Basic Types

- `Nat`
- `Int`
41 changes: 41 additions & 0 deletions src/Int.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Int "mo:base/Int";
import Result "mo:base/Result";
import Text "mo:base/Text";

import FmtNat "Nat"

module {
public func Parse(t : Text, base : Nat) : Result.Result<Int, Text> {
var neg = false;
var n = switch (Text.stripStart(t, #char('+'))) {
case (null) { t; };
case (? t) { t; };
};
let nt = switch (Text.stripStart(t, #char('-'))) {
case (? t) {
neg := true;
t;
};
case (null) {
switch (Text.stripStart(t, #char('+'))) {
case (null) { t; };
case (? t) { t; };
};
};
};
switch (FmtNat.Parse(nt, base)) {
case (#err(e)) { #err(e); };
case (#ok(n)) {
let i : Int = n;
if (neg) { #ok(-i); }
else { #ok(i); };
};
};
};

public func Format(i : Int, base : Nat) : Text {
let n = FmtNat.Format(Int.abs(i), base);
if (i < 0) { "-" # n; }
else { n; };
};
};
4 changes: 2 additions & 2 deletions src/Nat.mo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import P "mo:base/Prelude";
import Debug "mo:base/Debug";

module {
public func ParseNat(t : Text, base : Nat) : Result.Result<Nat, Text> {
public func Parse(t : Text, base : Nat) : Result.Result<Nat, Text> {
if (t == "") return #err("text must be non-empty");
if (base < 2 or 36 < base) return #err("invalid base: " # Nat.toText(base));

Expand All @@ -27,7 +27,7 @@ module {
#ok(n);
};

public func FormatNat(n : Nat, base : Nat) : Text {
public func Format(n : Nat, base : Nat) : Text {
var s = "";
var bs : [Nat] = [];
var u = n;
Expand Down
13 changes: 13 additions & 0 deletions test/Int.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import FmtInt "../src/Int";

assert(FmtInt.Parse("100_000", 10) == #ok(100_000));
assert(FmtInt.Parse("+100_000", 10) == #ok(100_000));
assert(FmtInt.Parse("-100_000", 10) == #ok(-100_000));

assert(FmtInt.Parse("-100", 10) == #ok(-100));
assert(FmtInt.Parse("-100", 8) == #ok(-64));
assert(FmtInt.Parse("-100", 2) == #ok(-4));

assert(FmtInt.Format(-4, 2) == "-100");
assert(FmtInt.Format(-64, 8) == "-100");
assert(FmtInt.Format(-100, 10) == "-100");
14 changes: 8 additions & 6 deletions test/Nat.mo
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import FmtNat "../src/Nat";

assert(FmtNat.ParseNat("100", 10) == #ok(100));
assert(FmtNat.ParseNat("100", 8) == #ok(64));
assert(FmtNat.ParseNat("100", 2) == #ok(4));
assert(FmtNat.Parse("100_000", 10) == #ok(100_000));

assert(FmtNat.FormatNat(4, 2) == "100");
assert(FmtNat.FormatNat(64, 8) == "100");
assert(FmtNat.FormatNat(100, 10) == "100");
assert(FmtNat.Parse("100", 10) == #ok(100));
assert(FmtNat.Parse("100", 8) == #ok(64));
assert(FmtNat.Parse("100", 2) == #ok(4));

assert(FmtNat.Format(4, 2) == "100");
assert(FmtNat.Format(64, 8) == "100");
assert(FmtNat.Format(100, 10) == "100");

0 comments on commit 8a0b851

Please sign in to comment.