dms/hms: Protect display against bad input

When entering a DMS, HMS or date values that have a non-real input,
display a unit object normally, instead of having a `Bad argument
type` error while rendering the stack.

Fixes: #984

Signed-off-by: Christophe de Dinechin <christophe@dinechin.org>
This commit is contained in:
Christophe de Dinechin 2024-06-25 23:42:18 +02:00
parent eb59577c1a
commit dd8e607da9
3 changed files with 22 additions and 11 deletions

View file

@ -523,7 +523,7 @@ void render_time(renderer &r, algebraic_g &value,
// Render a time (or an angle) as hours/minutes/seconds
// ----------------------------------------------------------------------------
{
if (!value)
if (!value || !value->is_real())
return;
bool as_time = *hrs == ':';
uint h = value->as_uint32(0, false);
@ -544,7 +544,7 @@ void render_time(renderer &r, algebraic_g &value,
r.put(sec);
value = value % one;
if (value && !value->is_zero())
if (value && !value->is_zero(false))
{
if (as_time && algebraic::to_decimal(value))
{
@ -571,6 +571,8 @@ size_t render_dms(renderer &r, algebraic_g value,
// Render a number as "degrees / minutes / seconds"
// ----------------------------------------------------------------------------
{
if (!value || !value->is_real())
return 0;
bool neg = value->is_negative(false);
if (neg)
{

View file

@ -162,7 +162,7 @@ void tests::run(bool onlyCurrent)
if (onlyCurrent)
{
here().begin("Current");
global_variables();
hms_dms_operations();
}
else
{
@ -6190,6 +6190,12 @@ void tests::hms_dms_operations()
.test(DOT).editor("\"1..\"")
.test(DOT).editor("\"1...\"")
.test(ENTER).expect("\"1...\"");
step("Invalid DMS value should display correctly")
.test(CLEAR, "ABC_dms", ENTER).expect("'ABC'dms");
step("Invalid HMS value should display correctly")
.test(CLEAR, "ABC_hms", ENTER).expect("'ABC'hms");
step("Invalid date value should display correctly")
.test(CLEAR, "ABC_date", ENTER).expect("'ABC'date");
step("Converting DMS to HMS")
.test(CLEAR)

View file

@ -179,15 +179,18 @@ RENDER_BODY(unit)
bool ed = r.editing();
if (symbol_p sym = uexpr->as_quoted<symbol>())
{
if (sym->matches("dms"))
sz = render_dms(r, value, "°", "", "");
else if (sym->matches("hms"))
sz = ed ? render_dms(r, value, "°", "", "")
if (value->is_real())
{
if (sym->matches("dms"))
sz = render_dms(r, value, "°", "", "");
else if (sym->matches("hms"))
sz = ed ? render_dms(r, value, "°", "", "")
: render_dms(r, value, ":", ":", "");
else if (sym->matches("date") && !ed)
sz = render_date(r, value);
if (sz && !ed)
return sz;
else if (sym->matches("date") && !ed)
sz = render_date(r, value);
if (sz && !ed)
return sz;
}
}
if (sz)
{