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

View file

@ -162,7 +162,7 @@ void tests::run(bool onlyCurrent)
if (onlyCurrent) if (onlyCurrent)
{ {
here().begin("Current"); here().begin("Current");
global_variables(); hms_dms_operations();
} }
else else
{ {
@ -6190,6 +6190,12 @@ void tests::hms_dms_operations()
.test(DOT).editor("\"1..\"") .test(DOT).editor("\"1..\"")
.test(DOT).editor("\"1...\"") .test(DOT).editor("\"1...\"")
.test(ENTER).expect("\"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") step("Converting DMS to HMS")
.test(CLEAR) .test(CLEAR)

View file

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