Skip to content

Commit

Permalink
Customize format for I32/U32 (ponylang#1920)
Browse files Browse the repository at this point in the history
Fixes formatting of I32.min_value().

TODO: the u32 and u64 funs in
_format_int.pony could probably be
largely re-unified.

Update formatting to follow style
guide proposal (ponylang#1894).

Fixes: ponylang#1920
  • Loading branch information
krig committed May 24, 2017
1 parent ee8b506 commit 48510fb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
70 changes: 63 additions & 7 deletions packages/format/_format_int.pony
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,59 @@ primitive _FormatInt
s.reverse_in_place()
end

fun u64(x: U64, neg: Bool, fmt: FormatInt, prefix: PrefixNumber,
prec: USize, width: USize, align: Align, fill: U32
): String iso^ =>
fun u32(
x: U32,
neg: Bool,
fmt: FormatInt,
prefix: PrefixNumber,
prec: USize,
width: USize,
align: Align,
fill: U32)
: String iso^
=>
match fmt
| FormatUTF32 => return recover String.from_utf32(x.u32()) end
end

(var base', var typestring, var table) = _fmt_int(fmt)
var prestring = _prefix(neg, prefix)
var prec' = if prec == -1 then 0 else prec end
let base = base'.u32()

recover
var s = String((prec + 1).max(width.max(31)))
var value = x

try
if value == 0 then
s.push(table(0))
else
while value != 0 do
let index = ((value = value / base) - (value * base))
s.push(table(index.usize()))
end
end
end

_extend_digits(s, prec')
s.append(typestring)
s.append(prestring)
_pad(s, width, align, fill)
s
end

fun u64(
x: U64,
neg: Bool,
fmt: FormatInt,
prefix: PrefixNumber,
prec: USize,
width: USize,
align: Align,
fill: U32)
: String iso^
=>
match fmt
| FormatUTF32 => return recover String.from_utf32(x.u32()) end
end
Expand Down Expand Up @@ -99,10 +149,16 @@ primitive _FormatInt
s
end

fun u128(x: U128, neg: Bool, fmt: FormatInt = FormatDefault,
prefix: PrefixNumber = PrefixDefault, prec: USize = -1, width: USize = 0,
align: Align = AlignLeft, fill: U32 = ' '
): String iso^ =>
fun u128(x: U128,
neg: Bool,
fmt: FormatInt = FormatDefault,
prefix: PrefixNumber = PrefixDefault,
prec: USize = -1,
width: USize = 0,
align: Align = AlignLeft,
fill: U32 = ' ')
: String iso^
=>
match fmt
| FormatUTF32 => return recover String.from_utf32(x.u32()) end
end
Expand Down
2 changes: 2 additions & 0 deletions packages/format/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class iso _TestInt is UnitTest
fun name(): String => "format/int"

fun apply(h: TestHelper) =>
h.assert_eq[String]("-2147483648",
Format.int[I32](I32.min_value()))
h.assert_eq[String]("00010",
Format.int[U64](10, FormatDefault, PrefixDefault, 5))
h.assert_eq[String]("0x0000A",
Expand Down
4 changes: 3 additions & 1 deletion packages/format/format.pony
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ primitive Format
_FormatInt.u128(x.u128(), false, fmt, prefix, prec, width, align, fill)
elseif x is I128 then
_FormatInt.u128(abs.u128(), neg, fmt, prefix, prec, width, align, fill)
else
elseif (x is U64) or (x is I64) then
_FormatInt.u64(abs.u64(), neg, fmt, prefix, prec, width, align, fill)
else
_FormatInt.u32(abs.u32(), neg, fmt, prefix, prec, width, align, fill)
end

fun float[A: (Float & FloatingPoint[A])](x: A,
Expand Down

0 comments on commit 48510fb

Please sign in to comment.