Improve: cmd_compare().

Because `cmd` is guaranteed by the compiler to be NULL-terminated, we can make use of that to ensure the loop always ends.
So we don't need to know when the next words starts.
This commit is contained in:
René van Dorst
2026-04-18 19:15:25 +02:00
parent 1a5208110c
commit 80ff1b25c9
+24 -15
View File
@@ -92,26 +92,35 @@ inline uint8_t isnumber(uint8_t l)
} }
uint8_t cmd_compare(uint8_t start, uint8_t * __code cmd) uint8_t cmd_compare(uint8_t start, __code uint8_t * cmd)
{ {
if ((start > 0) && (cmd_words_b[start] <= 0) )// nothing on this word -> no match if (cmd_words_len == 0 || start > (cmd_words_len - 1)) {
return 0; return 0;
}
uint8_t i = cmd_words_b[start];
uint8_t j = 0;
signed char i; do {
signed char j = 0; uint8_t c = cmd[j];
for (i = cmd_words_b[start]; i != cmd_words_b[start + 1] && cmd_buffer[i] != ' '; i++) { uint8_t b = cmd_buffer[i];
i &= CMD_BUF_SIZE - 1;
// print_byte(i); write_char(':'); print_byte(j); write_char('#'); print_string("\n"); // cmd is garanteerd to be NULL-terminated.
// write_char('>'); write_char(cmd[j]); write_char('-'); write_char(cmd_buffer[i]); print_string("\n"); if (c == '\0') {
if (!cmd[j]) // end of command reached, but cmd_buffer has more characters, so no match if ((b == ' ') || (b == '\0')) {
return 0; // Match
if (cmd_buffer[i] != cmd[j++]) return 1;
}
break; break;
} }
// write_char('.'); print_byte(i); write_char(':'); print_byte(j); write_char(','); print_byte (cmd[j-1]); if (b != c) {
// write_char(','); print_byte(cmd[j]); break;
if ( ((i == cmd_words_b[start + 1]) || (cmd_buffer[i] == ' ')) && !cmd[j]) // next word reached and command fully matched }
return 1;
j += 1;
i += 1;
} while (i < CMD_BUF_SIZE);
// No match
return 0; return 0;
} }