Browse Source

HAL_ChibiOS: avoid strstr in usbcfg

saves nearly 2k of flash
mission-4.1.18
Andrew Tridgell 7 years ago
parent
commit
5216870492
  1. 19
      libraries/AP_HAL_ChibiOS/hwdef/common/usbcfg.c

19
libraries/AP_HAL_ChibiOS/hwdef/common/usbcfg.c

@ -178,16 +178,31 @@ static USBDescriptor vcom_strings[] = {
{0, NULL}, // version {0, NULL}, // version
}; };
/*
check if one string contains another
*/
static bool string_contains(const char *haystack, const char *needle)
{
uint8_t needle_len = strlen(needle);
while (*haystack) {
if (strncmp(haystack, needle, needle_len) == 0) {
return true;
}
haystack++;
}
return false;
}
/* /*
handle substitution of variables in strings for USB descriptors handle substitution of variables in strings for USB descriptors
*/ */
static char *string_substitute(const char *str) static char *string_substitute(const char *str)
{ {
uint8_t new_len = strlen(str); uint8_t new_len = strlen(str);
if (strstr(str, "%BOARD%")) { if (string_contains(str, "%BOARD%")) {
new_len += strlen(HAL_BOARD_NAME) - 7; new_len += strlen(HAL_BOARD_NAME) - 7;
} }
if (strstr(str, "%SERIAL%")) { if (string_contains(str, "%SERIAL%")) {
new_len += 24 - 8; new_len += 24 - 8;
} }
char *str2 = malloc(new_len+1); char *str2 = malloc(new_len+1);

Loading…
Cancel
Save