From 2ce0b5a1abcb990aa3d897c6447cb9074bdbbf92 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 7 Jul 2011 01:33:43 -0400 Subject: [PATCH] closeEnough - Implemented closeEnough, which does a truncated, caseless compare between two strings. For example, "FoO" is cloneEnough to "food." to be a match. Used to allow mixed case and abbreviated arguments. --- src/text.cpp | 12 ++++++++++++ src/text.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/text.cpp b/src/text.cpp index a32688d7d..e53ddab88 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -617,6 +617,18 @@ bool compare ( return left == right; } +//////////////////////////////////////////////////////////////////////////////// +bool closeEnough (const std::string& reference, const std::string& attempt) +{ + if (compare (reference, attempt, false)) + return true; + + if (attempt.length () < reference.length ()) + return compare (reference.substr (0, attempt.length ()), attempt, false); + + return false; +} + //////////////////////////////////////////////////////////////////////////////// std::string::size_type find ( const std::string& text, diff --git a/src/text.h b/src/text.h index 073917fe6..346877f04 100644 --- a/src/text.h +++ b/src/text.h @@ -62,6 +62,7 @@ bool isWordStart (const std::string&, std::string::size_type); bool isWordEnd (const std::string&, std::string::size_type); bool isPunctuation (char); bool compare (const std::string&, const std::string&, bool sensitive = true); +bool closeEnough (const std::string&, const std::string&); std::string::size_type find (const std::string&, const std::string&, bool sensitive = true); std::string::size_type find (const std::string&, const std::string&, std::string::size_type, bool sensitive = true); int strippedLength (const std::string&);