Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Perl

Idiom #213 Case-insensitive string compare

Compare four strings in pair-wise variations. The string comparison can be implemented with an equality test or a containment test, must be case-insensitive and must apply Unicode casefolding.

use utf8;
use Test::More;
my @strings = ('ᾲ στο διάολο', 'ὰι στο διάολο', 'Ὰͅ ΣΤΟ ΔΙΆΟΛΟ', 'ᾺΙ ΣΤΟ ΔΙΆΟΛΟ');
my @indices = (
    [0,1], [0,2], [0,3],
    [1,0], [1,2], [1,3],
    [2,0], [2,1], [2,3],
    [3,0], [3,1], [3,2]
);
for my $tuple (@indices) {
    ok $strings[$tuple->[0]] =~ qr"\Q$strings[$tuple->[1]]"i;
}
done_testing;

in case you have trouble with copy-paste, the strings in alternative notation:

my @strings = (
"\x{1FB2} \x{3C3}\x{3C4}\x{3BF} \x{3B4}\x{3B9}\x{3AC}\x{3BF}\x{3BB}\x{3BF}",
"\x{1F70}\x{3B9} \x{3C3}\x{3C4}\x{3BF} \x{3B4}\x{3B9}\x{3AC}\x{3BF}\x{3BB}\x{3BF}",
"\x{1FBA}\x{345} \x{3A3}\x{3A4}\x{39F} \x{394}\x{399}\x{386}\x{39F}\x{39B}\x{39F}",
"\x{1FBA}\x{399} \x{3A3}\x{3A4}\x{39F} \x{394}\x{399}\x{386}\x{39F}\x{39B}\x{39F}",
);
code (C) when $a =< C andalso C =< $z -> C - $a;
code (C) when $A =< C andalso C =< $Z -> C - $A;
code(C) -> C.

isSame([C | Rest1], [C | Rest2]) when is_integer(C)->
        isSame(Rest1, Rest2);
isSame([C1 | Rest1], [C2 | Rest2]) ->
        case (code(C1) - code(C2)) of
                0 -> isSame(Rest1, Rest2);
                _ -> false
        end;
isSame([], []) -> true;
isSame(_, _) -> false.

with tail call optimization

New implementation...