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:
@@ -316,20 +316,20 @@ int main (int argc, char** argv)
|
|||||||
t.notok (compare ("foo", "FOO"), "foo != foo");
|
t.notok (compare ("foo", "FOO"), "foo != foo");
|
||||||
|
|
||||||
// Test case-sensitive.
|
// 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.ok (compare ("foo", "foo", true), "foo == foo");
|
||||||
t.notok (compare ("foo", "FOO", false), "foo != FOO");
|
t.notok (compare ("foo", "FOO", true), "foo != FOO");
|
||||||
t.notok (compare ("FOO", "foo", false), "FOO != foo");
|
t.notok (compare ("FOO", "foo", true), "FOO != foo");
|
||||||
t.ok (compare ("FOO", "FOO", false), "FOO == FOO");
|
t.ok (compare ("FOO", "FOO", true), "FOO == FOO");
|
||||||
|
|
||||||
// Test case-insensitive.
|
// 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", false), "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", true), "FOO == foo (caseless)");
|
t.ok (compare ("FOO", "foo", false), "FOO == foo (caseless)");
|
||||||
t.ok (compare ("FOO", "FOO", true), "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);
|
// std::string::size_type find (const std::string&, const std::string&, bool caseless = false);
|
||||||
// Make sure degenerate cases are handled.
|
// 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");
|
t.is ((int) find ("foo", "FO"), (int) std::string::npos, "foo !contains fo");
|
||||||
|
|
||||||
// Test case-sensitive.
|
// 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", true), 0, "foo contains fo");
|
||||||
t.is ((int) find ("foo", "FO", false), (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", false), (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", false), 0, "foo contains fo");
|
t.is ((int) find ("FOO", "FO", true), 0, "foo contains fo");
|
||||||
|
|
||||||
// Test case-insensitive.
|
// 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", false), 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", true), 0, "FOO contains fo (caseless)");
|
t.is ((int) find ("FOO", "fo", false), 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)");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/text.cpp
10
src/text.cpp
@@ -466,10 +466,10 @@ bool isWordEnd (const std::string& input, std::string::size_type pos)
|
|||||||
bool compare (
|
bool compare (
|
||||||
const std::string& left,
|
const std::string& left,
|
||||||
const std::string& right,
|
const std::string& right,
|
||||||
bool caseless /*= false*/)
|
bool sensitive /*= true*/)
|
||||||
{
|
{
|
||||||
// Use strcasecmp if required.
|
// Use strcasecmp if required.
|
||||||
if (caseless)
|
if (!sensitive)
|
||||||
return strcasecmp (left.c_str (), right.c_str ()) == 0 ? true : false;
|
return strcasecmp (left.c_str (), right.c_str ()) == 0 ? true : false;
|
||||||
|
|
||||||
// Otherwise, just use std::string::operator==.
|
// Otherwise, just use std::string::operator==.
|
||||||
@@ -480,11 +480,11 @@ bool compare (
|
|||||||
std::string::size_type find (
|
std::string::size_type find (
|
||||||
const std::string& text,
|
const std::string& text,
|
||||||
const std::string& pattern,
|
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.
|
// comparing lower-case versions of each character in turn.
|
||||||
if (caseless)
|
if (!sensitive)
|
||||||
{
|
{
|
||||||
// Handle empty pattern.
|
// Handle empty pattern.
|
||||||
const char* p = pattern.c_str ();
|
const char* p = pattern.c_str ();
|
||||||
|
|||||||
@@ -54,8 +54,8 @@ bool noSpaces (const std::string&);
|
|||||||
bool noVerticalSpace (const std::string&);
|
bool noVerticalSpace (const std::string&);
|
||||||
bool isWordStart (const std::string&, std::string::size_type);
|
bool isWordStart (const std::string&, std::string::size_type);
|
||||||
bool isWordEnd (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);
|
bool compare (const std::string&, const std::string&, bool sensitive = true);
|
||||||
std::string::size_type find (const std::string&, const std::string&, bool caseless = false);
|
std::string::size_type find (const std::string&, const std::string&, bool sensitive = true);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user