From: Dan White Date: Mon, 13 May 2013 16:48:55 +0000 (-0500) Subject: msp4th: rewrite lookupToken() X-Git-Tag: cheetah~83 X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=209ec0780798a0ef1058ef403dc365daea60d28c;p=430.git msp4th: rewrite lookupToken() Faster execution and saves 68 bytes FTW! --- diff --git a/msp4th/msp4th.c b/msp4th/msp4th.c index b9a89dc..ea2877f 100644 --- a/msp4th/msp4th.c +++ b/msp4th/msp4th.c @@ -675,57 +675,42 @@ void ndropFunc(void) } -int16_t lookupToken(uint8_t *x, uint8_t *l) // looking for x in l +int16_t lookupToken(uint8_t *word, uint8_t *list) { - int16_t i,j,k,n; + // looking for word in list + // Matches FIRST OCCURENCE of word in list + + int16_t i; + int16_t j; + int16_t k; + int16_t n; i = 0; j = 0; k = 0; n = 1; - while (l[i] != 0) { - if (x[j] != 0) { - // we expect the next char to match - if (l[i] == ' ') { - // can't match x is longer than the one we were looking at - j = 0; - n++; - while (l[i] > ' ') { - i++; - } - } else { - if (l[i] == x[j]) { - j++; - } else { - j = 0; - while (l[i] > ' ') { - i++; - } - n++; - } - } +#define CONSUMETO(c) do { while (list[i] > c) { i++; } } while (0) + while (list[i] != 0) { + if ((word[j] != 0) && (list[i] == word[j])) { + // keep matching + j++; + } else if ((word[j] == 0) && (list[i] == ' ')){ + // end of word, match iff it's the end of the list item + k = n; + //just break the while early + break; } else { - // ran out of input ... did we hit the space we expected??? - if (l[i] == ' ') { - // we found it. - k = n; - while (l[i] != 0) { - i++; - } - } else { - // missed it - j = 0; - n++; - while (l[i] > ' ') { - i++; - } - } + j = 0; + n++; + CONSUMETO(' '); } + i++; } return(k); +#undef CONSUMETO }