mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[api] Eliminate intermediate buffers in protobuf dump helpers (#13742)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -2599,15 +2599,8 @@ static inline void append_field_prefix(DumpBuffer &out, const char *field_name,
|
||||
out.append(indent, ' ').append(field_name).append(": ");
|
||||
}
|
||||
|
||||
static inline void append_with_newline(DumpBuffer &out, const char *str) {
|
||||
out.append(str);
|
||||
out.append("\\n");
|
||||
}
|
||||
|
||||
static inline void append_uint(DumpBuffer &out, uint32_t value) {
|
||||
char buf[16];
|
||||
snprintf(buf, sizeof(buf), "%" PRIu32, value);
|
||||
out.append(buf);
|
||||
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRIu32, value));
|
||||
}
|
||||
|
||||
// RAII helper for message dump formatting
|
||||
@@ -2625,31 +2618,23 @@ class MessageDumpHelper {
|
||||
|
||||
// Helper functions to reduce code duplication in dump methods
|
||||
static void dump_field(DumpBuffer &out, const char *field_name, int32_t value, int indent = 2) {
|
||||
char buffer[64];
|
||||
append_field_prefix(out, field_name, indent);
|
||||
snprintf(buffer, 64, "%" PRId32, value);
|
||||
append_with_newline(out, buffer);
|
||||
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRId32 "\\n", value));
|
||||
}
|
||||
|
||||
static void dump_field(DumpBuffer &out, const char *field_name, uint32_t value, int indent = 2) {
|
||||
char buffer[64];
|
||||
append_field_prefix(out, field_name, indent);
|
||||
snprintf(buffer, 64, "%" PRIu32, value);
|
||||
append_with_newline(out, buffer);
|
||||
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRIu32 "\\n", value));
|
||||
}
|
||||
|
||||
static void dump_field(DumpBuffer &out, const char *field_name, float value, int indent = 2) {
|
||||
char buffer[64];
|
||||
append_field_prefix(out, field_name, indent);
|
||||
snprintf(buffer, 64, "%g", value);
|
||||
append_with_newline(out, buffer);
|
||||
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%g\\n", value));
|
||||
}
|
||||
|
||||
static void dump_field(DumpBuffer &out, const char *field_name, uint64_t value, int indent = 2) {
|
||||
char buffer[64];
|
||||
append_field_prefix(out, field_name, indent);
|
||||
snprintf(buffer, 64, "%" PRIu64, value);
|
||||
append_with_newline(out, buffer);
|
||||
out.set_pos(buf_append_printf(out.data(), DumpBuffer::CAPACITY, out.pos(), "%" PRIu64 "\\n", value));
|
||||
}
|
||||
|
||||
static void dump_field(DumpBuffer &out, const char *field_name, bool value, int indent = 2) {
|
||||
@@ -2689,7 +2674,7 @@ static void dump_bytes_field(DumpBuffer &out, const char *field_name, const uint
|
||||
char hex_buf[format_hex_pretty_size(160)];
|
||||
append_field_prefix(out, field_name, indent);
|
||||
format_hex_pretty_to(hex_buf, data, len);
|
||||
append_with_newline(out, hex_buf);
|
||||
out.append(hex_buf).append("\\n");
|
||||
}
|
||||
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user