From 508f139f471d18821d7b214aba71e05c9007335b Mon Sep 17 00:00:00 2001 From: BlackMark Date: Mon, 6 Apr 2020 17:46:10 +0200 Subject: [PATCH] Refactor substring comparison --- fantemp/terminal.hpp | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/fantemp/terminal.hpp b/fantemp/terminal.hpp index 8a1fa3a..bb22a6e 100644 --- a/fantemp/terminal.hpp +++ b/fantemp/terminal.hpp @@ -21,6 +21,20 @@ GF(BOOTLOADER_CMD, "bootloader"); GF(VERSION_CMD, "version"); GF(VERSION, "1.1"); +static inline bool substringEquals(const char *str, const ::detail::FlashString *flashStr, const size_t &size) +{ + return (strncmp_P(str, reinterpret_cast(flashStr), size) == 0); +} + +static inline bool stringEquals(const char *str, const ::detail::FlashString *flashStr, const size_t &size) +{ + if (size == strlen_P(reinterpret_cast(flashStr))) { + return substringEquals(str, flashStr, size); + } + + return false; +} + } // namespace detail template @@ -109,20 +123,17 @@ class Terminal { if (m_inputSize) { if (m_inputBuffer[m_inputSize - 1] == CTRL_C) { handleCtrlC(); - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::HELP_CMD), m_inputSize) == 0) { + } else if (substringEquals(m_inputBuffer, detail::HELP_CMD, m_inputSize)) { printHelp(); - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::SHOW_CMD), m_inputSize) == 0) { + } else if (substringEquals(m_inputBuffer, detail::SHOW_CMD, m_inputSize)) { showState(); - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::CURVE_CMD), m_inputSize) == 0) { + } else if (substringEquals(m_inputBuffer, detail::CURVE_CMD, m_inputSize)) { printCurve(); - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::MONITOR_CMD), m_inputSize) == - 0) { + } else if (substringEquals(m_inputBuffer, detail::MONITOR_CMD, m_inputSize)) { m_state = State::MONITOR; - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::BOOTLOADER_CMD), m_inputSize) == - 0) { + } else if (substringEquals(m_inputBuffer, detail::BOOTLOADER_CMD, m_inputSize)) { handleBootloader(); - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::VERSION_CMD), m_inputSize) == - 0) { + } else if (substringEquals(m_inputBuffer, detail::VERSION_CMD, m_inputSize)) { printVersion(); } else { printUnknown(); @@ -141,8 +152,7 @@ class Terminal { m_serial << F("FanTemp command overview: ") << detail::ENDL; m_serial << detail::HELP_CMD << F(" .......: print this help message") << detail::ENDL; m_serial << detail::SHOW_CMD << F(" .......: shows current temperature and fan speed") << detail::ENDL; - m_serial << detail::CURVE_CMD << F(" ......: shows the curve used to map temperature to fan speed") - << detail::ENDL; + m_serial << detail::CURVE_CMD << F(" ......: shows mapping from temperature to fan speed") << detail::ENDL; m_serial << detail::MONITOR_CMD << F(" ....: loops the show command until Ctrl + C is pressed") << detail::ENDL; m_serial << detail::BOOTLOADER_CMD << F(" .: enters the bootloader after 10 seconds") << detail::ENDL; m_serial << detail::VERSION_CMD << F(" ....: displays firmware version") << detail::ENDL;