mirror of
https://github.com/esphome/esphome.git
synced 2026-07-10 17:05:36 +00:00
Fix clang-tidy naming and add PROTO_VARINT_PARSE_FAILED constant
- Rename parse_slow_ -> parse_slow and parse_wide_ -> parse_wide (static methods should not have trailing underscore per clang-tidy) - Add PROTO_VARINT_PARSE_FAILED constant for consumed field sentinel Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -20,7 +20,7 @@ void ProtoWriteBuffer::encode_varint_raw_slow_(uint32_t value) {
|
||||
*this->pos_++ = static_cast<uint8_t>(value);
|
||||
}
|
||||
|
||||
ProtoVarIntResult ProtoVarInt::parse_slow_(const uint8_t *buffer, uint32_t len) {
|
||||
ProtoVarIntResult ProtoVarInt::parse_slow(const uint8_t *buffer, uint32_t len) {
|
||||
// Multi-byte varint: first byte already checked to have high bit set
|
||||
uint32_t result32 = buffer[0] & 0x7F;
|
||||
#ifdef USE_API_VARINT64
|
||||
@@ -36,14 +36,14 @@ ProtoVarIntResult ProtoVarInt::parse_slow_(const uint8_t *buffer, uint32_t len)
|
||||
}
|
||||
}
|
||||
#ifdef USE_API_VARINT64
|
||||
return parse_wide_(buffer, len, result32);
|
||||
return parse_wide(buffer, len, result32);
|
||||
#else
|
||||
return {0, 0};
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_API_VARINT64
|
||||
ProtoVarIntResult ProtoVarInt::parse_wide_(const uint8_t *buffer, uint32_t len, uint32_t result32) {
|
||||
ProtoVarIntResult ProtoVarInt::parse_wide(const uint8_t *buffer, uint32_t len, uint32_t result32) {
|
||||
uint64_t result64 = result32;
|
||||
uint32_t limit = std::min(len, uint32_t(10));
|
||||
for (uint32_t i = 4; i < limit; i++) {
|
||||
|
||||
@@ -98,17 +98,20 @@ inline void encode_varint_to_buffer(uint32_t val, uint8_t *buffer) {
|
||||
* within the same function scope where temporaries are created.
|
||||
*/
|
||||
|
||||
/// Sentinel value for consumed field indicating parse failure
|
||||
inline constexpr uint32_t PROTO_VARINT_PARSE_FAILED = 0;
|
||||
|
||||
/// Result of parsing a varint: value + number of bytes consumed.
|
||||
/// consumed == 0 indicates parse failure (not enough data or invalid).
|
||||
/// consumed == PROTO_VARINT_PARSE_FAILED indicates parse failure (not enough data or invalid).
|
||||
struct ProtoVarIntResult {
|
||||
#ifdef USE_API_VARINT64
|
||||
uint64_t value;
|
||||
#else
|
||||
uint32_t value;
|
||||
#endif
|
||||
uint32_t consumed; // 0 = parse failed
|
||||
uint32_t consumed; // PROTO_VARINT_PARSE_FAILED = parse failed
|
||||
|
||||
constexpr bool has_value() const { return this->consumed != 0; }
|
||||
constexpr bool has_value() const { return this->consumed != PROTO_VARINT_PARSE_FAILED; }
|
||||
constexpr uint16_t as_uint16() const { return this->value; }
|
||||
constexpr uint32_t as_uint32() const { return this->value; }
|
||||
constexpr bool as_bool() const { return this->value; }
|
||||
@@ -135,17 +138,16 @@ class ProtoVarInt {
|
||||
// (booleans, small enums, field tags, small message sizes/types).
|
||||
if ((buffer[0] & 0x80) == 0) [[likely]]
|
||||
return {buffer[0], 1};
|
||||
return parse_slow_(buffer, len);
|
||||
return parse_slow(buffer, len);
|
||||
}
|
||||
|
||||
protected:
|
||||
// Slow path for multi-byte varints (>= 128), outlined to keep fast path small
|
||||
static ProtoVarIntResult parse_slow_(const uint8_t *buffer, uint32_t len) __attribute__((noinline));
|
||||
static ProtoVarIntResult parse_slow(const uint8_t *buffer, uint32_t len) __attribute__((noinline));
|
||||
|
||||
#ifdef USE_API_VARINT64
|
||||
/// Continue parsing varint bytes 4-9 with 64-bit arithmetic.
|
||||
static ProtoVarIntResult parse_wide_(const uint8_t *buffer, uint32_t len, uint32_t result32)
|
||||
__attribute__((noinline));
|
||||
static ProtoVarIntResult parse_wide(const uint8_t *buffer, uint32_t len, uint32_t result32) __attribute__((noinline));
|
||||
#endif
|
||||
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user