From cf14bd04704a9bba847def6d0f6555ed33350176 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Fri, 20 Jun 2014 07:25:59 -0400 Subject: [PATCH] Text - Migrated some utility functions over from Taskwarrior. --- src/text.cpp | 31 +++++++++++++++++++++++++++++++ src/text.h | 2 ++ 2 files changed, 33 insertions(+) diff --git a/src/text.cpp b/src/text.cpp index 5f58cd0..33243ae 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -27,6 +27,7 @@ #include #include #include +#include static void replace_positional (std::string&, const std::string&, const std::string&); @@ -78,6 +79,36 @@ std::string lowerCase (const std::string& input) return output; } +//////////////////////////////////////////////////////////////////////////////// +bool compare ( + const std::string& left, + const std::string& right, + bool sensitive /*= true*/) +{ + // Use strcasecmp if required. + if (!sensitive) + return strcasecmp (left.c_str (), right.c_str ()) == 0 ? true : false; + + // Otherwise, just use std::string::operator==. + return left == right; +} + +//////////////////////////////////////////////////////////////////////////////// +bool closeEnough ( + const std::string& reference, + const std::string& attempt, + unsigned int minLength /* = 0 */) +{ + if (compare (reference, attempt, false)) + return true; + + if (attempt.length () < reference.length () && + attempt.length () >= minLength) + return compare (reference.substr (0, attempt.length ()), attempt, false); + + return false; +} + //////////////////////////////////////////////////////////////////////////////// static void replace_positional ( std::string& fmt, diff --git a/src/text.h b/src/text.h index 29f11f1..6d5cbec 100644 --- a/src/text.h +++ b/src/text.h @@ -35,6 +35,8 @@ std::string trimRight (const std::string& in, const std::string& t = " "); std::string trim (const std::string& in, const std::string& t = " "); void split (std::vector&, const std::string&, const char); std::string lowerCase (const std::string&); +bool compare (const std::string&, const std::string&, bool sensitive = true); +bool closeEnough (const std::string&, const std::string&, unsigned int minLength = 0); const std::string format (const std::string&, const std::string&); #endif