From b5b6b972952040d7b6d401f79cb36080fec92a7a Mon Sep 17 00:00:00 2001 From: Christopher Tremblay Date: Wed, 4 Nov 2020 02:20:17 -0800 Subject: [PATCH] Swapped strstr for strchr where applicable strstr calls with a single character were changed to strchr which should be more efficient. --- tasmota/support_command.ino | 8 ++++---- tasmota/support_tasmota.ino | 4 ++-- tasmota/tasmota.ino | 2 +- tasmota/xdrv_01_webserver.ino | 4 ++-- tasmota/xdrv_04_light.ino | 2 +- tasmota/xdrv_05_irremote.ino | 2 +- tasmota/xdrv_05_irremote_full.ino | 2 +- tasmota/xdrv_07_domoticz.ino | 2 +- tasmota/xdrv_10_rules.ino | 2 +- tasmota/xdrv_11_knx.ino | 6 +++--- tasmota/xsns_02_analog.ino | 2 +- tasmota/xsns_34_hx711.ino | 14 +++++++------- tasmota/xsns_68_windmeter.ino | 10 +++++----- 13 files changed, 30 insertions(+), 30 deletions(-) diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index cbeab8d0f..79a3f1a10 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -1242,7 +1242,7 @@ void CmndTemplate(void) bool error = false; - if (strstr(XdrvMailbox.data, "{") == nullptr) { // If no JSON it must be parameter + if (strchr(XdrvMailbox.data, '{') == nullptr) { // If no JSON it must be parameter if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload <= MAXMODULE)) { XdrvMailbox.payload--; if (ValidTemplateModule(XdrvMailbox.payload)) { @@ -1550,7 +1550,7 @@ void CmndHostname(void) { if (!XdrvMailbox.grpflg && (XdrvMailbox.data_len > 0)) { SettingsUpdateText(SET_HOSTNAME, (SC_DEFAULT == Shortcut()) ? WIFI_HOSTNAME : XdrvMailbox.data); - if (strstr(SettingsText(SET_HOSTNAME), "%") != nullptr) { + if (strchr(SettingsText(SET_HOSTNAME), '%') != nullptr) { SettingsUpdateText(SET_HOSTNAME, WIFI_HOSTNAME); } TasmotaGlobal.restart_flag = 2; @@ -1643,7 +1643,7 @@ void CmndInterlock(void) if (max_relays > sizeof(Settings.interlock[0]) * 8) { max_relays = sizeof(Settings.interlock[0]) * 8; } if (max_relays > 1) { // Only interlock with more than 1 relay if (XdrvMailbox.data_len > 0) { - if (strstr(XdrvMailbox.data, ",") != nullptr) { // Interlock entry + if (strchr(XdrvMailbox.data, ',') != nullptr) { // Interlock entry for (uint32_t i = 0; i < MAX_INTERLOCKS; i++) { Settings.interlock[i] = 0; } // Reset current interlocks char *group; char *q; @@ -1801,7 +1801,7 @@ void CmndTimeStdDst(uint32_t ts) { // TimeStd 0/1, 0/1/2/3/4, 1..12, 1..7, 0..23, +/-780 if (XdrvMailbox.data_len > 0) { - if (strstr(XdrvMailbox.data, ",") != nullptr) { // Process parameter entry + if (strchr(XdrvMailbox.data, ',') != nullptr) { // Process parameter entry uint32_t tpos = 0; // Parameter index int value = 0; char *p = XdrvMailbox.data; // Parameters like "1, 2,3 , 4 ,5, -120" or ",,,,,+240" diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index f805ee57e..afe8d4126 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -25,10 +25,10 @@ char* Format(char* output, const char* input, int size) char *token; uint32_t digits = 0; - if (strstr(input, "%") != nullptr) { + if (strchr(input, '%') != nullptr) { strlcpy(output, input, size); token = strtok(output, "%"); - if (strstr(input, "%") == input) { + if (strchr(input, '%') == input) { output[0] = '\0'; } else { token = strtok(nullptr, ""); diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 3034013a3..84eeecc66 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -280,7 +280,7 @@ void setup(void) { Format(TasmotaGlobal.mqtt_client, SettingsText(SET_MQTT_CLIENT), sizeof(TasmotaGlobal.mqtt_client)); Format(TasmotaGlobal.mqtt_topic, SettingsText(SET_MQTT_TOPIC), sizeof(TasmotaGlobal.mqtt_topic)); - if (strstr(SettingsText(SET_HOSTNAME), "%") != nullptr) { + if (strchr(SettingsText(SET_HOSTNAME), '%') != nullptr) { SettingsUpdateText(SET_HOSTNAME, WIFI_HOSTNAME); snprintf_P(TasmotaGlobal.hostname, sizeof(TasmotaGlobal.hostname)-1, SettingsText(SET_HOSTNAME), TasmotaGlobal.mqtt_topic, ESP_getChipId() & 0x1FFF); } else { diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino index 3a9cf89aa..fe7660b62 100644 --- a/tasmota/xdrv_01_webserver.ino +++ b/tasmota/xdrv_01_webserver.ino @@ -2185,7 +2185,7 @@ void WifiSaveSettings(void) WebGetArg("h", tmp, sizeof(tmp)); SettingsUpdateText(SET_HOSTNAME, (!strlen(tmp)) ? WIFI_HOSTNAME : tmp); - if (strstr(SettingsText(SET_HOSTNAME), "%") != nullptr) { + if (strchr(SettingsText(SET_HOSTNAME), '%') != nullptr) { SettingsUpdateText(SET_HOSTNAME, WIFI_HOSTNAME); } WebGetArg("c", tmp, sizeof(tmp)); @@ -3429,7 +3429,7 @@ void CmndWebSend(void) void CmndWebColor(void) { if (XdrvMailbox.data_len > 0) { - if (strstr(XdrvMailbox.data, "{") == nullptr) { // If no JSON it must be parameter + if (strchr(XdrvMailbox.data, '{') == nullptr) { // If no JSON it must be parameter if ((XdrvMailbox.data_len > 3) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= COL_LAST)) { WebHexCode(XdrvMailbox.index -1, XdrvMailbox.data); } diff --git a/tasmota/xdrv_04_light.ino b/tasmota/xdrv_04_light.ino index fb6f06dd2..5f8491dc0 100644 --- a/tasmota/xdrv_04_light.ino +++ b/tasmota/xdrv_04_light.ino @@ -2513,7 +2513,7 @@ bool LightColorEntry(char *buffer, uint32_t buffer_length) buffer_length--; // remove all trailing '=' memcpy(&Light.entry_color, &Light.current_color, sizeof(Light.entry_color)); } - if (strstr(buffer, ",") != nullptr) { // Decimal entry + if (strchr(buffer, ',') != nullptr) { // Decimal entry int8_t i = 0; for (str = strtok_r(buffer, ",", &p); str && i < 6; str = strtok_r(nullptr, ",", &p)) { if (i < LST_MAX) { diff --git a/tasmota/xdrv_05_irremote.ino b/tasmota/xdrv_05_irremote.ino index bcca685c5..65e08c160 100644 --- a/tasmota/xdrv_05_irremote.ino +++ b/tasmota/xdrv_05_irremote.ino @@ -318,7 +318,7 @@ void CmndIrSend(void) uint8_t error = IE_SYNTAX_IRSEND; if (XdrvMailbox.data_len) { - if (strstr(XdrvMailbox.data, "{") == nullptr) { + if (strchr(XdrvMailbox.data, '{') == nullptr) { error = IE_INVALID_JSON; } else { error = IrRemoteCmndIrSendJson(); diff --git a/tasmota/xdrv_05_irremote_full.ino b/tasmota/xdrv_05_irremote_full.ino index a64e87aff..73974feb6 100644 --- a/tasmota/xdrv_05_irremote_full.ino +++ b/tasmota/xdrv_05_irremote_full.ino @@ -805,7 +805,7 @@ void CmndIrSend(void) uint8_t error = IE_SYNTAX_IRSEND; if (XdrvMailbox.data_len) { - if (strstr(XdrvMailbox.data, "{") == nullptr) { + if (strchr(XdrvMailbox.data, '{') == nullptr) { error = IrRemoteCmndIrSendRaw(); } else { error = IrRemoteCmndIrSendJson(); diff --git a/tasmota/xdrv_07_domoticz.ino b/tasmota/xdrv_07_domoticz.ino index 842c1a35f..4c00f6ee5 100644 --- a/tasmota/xdrv_07_domoticz.ino +++ b/tasmota/xdrv_07_domoticz.ino @@ -497,7 +497,7 @@ void CmndDomoticzSend(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 5)) { if (XdrvMailbox.data_len > 0) { - if (strstr(XdrvMailbox.data, ",") != nullptr) { // Process parameter entry + if (strchr(XdrvMailbox.data, ',') != nullptr) { // Process parameter entry char *data; uint32_t index = strtoul(strtok_r(XdrvMailbox.data, ",", &data), nullptr, 10); if ((index > 0) && (data != nullptr)) { diff --git a/tasmota/xdrv_10_rules.ino b/tasmota/xdrv_10_rules.ino index 854c1d5a3..7cd330a38 100644 --- a/tasmota/xdrv_10_rules.ino +++ b/tasmota/xdrv_10_rules.ino @@ -2249,7 +2249,7 @@ void CmndScale(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_RULE_VARS)) { if (XdrvMailbox.data_len > 0) { - if (strstr(XdrvMailbox.data, ",") != nullptr) { // Process parameter entry + if (strchr(XdrvMailbox.data, ',') != nullptr) { // Process parameter entry char sub_string[XdrvMailbox.data_len +1]; float valueIN = CharToFloat(subStr(sub_string, XdrvMailbox.data, ",", 1)); diff --git a/tasmota/xdrv_11_knx.ino b/tasmota/xdrv_11_knx.ino index ac5bb080d..e2aa91544 100644 --- a/tasmota/xdrv_11_knx.ino +++ b/tasmota/xdrv_11_knx.ino @@ -1120,7 +1120,7 @@ void CmndKnxEnhanced(void) void CmndKnxPa(void) { if (XdrvMailbox.data_len) { - if (strstr(XdrvMailbox.data, ".") != nullptr) { // Process parameter entry + if (strchr(XdrvMailbox.data, '.') != nullptr) { // Process parameter entry char sub_string[XdrvMailbox.data_len]; int pa_area = atoi(subStr(sub_string, XdrvMailbox.data, ".", 1)); @@ -1148,7 +1148,7 @@ void CmndKnxGa(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_KNX_GA)) { if (XdrvMailbox.data_len) { - if (strstr(XdrvMailbox.data, ",") != nullptr) { // Process parameter entry + if (strchr(XdrvMailbox.data, ',') != nullptr) { // Process parameter entry char sub_string[XdrvMailbox.data_len]; int ga_option = atoi(subStr(sub_string, XdrvMailbox.data, ",", 1)); @@ -1199,7 +1199,7 @@ void CmndKnxCb(void) { if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_KNX_CB)) { if (XdrvMailbox.data_len) { - if (strstr(XdrvMailbox.data, ",") != nullptr) { // Process parameter entry + if (strchr(XdrvMailbox.data, ',') != nullptr) { // Process parameter entry char sub_string[XdrvMailbox.data_len]; int cb_option = atoi(subStr(sub_string, XdrvMailbox.data, ",", 1)); diff --git a/tasmota/xsns_02_analog.ino b/tasmota/xsns_02_analog.ino index 89281e467..ed801b384 100644 --- a/tasmota/xsns_02_analog.ino +++ b/tasmota/xsns_02_analog.ino @@ -132,7 +132,7 @@ void AdcGetSettings(uint32_t idx) { Adc[idx].param2 = 0; Adc[idx].param3 = 0; Adc[idx].param4 = 0; - if (strstr(SettingsText(SET_ADC_PARAM1 + idx), ",") != nullptr) { + if (strchr(SettingsText(SET_ADC_PARAM1 + idx), ',') != nullptr) { Adcs.type = atoi(subStr(parameters, SettingsText(SET_ADC_PARAM1 + idx), ",", 1)); Adc[idx].param1 = atoi(subStr(parameters, SettingsText(SET_ADC_PARAM1 + idx), ",", 2)); Adc[idx].param2 = atoi(subStr(parameters, SettingsText(SET_ADC_PARAM1 + idx), ",", 3)); diff --git a/tasmota/xsns_34_hx711.ino b/tasmota/xsns_34_hx711.ino index def1276a9..2894a9cad 100644 --- a/tasmota/xsns_34_hx711.ino +++ b/tasmota/xsns_34_hx711.ino @@ -205,7 +205,7 @@ bool HxCommand(void) Response_P(S_JSON_SENSOR_INDEX_SVALUE, XSNS_34, "Reset"); break; case 2: // Calibrate - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.weight_reference = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10); } Hx.scale = 1; @@ -215,26 +215,26 @@ bool HxCommand(void) HxCalibrationStateTextJson(3); break; case 3: // WeightRef to user reference - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.weight_reference = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10); } show_parms = true; break; case 4: // WeightCal to user calculated value - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.weight_calibration = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10); Hx.scale = Settings.weight_calibration; } show_parms = true; break; case 5: // WeightMax - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.weight_max = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10) / 1000; } show_parms = true; break; case 6: // WeightItem - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.weight_item = (unsigned long)(CharToFloat(subStr(sub_string, XdrvMailbox.data, ",", 2)) * 10); } show_parms = true; @@ -244,13 +244,13 @@ bool HxCommand(void) Response_P(S_JSON_SENSOR_INDEX_SVALUE, XSNS_34, D_JSON_DONE); break; case 8: // Json on weight change - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.SensorBits1.hx711_json_weight_change = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10) & 1; } show_parms = true; break; case 9: // WeightDelta - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.weight_change = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10); SetWeightDelta(); } diff --git a/tasmota/xsns_68_windmeter.ino b/tasmota/xsns_68_windmeter.ino index e30d245d2..320ae0749 100644 --- a/tasmota/xsns_68_windmeter.ino +++ b/tasmota/xsns_68_windmeter.ino @@ -294,27 +294,27 @@ bool Xsns68Cmnd(void) char sub_string[XdrvMailbox.data_len +1]; switch (XdrvMailbox.payload) { case 1: - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.windmeter_radius = (uint16_t)strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10); } break; case 2: - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.windmeter_pulses_x_rot = (uint8_t)strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10); } break; case 3: - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.windmeter_pulse_debounce = (uint16_t)strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10); } break; case 4: - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.windmeter_speed_factor = (int16_t)(CharToFloat(subStr(sub_string, XdrvMailbox.data, ",", 2)) * 1000); } break; case 5: - if (strstr(XdrvMailbox.data, ",") != nullptr) { + if (strchr(XdrvMailbox.data, ',') != nullptr) { Settings.windmeter_tele_pchange = (uint8_t)strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10); } break;