mirror of
https://github.com/esphome/esphome.git
synced 2026-09-04 03:56:04 +00:00
Remove redundant block scoping inside loop bodies
This commit is contained in:
@@ -2331,39 +2331,35 @@ bool SubscribeBluetoothLEAdvertisementsRequest::decode_varint(uint32_t field_id,
|
||||
uint8_t *BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer &buffer PROTO_ENCODE_DEBUG_PARAM) const {
|
||||
uint8_t *__restrict__ pos = buffer.get_pos();
|
||||
for (uint16_t i = 0; i < this->advertisements_len; i++) {
|
||||
{
|
||||
auto &sub_msg = this->advertisements[i];
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, 10);
|
||||
uint8_t *len_pos = pos++;
|
||||
uint8_t *body_start = pos;
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, 8);
|
||||
ProtoEncode::encode_varint_raw_64(pos PROTO_ENCODE_DEBUG_ARG, sub_msg.address);
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, 16);
|
||||
ProtoEncode::encode_varint_raw_short(pos PROTO_ENCODE_DEBUG_ARG, encode_zigzag32(sub_msg.rssi));
|
||||
if (sub_msg.address_type) {
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, 24);
|
||||
ProtoEncode::encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, sub_msg.address_type);
|
||||
}
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, 34);
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, static_cast<uint8_t>(sub_msg.data_len));
|
||||
ProtoEncode::encode_raw(pos PROTO_ENCODE_DEBUG_ARG, sub_msg.data, sub_msg.data_len);
|
||||
*len_pos = static_cast<uint8_t>(pos - body_start);
|
||||
auto &sub_msg = this->advertisements[i];
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, 10);
|
||||
uint8_t *len_pos = pos++;
|
||||
uint8_t *body_start = pos;
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, 8);
|
||||
ProtoEncode::encode_varint_raw_64(pos PROTO_ENCODE_DEBUG_ARG, sub_msg.address);
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, 16);
|
||||
ProtoEncode::encode_varint_raw_short(pos PROTO_ENCODE_DEBUG_ARG, encode_zigzag32(sub_msg.rssi));
|
||||
if (sub_msg.address_type) {
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, 24);
|
||||
ProtoEncode::encode_varint_raw(pos PROTO_ENCODE_DEBUG_ARG, sub_msg.address_type);
|
||||
}
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, 34);
|
||||
ProtoEncode::write_raw_byte(pos PROTO_ENCODE_DEBUG_ARG, static_cast<uint8_t>(sub_msg.data_len));
|
||||
ProtoEncode::encode_raw(pos PROTO_ENCODE_DEBUG_ARG, sub_msg.data, sub_msg.data_len);
|
||||
*len_pos = static_cast<uint8_t>(pos - body_start);
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
uint32_t BluetoothLERawAdvertisementsResponse::calculate_size() const {
|
||||
uint32_t size = 0;
|
||||
for (uint16_t i = 0; i < this->advertisements_len; i++) {
|
||||
{
|
||||
auto &sub_msg = this->advertisements[i];
|
||||
uint32_t sub_size = 0;
|
||||
sub_size += ProtoSize::calc_uint64_force(1, sub_msg.address);
|
||||
sub_size += ProtoSize::calc_sint32_force(1, sub_msg.rssi);
|
||||
sub_size += sub_msg.address_type ? 2 : 0;
|
||||
sub_size += 2 + sub_msg.data_len;
|
||||
size += 2 + sub_size;
|
||||
}
|
||||
auto &sub_msg = this->advertisements[i];
|
||||
uint32_t sub_size = 0;
|
||||
sub_size += ProtoSize::calc_uint64_force(1, sub_msg.address);
|
||||
sub_size += ProtoSize::calc_sint32_force(1, sub_msg.rssi);
|
||||
sub_size += sub_msg.address_type ? 2 : 0;
|
||||
sub_size += 2 + sub_msg.data_len;
|
||||
size += 2 + sub_size;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
@@ -1593,11 +1593,10 @@ def _generate_inline_encode_block(
|
||||
assert tag < 128, f"inline_encode requires single-byte tag, got {tag}"
|
||||
|
||||
lines = []
|
||||
lines.append("{")
|
||||
lines.append(f" auto &sub_msg = {element};")
|
||||
lines.append(f" ProtoEncode::write_raw_byte(pos, {tag});")
|
||||
lines.append(" uint8_t *len_pos = pos++;")
|
||||
lines.append(" uint8_t *body_start = pos;")
|
||||
lines.append(f"auto &sub_msg = {element};")
|
||||
lines.append(f"ProtoEncode::write_raw_byte(pos, {tag});")
|
||||
lines.append("uint8_t *len_pos = pos++;")
|
||||
lines.append("uint8_t *body_start = pos;")
|
||||
|
||||
# Generate inline field encoding for each sub-message field
|
||||
for field in sub_desc.field:
|
||||
@@ -1607,10 +1606,9 @@ def _generate_inline_encode_block(
|
||||
encode_line = ti.encode_content
|
||||
# Replace this-> with sub_msg reference for the sub-message fields
|
||||
encode_line = encode_line.replace("this->", "sub_msg.")
|
||||
lines.extend(f" {line}" for line in encode_line.split("\n"))
|
||||
lines.extend(encode_line.split("\n"))
|
||||
|
||||
lines.append(" *len_pos = static_cast<uint8_t>(pos - body_start);")
|
||||
lines.append("}")
|
||||
lines.append("*len_pos = static_cast<uint8_t>(pos - body_start);")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
@@ -1630,9 +1628,8 @@ def _generate_inline_size_block(
|
||||
sub_desc = _message_desc_map[sub_msg_name]
|
||||
|
||||
lines = []
|
||||
lines.append("{")
|
||||
lines.append(f" auto &sub_msg = {element};")
|
||||
lines.append(" uint32_t sub_size = 0;")
|
||||
lines.append(f"auto &sub_msg = {element};")
|
||||
lines.append("uint32_t sub_size = 0;")
|
||||
|
||||
for field in sub_desc.field:
|
||||
if field.options.deprecated:
|
||||
@@ -1644,11 +1641,10 @@ def _generate_inline_size_block(
|
||||
size_line = size_line.replace("size +=", "sub_size +=")
|
||||
# Replace hardcoded this-> references (e.g., FixedArrayBytesType uses this->field_len)
|
||||
size_line = size_line.replace("this->", "sub_msg.")
|
||||
lines.extend(f" {line}" for line in size_line.split("\n"))
|
||||
lines.extend(size_line.split("\n"))
|
||||
|
||||
# 1 byte tag + 1 byte length (guaranteed < 128) + body
|
||||
lines.append(" size += 2 + sub_size;")
|
||||
lines.append("}")
|
||||
lines.append("size += 2 + sub_size;")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user