diff options
Diffstat (limited to 'libalee/corewords.cpp')
-rw-r--r-- | libalee/corewords.cpp | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/libalee/corewords.cpp b/libalee/corewords.cpp index d981098..1d705f4 100644 --- a/libalee/corewords.cpp +++ b/libalee/corewords.cpp @@ -214,17 +214,18 @@ execute: state.ip += sizeof(Cell); } -int CoreWords::findi(const char *word) +template<class Comp> +int findi(Comp comp) { std::size_t i = 0; int wordsi = 0; - while (i < sizeof(wordsarr)) { + while (i < sizeof(CoreWords::wordsarr)) { auto end = i; - while (wordsarr[end]) + while (CoreWords::wordsarr[end]) ++end; - if (!std::strcmp(word, wordsarr + i)) + if (comp(CoreWords::wordsarr + i, end - i)) return wordsi; ++wordsi; @@ -234,23 +235,15 @@ int CoreWords::findi(const char *word) return -1; } -int CoreWords::findi(State& state, Word word) +int CoreWords::findi(const char *word) { - std::size_t i = 0; - int wordsi = 0; - - while (i < sizeof(wordsarr)) { - auto end = i; - while (wordsarr[end]) - ++end; - - if (state.dict.equal(word, wordsarr + i, end - i)) - return wordsi; - - ++wordsi; - i = end + 1; - } + return ::findi([word](auto b, auto e) { + return !std::strncmp(word, b, e); }); +} - return -1; +int CoreWords::findi(State& state, Word word) +{ + return ::findi([state, word](auto b, auto e) { + return state.dict.equal(word, b, e); }); } |