Enhancement - caseless string compare, find

- Switched the sense of the Boolean parameter to match a more natural
  name in the .taskrc file.
This commit is contained in:
Paul Beckingham 2010-01-27 09:33:26 -05:00
parent 3e5ea8cb6c
commit 2dfe144236
3 changed files with 27 additions and 27 deletions

View file

@ -316,20 +316,20 @@ int main (int argc, char** argv)
t.notok (compare ("foo", "FOO"), "foo != foo");
// Test case-sensitive.
t.notok (compare ("foo", "xx", false), "foo != xx");
t.notok (compare ("foo", "xx", true), "foo != xx");
t.ok (compare ("foo", "foo", false), "foo == foo");
t.notok (compare ("foo", "FOO", false), "foo != FOO");
t.notok (compare ("FOO", "foo", false), "FOO != foo");
t.ok (compare ("FOO", "FOO", false), "FOO == FOO");
t.ok (compare ("foo", "foo", true), "foo == foo");
t.notok (compare ("foo", "FOO", true), "foo != FOO");
t.notok (compare ("FOO", "foo", true), "FOO != foo");
t.ok (compare ("FOO", "FOO", true), "FOO == FOO");
// Test case-insensitive.
t.notok (compare ("foo", "xx", true), "foo != foo (caseless)");
t.notok (compare ("foo", "xx", false), "foo != foo (caseless)");
t.ok (compare ("foo", "foo", true), "foo == foo (caseless)");
t.ok (compare ("foo", "FOO", true), "foo == FOO (caseless)");
t.ok (compare ("FOO", "foo", true), "FOO == foo (caseless)");
t.ok (compare ("FOO", "FOO", true), "FOO == FOO (caseless)");
t.ok (compare ("foo", "foo", false), "foo == foo (caseless)");
t.ok (compare ("foo", "FOO", false), "foo == FOO (caseless)");
t.ok (compare ("FOO", "foo", false), "FOO == foo (caseless)");
t.ok (compare ("FOO", "FOO", false), "FOO == FOO (caseless)");
// std::string::size_type find (const std::string&, const std::string&, bool caseless = false);
// Make sure degenerate cases are handled.
@ -341,20 +341,20 @@ int main (int argc, char** argv)
t.is ((int) find ("foo", "FO"), (int) std::string::npos, "foo !contains fo");
// Test case-sensitive.
t.is ((int) find ("foo", "xx", false), (int) std::string::npos, "foo !contains xx");
t.is ((int) find ("foo", "xx", true), (int) std::string::npos, "foo !contains xx");
t.is ((int) find ("foo", "fo", false), 0, "foo contains fo");
t.is ((int) find ("foo", "FO", false), (int) std::string::npos, "foo !contains fo");
t.is ((int) find ("FOO", "fo", false), (int) std::string::npos, "foo !contains fo");
t.is ((int) find ("FOO", "FO", false), 0, "foo contains fo");
t.is ((int) find ("foo", "fo", true), 0, "foo contains fo");
t.is ((int) find ("foo", "FO", true), (int) std::string::npos, "foo !contains fo");
t.is ((int) find ("FOO", "fo", true), (int) std::string::npos, "foo !contains fo");
t.is ((int) find ("FOO", "FO", true), 0, "foo contains fo");
// Test case-insensitive.
t.is ((int) find ("foo", "xx", true), (int) std::string::npos, "foo !contains xx (caseless)");
t.is ((int) find ("foo", "xx", false), (int) std::string::npos, "foo !contains xx (caseless)");
t.is ((int) find ("foo", "fo", true), 0, "foo contains fo (caseless)");
t.is ((int) find ("foo", "FO", true), 0, "foo contains FO (caseless)");
t.is ((int) find ("FOO", "fo", true), 0, "FOO contains fo (caseless)");
t.is ((int) find ("FOO", "FO", true), 0, "FOO contains FO (caseless)");
t.is ((int) find ("foo", "fo", false), 0, "foo contains fo (caseless)");
t.is ((int) find ("foo", "FO", false), 0, "foo contains FO (caseless)");
t.is ((int) find ("FOO", "fo", false), 0, "FOO contains fo (caseless)");
t.is ((int) find ("FOO", "FO", false), 0, "FOO contains FO (caseless)");
return 0;
}

View file

@ -466,10 +466,10 @@ bool isWordEnd (const std::string& input, std::string::size_type pos)
bool compare (
const std::string& left,
const std::string& right,
bool caseless /*= false*/)
bool sensitive /*= true*/)
{
// Use strcasecmp if required.
if (caseless)
if (!sensitive)
return strcasecmp (left.c_str (), right.c_str ()) == 0 ? true : false;
// Otherwise, just use std::string::operator==.
@ -480,11 +480,11 @@ bool compare (
std::string::size_type find (
const std::string& text,
const std::string& pattern,
bool caseless /*= false*/)
bool sensitive /*= true*/)
{
// Implement a caseless find, which is really just a loop withing a loop,
// Implement a sensitive find, which is really just a loop withing a loop,
// comparing lower-case versions of each character in turn.
if (caseless)
if (!sensitive)
{
// Handle empty pattern.
const char* p = pattern.c_str ();

View file

@ -54,8 +54,8 @@ bool noSpaces (const std::string&);
bool noVerticalSpace (const std::string&);
bool isWordStart (const std::string&, std::string::size_type);
bool isWordEnd (const std::string&, std::string::size_type);
bool compare (const std::string&, const std::string&, bool caseless = false);
std::string::size_type find (const std::string&, const std::string&, bool caseless = false);
bool compare (const std::string&, const std::string&, bool sensitive = true);
std::string::size_type find (const std::string&, const std::string&, bool sensitive = true);
#endif
////////////////////////////////////////////////////////////////////////////////