File Coverage

File:lib/CheckSpelling/Homoglyph.pm
Coverage:100.0%

linestmtbrancondsubtimecode
1#! -*-perl-*-
2
3package CheckSpelling::Homoglyph;
4
5our $VERSION='0.1.0';
6our $flatten=0;
7
8
2
2
2
117046
2
4
use utf8;
9
2
2
2
160
2
464
use CheckSpelling::Util;
10
11my %homoglyph_map;
12my $homoglyphs;
13
14my %homoglyph_to_glyph;
15
16sub init {
17
16
181
    my ($file) = @_;
18
16
1
1
1
212
2
1
3
    return unless open (my $fh, '<:encoding(UTF-8)', $file);
19
15
860
    my $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]');
20
15
35
    my $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]');
21
15
26
    my $punctuation_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_PUNCTUATION_PATTERN', q<'>);
22
15
39
    local $/ = "\n";
23
15
110
    while (<$fh>) {
24
692
887
        next if /^#/;
25
691
375
        s/^\\//;
26
691
1073
        next unless /^($upper_pattern|$lower_pattern|$punctuation_pattern)(.+)$/;
27
690
549
        my ($expected, $aliases) = ($1, $2);
28
690
787
        my @chars = split('', $aliases);
29
690
284
        my @unexpected_chars;
30
690
340
        for my $ch (@chars) {
31
16219
16335
            next if $ch =~ /$upper_pattern|$lower_pattern|$punctuation_pattern/;
32
16213
6152
            my %ref;
33
16213
5998
            my $known_aliases = \%ref;
34
16213
9861
            if (defined $homoglyph_map{$ch}) {
35
15042
6841
                $known_aliases = $homoglyph_map{$ch};
36            }
37
16213
8015
            $known_aliases->{$expected} = 1;
38
16213
12788
            $homoglyph_map{$ch} = $known_aliases;
39        }
40    }
41
15
81
    close $fh;
42
15
4609
    my @glyphs = sort keys %homoglyph_map;
43
15
410
    our $homoglyphs = join '', @glyphs;
44
15
11
    our %homoglyph_to_glyph;
45
15
9
    for my $ch (@glyphs) {
46
16212
16212
5657
9511
        my @known_aliases = keys %{$homoglyph_map{$ch}};
47
16212
7683
        if (scalar @known_aliases == 1) {
48
16211
10215
            $homoglyph_to_glyph{$ch} = $known_aliases[0];
49        } else {
50
1
2
            $homoglyph_to_glyph{$ch} = $ch;
51        }
52    }
53}
54
55sub dump_aliases {
56
1
644
    my $a;
57
1
2
    for my $ch (keys %homoglyph_map) {
58
14
8
        $a = $ch;
59
14
5
        my $b = $homoglyph_map{$a};
60
14
66
        print "$a: ".(join " ", (sort keys %$b))."\n";
61    }
62
1
1
    our $homoglyphs;
63
1
3
    print "\n"."homoglyphs: $homoglyphs\n";
64
1
1
    0;
65}
661;