- Migrated some utility functions over from Taskwarrior.
This commit is contained in:
Paul Beckingham 2014-06-20 07:25:59 -04:00
parent 0d5b5bb125
commit cf14bd0470
2 changed files with 33 additions and 0 deletions

View file

@ -27,6 +27,7 @@
#include <cmake.h>
#include <vector>
#include <string>
#include <strings.h>
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,

View file

@ -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<std::string>&, 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