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
112023
2
4
use utf8;
9
2
2
2
167
2
509
use CheckSpelling::Util;
10
11my %homoglyph_map;
12my $homoglyphs;
13
14my %homoglyph_to_glyph;
15
16sub init {
17
13
171
    my ($file) = @_;
18
13
1
1
1
158
2
1
2
    return unless open (my $fh, '<:encoding(UTF-8)', $file);
19
12
732
    my $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]');
20
12
28
    my $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]');
21
12
21
    my $punctuation_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_PUNCTUATION_PATTERN', q<'>);
22
12
32
    local $/ = "\n";
23
12
115
    while (<$fh>) {
24
545
715
        next if /^#/;
25
544
268
        s/^\\//;
26
544
838
        next unless /^($upper_pattern|$lower_pattern|$punctuation_pattern)(.+)$/;
27
543
431
        my ($expected, $aliases) = ($1, $2);
28
543
617
        my @chars = split('', $aliases);
29
543
207
        my @unexpected_chars;
30
543
298
        for my $ch (@chars) {
31
12748
13546
            next if $ch =~ /$upper_pattern|$lower_pattern|$punctuation_pattern/;
32
12742
5228
            my %ref;
33
12742
5160
            my $known_aliases = \%ref;
34
12742
7933
            if (defined $homoglyph_map{$ch}) {
35
11571
5689
                $known_aliases = $homoglyph_map{$ch};
36            }
37
12742
5965
            $known_aliases->{$expected} = 1;
38
12742
10475
            $homoglyph_map{$ch} = $known_aliases;
39        }
40    }
41
12
53
    close $fh;
42
12
3392
    my @glyphs = sort keys %homoglyph_map;
43
12
324
    our $homoglyphs = join '', @glyphs;
44
12
5
    our %homoglyph_to_glyph;
45
12
10
    for my $ch (@glyphs) {
46
12741
12741
4763
7650
        my @known_aliases = keys %{$homoglyph_map{$ch}};
47
12741
6599
        if (scalar @known_aliases == 1) {
48
12740
8217
            $homoglyph_to_glyph{$ch} = $known_aliases[0];
49        } else {
50
1
1
            $homoglyph_to_glyph{$ch} = $ch;
51        }
52    }
53}
54
55sub dump_aliases {
56
1
562
    my $a;
57
1
2
    for my $ch (keys %homoglyph_map) {
58
14
5
        $a = $ch;
59
14
6
        my $b = $homoglyph_map{$a};
60
14
59
        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;