diff options
author | Evan Martin <martine@danga.com> | 2012-01-04 23:47:29 (GMT) |
---|---|---|
committer | Evan Martin <martine@danga.com> | 2012-01-04 23:56:29 (GMT) |
commit | d56c101948bd69fdc9e7541eb6ac38ebc0e6c8bf (patch) | |
tree | e7193e2fa4aaa2c15752215f6a4fef12c6044d50 | |
parent | 4f6f015b7f43099a2514c8e75308d81dcd1b8e7d (diff) | |
download | Ninja-d56c101948bd69fdc9e7541eb6ac38ebc0e6c8bf.zip Ninja-d56c101948bd69fdc9e7541eb6ac38ebc0e6c8bf.tar.gz Ninja-d56c101948bd69fdc9e7541eb6ac38ebc0e6c8bf.tar.bz2 |
allow spellcheck to be used with a vector of strings
-rw-r--r-- | src/ninja.cc | 7 | ||||
-rw-r--r-- | src/util.cc | 28 | ||||
-rw-r--r-- | 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<const char*> 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<const char*>& 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 char*>::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<const char*> 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(); @@ -23,6 +23,7 @@ #endif #include <string> +#include <vector> 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<const char*>& words); + +/// Like SpellcheckStringV, but takes a NULL-terminated list. const char* SpellcheckString(const string& text, ...); #ifdef _WIN32 |