Skip to content

Commit

Permalink
Use strfromf for explicit float formats too
Browse files Browse the repository at this point in the history
The default float format was using strfromf, but when a float format
was explicitly selected, sprintf was being used.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Sep 16, 2021
1 parent efd9884 commit 8f5c7c8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion chips/atmega/snek-atmega.h
Expand Up @@ -51,7 +51,8 @@
static const char PROGMEM __fmt__[] = (fmt); \
sprintf_P(dst, __fmt__, ##args); \
})
#define strfromf(dst, len, fmt, val) sprintf_const(dst, fmt, val)
#define strfromf_const(dst, len, fmt, val) sprintf_const(dst, fmt, val)
#define strfromf(dst, len, fmt, val) sprintf(dst, fmt, val)

#define SNEK_BUILTIN_NAMES_DECLARE(n) PROGMEM n
#define SNEK_BUILTIN_NAMES(a) ((uint8_t) pgm_read_byte(&snek_builtin_names[a]))
Expand Down
3 changes: 2 additions & 1 deletion chips/avr/snek-avr.h
Expand Up @@ -64,7 +64,8 @@ sqrtf(float x) {
static const char PROGMEM __fmt__[] = (fmt); \
sprintf_P(dst, __fmt__, ##args); \
})
#define strfromf(dst, len, fmt, val) sprintf_const(dst, fmt, val)
#define strfromf_const(dst, len, fmt, val) sprintf_const(dst, fmt, val)
#define strfromf(dst, len, fmt, val) sprintf(dst, fmt, val)

#define SNEK_BUILTIN_NAMES_DECLARE(n) PROGMEM n
#define SNEK_BUILTIN_NAMES(a) ((uint8_t) pgm_read_byte(&snek_builtin_names[a]))
Expand Down
4 changes: 2 additions & 2 deletions snek-print.c
Expand Up @@ -87,7 +87,7 @@ snek_poly_format(snek_buf_t *buf, snek_poly_t a, char format)
case 'G':
if (atype != snek_float)
break;
sprintf(tmp, format_string, printf_float(snek_poly_to_float(a)));
strfromf(tmp, sizeof(tmp), format_string, snek_poly_to_float(a));
buf->put_s(tmp, closure);
return;
case 'c':
Expand All @@ -114,7 +114,7 @@ snek_poly_format(snek_buf_t *buf, snek_poly_t a, char format)
buf->put_s("None", closure);
else switch (atype) {
case snek_float:
strfromf(tmp, sizeof(tmp), "%.9g", snek_poly_to_float(a));
strfromf_const(tmp, sizeof(tmp), "%.9g", snek_poly_to_float(a));
buf->put_s(tmp, closure);
break;
case snek_string:
Expand Down
4 changes: 4 additions & 0 deletions snek.h
Expand Up @@ -574,6 +574,10 @@ snek_panic(const char *message);
#define sprintf_const sprintf
#endif

#ifndef strfromf_const
#define strfromf_const strfromf
#endif

extern bool snek_abort;

/* snek-frame.c */
Expand Down

0 comments on commit 8f5c7c8

Please sign in to comment.