Refactor substring comparison

This commit is contained in:
BlackMark 2020-04-06 17:46:10 +02:00
parent 45a79adc56
commit 508f139f47

View File

@ -21,6 +21,20 @@ GF(BOOTLOADER_CMD, "bootloader");
GF(VERSION_CMD, "version"); GF(VERSION_CMD, "version");
GF(VERSION, "1.1"); 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<const char *>(flashStr), size) == 0);
}
static inline bool stringEquals(const char *str, const ::detail::FlashString *flashStr, const size_t &size)
{
if (size == strlen_P(reinterpret_cast<const char *>(flashStr))) {
return substringEquals(str, flashStr, size);
}
return false;
}
} // namespace detail } // namespace detail
template <class Uart> template <class Uart>
@ -109,20 +123,17 @@ class Terminal {
if (m_inputSize) { if (m_inputSize) {
if (m_inputBuffer[m_inputSize - 1] == CTRL_C) { if (m_inputBuffer[m_inputSize - 1] == CTRL_C) {
handleCtrlC(); handleCtrlC();
} else if (strncmp_P(m_inputBuffer, reinterpret_cast<const char *>(detail::HELP_CMD), m_inputSize) == 0) { } else if (substringEquals(m_inputBuffer, detail::HELP_CMD, m_inputSize)) {
printHelp(); printHelp();
} else if (strncmp_P(m_inputBuffer, reinterpret_cast<const char *>(detail::SHOW_CMD), m_inputSize) == 0) { } else if (substringEquals(m_inputBuffer, detail::SHOW_CMD, m_inputSize)) {
showState(); showState();
} else if (strncmp_P(m_inputBuffer, reinterpret_cast<const char *>(detail::CURVE_CMD), m_inputSize) == 0) { } else if (substringEquals(m_inputBuffer, detail::CURVE_CMD, m_inputSize)) {
printCurve(); printCurve();
} else if (strncmp_P(m_inputBuffer, reinterpret_cast<const char *>(detail::MONITOR_CMD), m_inputSize) == } else if (substringEquals(m_inputBuffer, detail::MONITOR_CMD, m_inputSize)) {
0) {
m_state = State::MONITOR; m_state = State::MONITOR;
} else if (strncmp_P(m_inputBuffer, reinterpret_cast<const char *>(detail::BOOTLOADER_CMD), m_inputSize) == } else if (substringEquals(m_inputBuffer, detail::BOOTLOADER_CMD, m_inputSize)) {
0) {
handleBootloader(); handleBootloader();
} else if (strncmp_P(m_inputBuffer, reinterpret_cast<const char *>(detail::VERSION_CMD), m_inputSize) == } else if (substringEquals(m_inputBuffer, detail::VERSION_CMD, m_inputSize)) {
0) {
printVersion(); printVersion();
} else { } else {
printUnknown(); printUnknown();
@ -141,8 +152,7 @@ class Terminal {
m_serial << F("FanTemp command overview: ") << detail::ENDL; m_serial << F("FanTemp command overview: ") << detail::ENDL;
m_serial << detail::HELP_CMD << F(" .......: print this help message") << 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::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") m_serial << detail::CURVE_CMD << F(" ......: shows mapping from temperature to fan speed") << detail::ENDL;
<< detail::ENDL;
m_serial << detail::MONITOR_CMD << F(" ....: loops the show command until Ctrl + C is pressed") << 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::BOOTLOADER_CMD << F(" .: enters the bootloader after 10 seconds") << detail::ENDL;
m_serial << detail::VERSION_CMD << F(" ....: displays firmware version") << detail::ENDL; m_serial << detail::VERSION_CMD << F(" ....: displays firmware version") << detail::ENDL;