Enhancement - caseless compare

- Fixed bug in text.cpp:find that failed to locate a substring if it occurred
  at the end of the string.
This commit is contained in:
Paul Beckingham 2010-01-27 16:28:57 -05:00
parent 0c5a71b02f
commit 4f1183a358
3 changed files with 62 additions and 43 deletions

View file

@ -35,72 +35,89 @@ if (open my $fh, '>', 'caseless.rc')
{ {
print $fh "data.location=.\n"; print $fh "data.location=.\n";
close $fh; close $fh;
ok (-r 'append.rc', 'Created append.rc'); ok (-r 'caseless.rc', 'Created caseless.rc');
} }
# Attempt case-sensitive and case-insensitive substitutions and filters. # Attempt case-sensitive and case-insensitive substitutions and filters.
qx{../task add one two three}; qx{../task rc:caseless.rc add one two three};
qx{../task 1 annotate four five six}; qx{../task rc:caseless.rc 1 annotate four five six};
my $output;
# Description substitution. # Description substitution.
qx{../task rc.search.case.sensitive=yes 1 /One/ONE/}; # 2
my $output = qx{../task info 1}; qx{../task rc:caseless.rc rc.search.case.sensitive:yes 1 /One/ONE/};
$output = qx{../task rc:caseless.rc info 1};
unlike ($output, qr/One two three/, 'one two three\nfour five six -> /One/ONE/ = fail'); unlike ($output, qr/One two three/, 'one two three\nfour five six -> /One/ONE/ = fail');
qx{../task rc.search.case.sensitive=no 1 /One/ONE/}; # 3
$output = qx{../task info 1}; qx{../task rc:caseless.rc rc.search.case.sensitive:no 1 /One/ONE/};
$output = qx{../task rc:caseless.rc info 1};
like ($output, qr/ONE two three/, 'one two three\nfour five six -> /One/ONE/ = caseless succeed'); like ($output, qr/ONE two three/, 'one two three\nfour five six -> /One/ONE/ = caseless succeed');
qx{../task rc.search.case.sensitive=yes 1 /one/One/}; # 4
$output = qx{../task info 1}; qx{../task rc:caseless.rc rc.search.case.sensitive:yes 1 /one/One/};
$output = qx{../task rc:caseless.rc info 1};
unlike ($output, qr/One two three/, 'ONE two three\nfour five six -> /one/ONE/ = fail'); unlike ($output, qr/One two three/, 'ONE two three\nfour five six -> /one/ONE/ = fail');
qx{../task rc.search.case.sensitive=no 1 /one/one/}; # 5
$output = qx{../task info 1}; qx{../task rc:caseless.rc rc.search.case.sensitive:no 1 /one/one/};
$output = qx{../task rc:caseless.rc info 1};
like ($output, qr/one two three/, 'ONE two three\nfour five six -> /one/one/ = caseless succeed'); like ($output, qr/one two three/, 'ONE two three\nfour five six -> /one/one/ = caseless succeed');
# Annotation substitution. # Annotation substitution.
qx{../task rc.search.case.sensitive=yes 1 /Five/FIVE/}; # 6
$output = qx{../task info 1}; qx{../task rc:caseless.rc rc.search.case.sensitive:yes 1 /Five/FIVE/};
unlike ($output, qr/four five six/, 'one two three\nfour five six -> /Five/FIVE/ = fail'); $output = qx{../task rc:caseless.rc info 1};
unlike ($output, qr/four FIVE six/, 'one two three\nfour five six -> /Five/FIVE/ = fail');
qx{../task rc.search.case.sensitive=no 1 /Five/FIVE/}; # 7
$output = qx{../task info 1}; qx{../task rc:caseless.rc rc.search.case.sensitive:no 1 /Five/FIVE/};
$output = qx{../task rc:caseless.rc info 1};
like ($output, qr/four FIVE six/, 'one two three\nfour five six -> /Five/FIVE/ = caseless succeed'); like ($output, qr/four FIVE six/, 'one two three\nfour five six -> /Five/FIVE/ = caseless succeed');
qx{../task rc.search.case.sensitive=yes 1 /five/Five/}; # 8
$output = qx{../task info 1}; qx{../task rc:caseless.rc rc.search.case.sensitive:yes 1 /five/Five/};
unlike ($output, qr/four five six/, 'one two three\nfour FIVE six -> /five/Five/ = fail'); $output = qx{../task rc:caseless.rc info 1};
unlike ($output, qr/four Five six/, 'one two three\nfour FIVE six -> /five/Five/ = fail');
qx{../task rc.search.case.sensitive=no 1 /five/five/}; # 9
$output = qx{../task info 1}; qx{../task rc:caseless.rc rc.search.case.sensitive:no 1 /five/five/};
like ($output, qr/four FIVE six/, 'one two three\nfour FIVE six -> /five/five/ = caseless succeed'); $output = qx{../task rc:caseless.rc info 1};
like ($output, qr/four five six/, 'one two three\nfour FIVE six -> /five/five/ = caseless succeed');
# Description filter. # Description filter.
$output = qx{../task rc.search.case.sensitive=yes ls One}; # 10
unlike ($output, qr/one two three/, 'one two three\bfour five six -> ls One = fail'); $output = qx{../task rc:caseless.rc rc.search.case.sensitive:yes ls One};
unlike ($output, qr/one two three/, 'one two three\nfour five six -> ls One = fail');
$output = qx{../task rc.search.case.sensitive=no ls One}; # 11
like ($output, qr/one two three/, 'one two three\bfour five six -> ls One caseless = succeed'); $output = qx{../task rc:caseless.rc rc.search.case.sensitive:no ls One};
like ($output, qr/one two three/, 'one two three\nfour five six -> ls One caseless = succeed');
$output = qx{../task rc.search.case.sensitive=yes ls Five}; # 12
unlike ($output, qr/four five six/, 'one two three\bfour five six -> ls Five = fail'); $output = qx{../task rc:caseless.rc rc.search.case.sensitive:yes ls Five};
unlike ($output, qr/four five six/, 'one two three\nfour five six -> ls Five = fail');
$output = qx{../task rc.search.case.sensitive=no ls Five}; # 13
like ($output, qr/four five six/, 'one two three\bfour five six -> ls Five caseless = succeed'); $output = qx{../task rc:caseless.rc rc.search.case.sensitive:no ls Five};
like ($output, qr/four five six/, 'one two three\nfour five six -> ls Five caseless = succeed');
# Annotation filter. # Annotation filter.
$output = qx{../task rc.search.case.sensitive=yes ls description.contains:Three}; # 14
unlike ($output, qr/one two three/, 'one two three\bfour five six -> ls description.contains:Three = fail'); $output = qx{../task rc:caseless.rc rc.search.case.sensitive:yes ls description.contains:Three};
unlike ($output, qr/one two three/, 'one two three\nfour five six -> ls description.contains:Three = fail');
$output = qx{../task rc.search.case.sensitive=no ls description.contains:Three}; # 15
like ($output, qr/one two three/, 'one two three\bfour five six -> ls description.contains:Three caseless = succeed'); $output = qx{../task rc:caseless.rc rc.search.case.sensitive:no ls description.contains:Three};
like ($output, qr/one two three/, 'one two three\nfour five six -> ls description.contains:Three caseless = succeed');
$output = qx{../task rc.search.case.sensitive=yes ls description.contains:Six}; # 16
unlike ($output, qr/four five six/, 'one two three\bfour five six -> ls description.contains:Six = fail'); $output = qx{../task rc:caseless.rc rc.search.case.sensitive:yes ls description.contains:Six};
unlike ($output, qr/four five six/, 'one two three\nfour five six -> ls description.contains:Six = fail');
$output = qx{../task rc.search.case.sensitive=no ls description.contains:Six}; # 17
like ($output, qr/four five six/, 'one two three\bfour five six -> ls description.contains:Six caseless = succeed'); $output = qx{../task rc:caseless.rc rc.search.case.sensitive:no ls description.contains:Six};
like ($output, qr/four five six/, 'one two three\nfour five six -> ls description.contains:Six caseless = succeed');
# Cleanup. # Cleanup.
unlink 'pending.data'; unlink 'pending.data';
@ -112,8 +129,8 @@ ok (!-r 'completed.data', 'Removed completed.data');
unlink 'undo.data'; unlink 'undo.data';
ok (!-r 'undo.data', 'Removed undo.data'); ok (!-r 'undo.data', 'Removed undo.data');
unlink 'append.rc'; unlink 'caseless.rc';
ok (!-r 'append.rc', 'Removed append.rc'); ok (!-r 'caseless.rc', 'Removed caseless.rc');
exit 0; exit 0;

View file

@ -34,7 +34,7 @@ Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
UnitTest t (178); UnitTest t (180);
// void wrapText (std::vector <std::string>& lines, const std::string& text, const int width) // void wrapText (std::vector <std::string>& lines, const std::string& text, const int width)
std::string text = "This is a test of the line wrapping code."; std::string text = "This is a test of the line wrapping code.";
@ -342,6 +342,7 @@ int main (int argc, char** argv)
// Test case-sensitive. // Test case-sensitive.
t.is ((int) find ("foo", "xx", true), (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", "oo", true), 1, "foo contains oo");
t.is ((int) find ("foo", "fo", true), 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");
@ -350,6 +351,7 @@ int main (int argc, char** argv)
// Test case-insensitive. // Test case-insensitive.
t.is ((int) find ("foo", "xx", false), (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", "oo", false), 1, "foo contains oo (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)");

View file

@ -497,7 +497,7 @@ std::string::size_type find (
const char* start = t; const char* start = t;
const char* end = start + text.size (); const char* end = start + text.size ();
for (; t < end - len; ++t) for (; t <= end - len; ++t)
{ {
int diff; int diff;
for (size_t i = 0; i < len; ++i) for (size_t i = 0; i < len; ++i)
@ -544,7 +544,7 @@ std::string::size_type find (
const char* t = start + begin; const char* t = start + begin;
const char* end = start + text.size (); const char* end = start + text.size ();
for (; t < end - len; ++t) for (; t <= end - len; ++t)
{ {
int diff; int diff;
for (size_t i = 0; i < len; ++i) for (size_t i = 0; i < len; ++i)