format: Don't try (and fail) to write an empty buffer
We failed at dumping empty strings, because the file writer reported 0 bytes written as an error. Let's just not write empty buffers in the first place, this fixes the bug and also avoids some pointless function calls.
This commit is contained in:
parent
3fcf4f4c49
commit
1a41df2389
1 changed files with 13 additions and 6 deletions
19
src/format.c
19
src/format.c
|
|
@ -7,9 +7,16 @@
|
|||
|
||||
#include "format.h"
|
||||
|
||||
#define WRITE(w, buf, len) w.write(w.opaque, buf, len)
|
||||
#define TRY FMT_TRY
|
||||
|
||||
static bool
|
||||
write(struct apfl_format_writer w, const char *buf, size_t len)
|
||||
{
|
||||
return len == 0
|
||||
? true
|
||||
: w.write(w.opaque, buf, len);
|
||||
}
|
||||
|
||||
static bool
|
||||
write_file(void *opaque, const char *buf, size_t len)
|
||||
{
|
||||
|
|
@ -45,7 +52,7 @@ apfl_format_string_writer(struct apfl_string_builder *sb)
|
|||
bool
|
||||
apfl_format_put_string_view(struct apfl_format_writer w, struct apfl_string_view sv)
|
||||
{
|
||||
return WRITE(w, sv.bytes, sv.len);
|
||||
return write(w, sv.bytes, sv.len);
|
||||
}
|
||||
|
||||
#define PUT_INT_BUFSIZE 21 // ceil(log10(2**64)) + 1
|
||||
|
|
@ -56,13 +63,13 @@ apfl_format_put_int(struct apfl_format_writer w, int i)
|
|||
char buf[PUT_INT_BUFSIZE];
|
||||
size_t len = snprintf(buf, PUT_INT_BUFSIZE, "%d", i);
|
||||
assert(len < PUT_INT_BUFSIZE);
|
||||
return WRITE(w, buf, len);
|
||||
return write(w, buf, len);
|
||||
}
|
||||
|
||||
bool
|
||||
apfl_format_put_char(struct apfl_format_writer w, char c)
|
||||
{
|
||||
return WRITE(w, &c, 1);
|
||||
return write(w, &c, 1);
|
||||
}
|
||||
|
||||
#define PUT_HEXBYTE_BUFSIZE 3
|
||||
|
|
@ -73,7 +80,7 @@ apfl_format_put_hexbyte(struct apfl_format_writer w, unsigned char c)
|
|||
char buf[PUT_HEXBYTE_BUFSIZE];
|
||||
size_t len = snprintf(buf, PUT_HEXBYTE_BUFSIZE, "%x", (unsigned) c);
|
||||
assert(len < PUT_HEXBYTE_BUFSIZE);
|
||||
return WRITE(w, buf, len);
|
||||
return write(w, buf, len);
|
||||
}
|
||||
|
||||
bool
|
||||
|
|
@ -101,7 +108,7 @@ apfl_format_put_number(struct apfl_format_writer w, apfl_number number)
|
|||
{
|
||||
char buf[PUT_NUMBER_BUFSIZE];
|
||||
size_t len = snprintf(buf, PUT_NUMBER_BUFSIZE, "%f", number);
|
||||
TRY(WRITE(w, buf, len));
|
||||
TRY(write(w, buf, len));
|
||||
if (len >= PUT_NUMBER_BUFSIZE) {
|
||||
TRY(apfl_format_put_string(w, "[...]"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue