From d56c101948bd69fdc9e7541eb6ac38ebc0e6c8bf Mon Sep 17 00:00:00 2001 From: Evan Martin Date: Wed, 4 Jan 2012 15:47:29 -0800 Subject: allow spellcheck to be used with a vector of strings --- src/ninja.cc | 7 ++++--- src/util.cc | 28 +++++++++++++++++----------- src/util.h | 8 ++++++-- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/ninja.cc b/src/ninja.cc index a747c48..64ab454 100644 --- a/src/ninja.cc +++ b/src/ninja.cc @@ -494,9 +494,10 @@ int RunTool(const string& tool, Globals* globals, int argc, char** argv) { return tools[i].func(globals, argc, argv); } - const char* suggestion = SpellcheckString(tool, - "graph", "query", "browse", "targets", "rules", "commands", "clean", - "list", NULL); + vector words; + for (int i = 0; tools[i].name; ++i) + words.push_back(tools[i].name); + const char* suggestion = SpellcheckStringV(tool, words); if (suggestion) { Error("unknown tool '%s', did you mean '%s'?", tool.c_str(), suggestion); } else { diff --git a/src/util.cc b/src/util.cc index 63bd2cd..f65dcb3 100644 --- a/src/util.cc +++ b/src/util.cc @@ -196,29 +196,35 @@ int64_t GetTimeMillis() { #endif } -const char* SpellcheckString(const string& text, ...) { +const char* SpellcheckStringV(const string& text, + const vector& words) { const bool kAllowReplacements = true; const int kMaxValidEditDistance = 3; - va_list ap; - va_start(ap, text); - const char* correct_spelling; - int min_distance = kMaxValidEditDistance + 1; const char* result = NULL; - while ((correct_spelling = va_arg(ap, const char*))) { - int distance = EditDistance( - correct_spelling, text, kAllowReplacements, kMaxValidEditDistance); + for (vector::const_iterator i = words.begin(); + i != words.end(); ++i) { + int distance = EditDistance(*i, text, kAllowReplacements, + kMaxValidEditDistance); if (distance < min_distance) { min_distance = distance; - result = correct_spelling; + result = *i; } } - - va_end(ap); return result; } +const char* SpellcheckString(const string& text, ...) { + va_list ap; + va_start(ap, text); + vector words; + const char* word; + while ((word = va_arg(ap, const char*))) + words.push_back(word); + return SpellcheckStringV(text, words); +} + #ifdef WIN32 string GetLastErrorString() { DWORD err = GetLastError(); diff --git a/src/util.h b/src/util.h index 69bf80c..f582847 100644 --- a/src/util.h +++ b/src/util.h @@ -23,6 +23,7 @@ #endif #include +#include using namespace std; #define NINJA_UNUSED_ARG(arg_name) (void)arg_name; @@ -55,8 +56,11 @@ void SetCloseOnExec(int fd); /// time. int64_t GetTimeMillis(); -/// Given a misspelled string and a NULL-terminatd list of correct spellings, -/// returns the closest match or NULL if there is no close enough match. +/// Given a misspelled string and a list of correct spellings, returns +/// the closest match or NULL if there is no close enough match. +const char* SpellcheckStringV(const string& text, const vector& words); + +/// Like SpellcheckStringV, but takes a NULL-terminated list. const char* SpellcheckString(const string& text, ...); #ifdef _WIN32 -- cgit v0.12