Add underscore separators for large numbers in reporting

This commit is contained in:
ayazhafiz 2022-02-03 00:18:24 -05:00
parent 5362ed1e6c
commit 706640a1fa
3 changed files with 58 additions and 15 deletions

View file

@ -1141,16 +1141,24 @@ fn pretty_runtime_error<'b>(
let (big_or_small, info) = if let IntErrorKind::Underflow = error_kind { let (big_or_small, info) = if let IntErrorKind::Underflow = error_kind {
( (
"small", "small",
alloc.reflow( alloc.concat(vec![
"The smallest number representable in Roc is the minimum I128 value, -170_141_183_460_469_231_731_687_303_715_884_105_728.", alloc.reflow(
), "The smallest number representable in Roc is the minimum I128 value, ",
),
alloc.int_literal(i128::MIN),
alloc.text("."),
]),
) )
} else { } else {
( (
"big", "big",
alloc.reflow( alloc.concat(vec![
"The largest number representable in Roc is the maximum U128 value, 340_282_366_920_938_463_463_374_607_431_768_211_455.", alloc.reflow(
), "The largest number representable in Roc is the maximum U128 value, ",
),
alloc.int_literal(u128::MAX),
alloc.text("."),
]),
) )
}; };
@ -1199,7 +1207,8 @@ fn pretty_runtime_error<'b>(
alloc.reflow("The suffix indicates this integer is a "), alloc.reflow("The suffix indicates this integer is a "),
alloc.type_str(suffix_type), alloc.type_str(suffix_type),
alloc.reflow(", whose maximum value is "), alloc.reflow(", whose maximum value is "),
alloc.text(format!("{}.", max_value)), alloc.int_literal(max_value),
alloc.reflow("."),
])), ])),
]); ]);
@ -1223,7 +1232,8 @@ fn pretty_runtime_error<'b>(
alloc.reflow("The suffix indicates this integer is a "), alloc.reflow("The suffix indicates this integer is a "),
alloc.type_str(suffix_type), alloc.type_str(suffix_type),
alloc.reflow(", whose minimum value is "), alloc.reflow(", whose minimum value is "),
alloc.text(format!("{}.", min_value)), alloc.int_literal(min_value),
alloc.reflow("."),
])), ])),
]); ]);

View file

@ -632,6 +632,39 @@ impl<'a> RocDocAllocator<'a> {
self.text(format!("{}", ident.as_inline_str())) self.text(format!("{}", ident.as_inline_str()))
.annotate(Annotation::Symbol) .annotate(Annotation::Symbol)
} }
pub fn int_literal<I>(&'a self, int: I) -> DocBuilder<'a, Self, Annotation>
where
I: ToString,
{
let s = int.to_string();
let is_negative = s.starts_with("-");
if s.len() < 7 + (is_negative as usize) {
// If the number is not at least in the millions, return it as-is.
return self.text(s);
}
// Otherwise, let's add numeric separators to make it easier to read.
let mut result = String::with_capacity(s.len() + s.len() / 3);
for (idx, c) in s
.get((is_negative as usize)..)
.unwrap()
.chars()
.rev()
.enumerate()
{
if idx != 0 && idx % 3 == 0 {
result.push('_');
}
result.push(c);
}
if is_negative {
result.push('-');
}
self.text(result.chars().rev().collect::<String>())
}
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]

View file

@ -7622,7 +7622,7 @@ I need all branches in an `if` to have the same type!
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
Tip: The suffix indicates this integer is a U32, whose maximum value Tip: The suffix indicates this integer is a U32, whose maximum value
is 4294967295. is 4_294_967_295.
"# "#
), ),
) )
@ -7662,7 +7662,7 @@ I need all branches in an `if` to have the same type!
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Tip: The suffix indicates this integer is a U64, whose maximum value Tip: The suffix indicates this integer is a U64, whose maximum value
is 18446744073709551615. is 18_446_744_073_709_551_615.
"# "#
), ),
) )
@ -7802,7 +7802,7 @@ I need all branches in an `if` to have the same type!
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
Tip: The suffix indicates this integer is a I32, whose maximum value Tip: The suffix indicates this integer is a I32, whose maximum value
is 2147483647. is 2_147_483_647.
"# "#
), ),
) )
@ -7822,7 +7822,7 @@ I need all branches in an `if` to have the same type!
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
Tip: The suffix indicates this integer is a I32, whose minimum value Tip: The suffix indicates this integer is a I32, whose minimum value
is -2147483648. is -2_147_483_648.
"# "#
), ),
) )
@ -7842,7 +7842,7 @@ I need all branches in an `if` to have the same type!
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Tip: The suffix indicates this integer is a I64, whose maximum value Tip: The suffix indicates this integer is a I64, whose maximum value
is 9223372036854775807. is 9_223_372_036_854_775_807.
"# "#
), ),
) )
@ -7862,7 +7862,7 @@ I need all branches in an `if` to have the same type!
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Tip: The suffix indicates this integer is a I64, whose minimum value Tip: The suffix indicates this integer is a I64, whose minimum value
is -9223372036854775808. is -9_223_372_036_854_775_808.
"# "#
), ),
) )
@ -7882,7 +7882,7 @@ I need all branches in an `if` to have the same type!
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Tip: The suffix indicates this integer is a I128, whose maximum value Tip: The suffix indicates this integer is a I128, whose maximum value
is 170141183460469231731687303715884105727. is 170_141_183_460_469_231_731_687_303_715_884_105_727.
"# "#
), ),
) )