-
Notifications
You must be signed in to change notification settings - Fork 1
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
70 additions
and
9 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 |
---|---|---|
@@ -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` |
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,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; }; | ||
}; | ||
}; |
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
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"); |
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 |
---|---|---|
@@ -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"); |