From d5f459d2a5b8d8a478602d05b435cd9f3467f732 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 20 Dec 2023 19:38:55 -1000 Subject: [PATCH] feat: small speed up to gap parser --- src/bluetooth_data_tools/gap.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/bluetooth_data_tools/gap.py b/src/bluetooth_data_tools/gap.py index e82132e..5f52ffa 100644 --- a/src/bluetooth_data_tools/gap.py +++ b/src/bluetooth_data_tools/gap.py @@ -210,31 +210,28 @@ def _uncached_parse_advertisement_data( for gap_data in data: offset = 0 total_length = len(gap_data) - while offset < total_length: - try: - length = gap_data[offset] - if not length: - if offset + 2 < total_length: - # Maybe zero padding - offset += 1 - continue - break - gap_type_num = gap_data[offset + 1] - if not gap_type_num: - break - start = offset + 2 - end = start + length - 1 - gap_value = gap_data[start:end] - except IndexError as ex: + while offset + 1 < total_length: + length = gap_data[offset] + if not length: + if offset + 2 < total_length: + # Maybe zero padding + offset += 1 + continue + break + gap_type_num = gap_data[offset + 1] + if not gap_type_num: + break + start = offset + 2 + end = start + length - 1 + if total_length < end: _LOGGER.debug( "Invalid BLE GAP AD structure at offset %s: %s (%s)", offset, gap_data, - ex, ) offset += 1 + length continue - + gap_value = gap_data[start:end] offset += 1 + length if end - start == 0: continue