From 2dfe1442362ad6a9304fd09c2571d3f772877efe Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 27 Jan 2010 09:33:26 -0500 Subject: [PATCH] Enhancement - caseless string compare, find - Switched the sense of the Boolean parameter to match a more natural name in the .taskrc file. --- src/tests/text.t.cpp | 40 ++++++++++++++++++++-------------------- src/text.cpp | 10 +++++----- src/text.h | 4 ++-- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/tests/text.t.cpp b/src/tests/text.t.cpp index f216c3d80..a8a96e20f 100644 --- a/src/tests/text.t.cpp +++ b/src/tests/text.t.cpp @@ -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; } diff --git a/src/text.cpp b/src/text.cpp index 56019c645..395fb78f8 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -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 (); diff --git a/src/text.h b/src/text.h index ba310e6b1..33571da00 100644 --- a/src/text.h +++ b/src/text.h @@ -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 ////////////////////////////////////////////////////////////////////////////////