File Coverage

File:lib/CheckSpelling/UnknownWordSplitter.pm
Coverage:87.2%

linestmtbrancondsubtimecode
1#! -*-perl-*-
2
3# ~/bin/w
4# Search for potentially misspelled words
5# Output is:
6# misspellled
7# woord (WOORD, Woord, woord, woord's)
8package CheckSpelling::UnknownWordSplitter;
9
10
1
1
107302
3
use 5.022;
11
1
1
1
2
1
76
use feature 'unicode_strings';
12
1
1
1
8
0
13
use strict;
13
1
1
1
1
2
28
use warnings;
14
1
1
1
2
1
23
no warnings qw(experimental::vlb);
15
1
1
1
2
1
26
use Encode qw/decode_utf8 encode FB_DEFAULT/;
16
1
1
1
2
1
34
use File::Basename;
17
1
1
1
3
0
27
use Cwd 'abs_path';
18
1
1
1
2
1
28
use File::Temp qw/ tempfile tempdir /;
19
1
1
1
361
1
2835
use CheckSpelling::Util;
20our $VERSION='0.1.0';
21
22my ($longest_word, $shortest_word, $word_match, $forbidden_re, $patterns_re, $candidates_re, $disable_word_collating, $check_file_names);
23my ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
24my ($shortest, $longest) = (255, 0);
25my @forbidden_re_list;
26my @candidates_re_list;
27my $hunspell_dictionary_path;
28my @hunspell_dictionaries;
29my %dictionary = ();
30my $base_dict;
31my %unique;
32my %unique_unrecognized;
33my ($last_file, $words, $unrecognized) = ('', 0, 0);
34
35my $disable_flags;
36
37sub test_re {
38
11
8
  my ($expression) = @_;
39
11
11
6
88
  return eval { qr /$expression/ };
40}
41
42sub quote_re {
43
11
10
  my ($expression) = @_;
44
11
11
  return $expression if $expression =~ /\?\{/;
45
11
25
  $expression =~ s/
46   \G
47   (
48      (?:[^\\]|\\[^Q])*
49   )
50   (?:
51      \\Q
52      (?:[^\\]|\\[^E])*
53      (?:\\E)?
54   )?
55/
56
22
37
   $1 . (defined($2) ? quotemeta($2) : '')
57/xge;
58
11
13
  return $expression;
59}
60
61sub file_to_list {
62
3
1308
  my ($re) = @_;
63
3
3
  my @file;
64
3
30
  return @file unless open(FILE, '<:utf8', $re);
65
66
3
7
  local $/=undef;
67
3
21
  my $file=<FILE>;
68
3
9
  close FILE;
69
3
3
  my $line_number = 0;
70
3
15
  for (split /\R/, $file) {
71
12
9
    ++$line_number;
72
12
16
    next if /^#/;
73
7
2
    chomp;
74
7
25
    next unless s/^(.+)/(?:$1)/;
75
5
19
    my $quoted = quote_re($1);
76
5
6
    if (test_re $quoted) {
77
4
7
      push @file, $_;
78    } else {
79
1
2
      my $error = $@;
80
1
44
      my $home = dirname(__FILE__);
81
1
19
      $error =~ s/$home.*?\.pm line \d+\./$re line $line_number (bad-regex)/;
82
1
9
      print STDERR $error;
83
1
2
      push @file, '(?:\$^ - skipped because bad-regex)';
84    }
85  }
86
87
3
11
  return @file;
88}
89
90sub list_to_re {
91
2
2
  my (@list) = @_;
92
2
4
4
1
2
3
  @list = map { my $quoted = quote_re($_); test_re($quoted) ? $quoted : '' } @list;
93
2
4
2
5
  @list = grep { $_ ne '' } @list;
94
2
2
  return '$^' unless scalar @list;
95
2
4
  return join "|", (@list);
96}
97
98sub not_empty {
99
51
44
  my ($thing) = @_;
100
51
162
  return defined $thing && $thing ne ''
101}
102
103sub valid_word {
104  # shortest_word is an absolute
105
22
6
  our ($shortest, $longest, $shortest_word, $longest_word);
106
22
22
  $shortest = $shortest_word if $shortest_word;
107
22
21
  if ($longest_word) {
108    # longest_word is an absolute
109
20
16
    $longest = $longest_word;
110  } elsif (not_empty($longest)) {
111    # we allow for some sloppiness (a couple of stuck keys per word)
112    # it's possible that this should scale with word length
113
1
1
    $longest += 2;
114  }
115
22
16
  our ($upper_pattern, $lower_pattern, $punctuation_pattern);
116
22
66
18
151
  my $word_pattern = join '|', (grep { defined $_ && /./ } ($upper_pattern, $lower_pattern, $punctuation_pattern));
117
22
20
  $word_pattern = q<\\w|'> unless $word_pattern;
118
22
37
  if ((defined $shortest && not_empty($longest)) &&
119      ($shortest > $longest)) {
120
0
0
    $word_pattern = "(?:$word_pattern){3}";
121
0
0
    return qr/$word_pattern/;
122  }
123
22
20
  $shortest = 3 unless defined $shortest;
124
22
21
  $longest = '' unless defined $longest;
125
22
68
  $word_match = "(?:$word_pattern){$shortest,$longest}";
126
22
175
  return qr/\b$word_match\b/;
127}
128
129sub load_dictionary {
130
12
2544
  my ($dict) = @_;
131
12
8
  our ($word_match, $longest, $shortest, $longest_word, $shortest_word, %dictionary);
132
12
10
  $longest_word = CheckSpelling::Util::get_val_from_env('INPUT_LONGEST_WORD', undef);
133
12
12
  $shortest_word = CheckSpelling::Util::get_val_from_env('INPUT_SHORTEST_WORD', undef);
134
12
8
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
135
12
12
  $ignore_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_IGNORE_PATTERN', q<[^a-zA-Z']>);
136
12
45
  $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]');
137
12
28
  $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]');
138
12
26
  $not_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_LOWER_PATTERN', '[^a-z]');
139
12
28
  $not_upper_or_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_UPPER_OR_LOWER_PATTERN', '[^A-Za-z]');
140
12
23
  $punctuation_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_PUNCTUATION_PATTERN', q<'>);
141
12
27
  %dictionary = ();
142
143
12
769
  open(DICT, '<:utf8', $dict);
144
12
58
  while (!eof(DICT)) {
145
31
36
    my $word = <DICT>;
146
31
31
    chomp $word;
147
31
89
    next unless $word =~ $word_match;
148
28
25
    my $l = length $word;
149
28
22
    $longest = -1 unless not_empty($longest);
150
28
27
    $longest = $l if $l > $longest;
151
28
29
    $shortest = $l if $l < $shortest;
152
28
57
    $dictionary{$word}=1;
153  }
154
12
27
  close DICT;
155
156
12
13
  $word_match = valid_word();
157}
158
159sub hunspell_dictionary {
160
3
4
  my ($dict) = @_;
161
3
3
  my $name = $dict;
162
3
3
  $name =~ s{/src/index/hunspell/index\.dic$}{};
163
3
11
  $name =~ s{.*/}{};
164
3
2
  my $aff = $dict;
165
3
1
  my $encoding;
166
3
8
  $aff =~ s/\.dic$/.aff/;
167
3
33
  if (open AFF, '<', $aff) {
168
3
18
    while (<AFF>) {
169
0
0
      next unless /^SET\s+(\S+)/;
170
0
0
      $encoding = $1 if ($1 !~ /utf-8/i);
171
0
0
      last;
172    }
173
3
8
    close AFF;
174  }
175  return {
176
3
254
    name => $name,
177    dict => $dict,
178    aff => $aff,
179    encoding => $encoding,
180    engine => Text::Hunspell->new($aff, $dict),
181  }
182}
183
184sub init {
185
9
10440
  my ($configuration) = @_;
186
9
9
  our ($word_match, %unique, $patterns_re, @forbidden_re_list, $forbidden_re, @candidates_re_list, $candidates_re);
187
9
21
  our $sandbox = CheckSpelling::Util::get_file_from_env('sandbox', '');
188
9
17
  our $hunspell_dictionary_path = CheckSpelling::Util::get_file_from_env('hunspell_dictionary_path', '');
189
9
17
  our $timeout = CheckSpelling::Util::get_val_from_env('splitter_timeout', 30);
190
9
13
  if ($hunspell_dictionary_path) {
191
3
27
    our @hunspell_dictionaries = ();
192
1
1
1
1
1
1
1
1
1
3
164
931
18
4
1
14
5
2
17
142
    if (eval 'use Text::Hunspell; 1') {
193
3
104
      my @hunspell_dictionaries_list = glob("$hunspell_dictionary_path/*.dic");
194
3
4
      for my $hunspell_dictionary_file (@hunspell_dictionaries_list) {
195
3
5
        push @hunspell_dictionaries, hunspell_dictionary($hunspell_dictionary_file);
196      }
197    } else {
198
0
0
      print STDERR "Could not load Text::Hunspell for dictionaries (hunspell-unavailable)\n";
199    }
200  }
201
9
13
  my (@patterns_re_list, %in_patterns_re_list);
202
9
51
  if (-e "$configuration/patterns.txt") {
203
0
0
    @patterns_re_list = file_to_list "$configuration/patterns.txt";
204
0
0
    $patterns_re = list_to_re @patterns_re_list;
205
0
0
0
0
    %in_patterns_re_list = map {$_ => 1} @patterns_re_list;
206  } else {
207
9
11
    $patterns_re = undef;
208  }
209
210
9
39
  if (-e "$configuration/forbidden.txt") {
211
1
2
    @forbidden_re_list = file_to_list "$configuration/forbidden.txt";
212
1
1
    $forbidden_re = list_to_re @forbidden_re_list;
213  } else {
214
8
6
    $forbidden_re = undef;
215  }
216
217
9
40
  if (-e "$configuration/candidates.txt") {
218
1
1
    @candidates_re_list = file_to_list "$configuration/candidates.txt";
219
1
2
2
1
1
5
    @candidates_re_list = map { my $quoted = quote_re($_); $in_patterns_re_list{$_} || !test_re($quoted) ? '' : $quoted } @candidates_re_list;
220
1
1
    $candidates_re = list_to_re @candidates_re_list;
221  } else {
222
8
6
    $candidates_re = undef;
223  }
224
225
9
12
  our $largest_file = CheckSpelling::Util::get_val_from_env('INPUT_LARGEST_FILE', 1024*1024);
226
227
9
8
  my $disable_flags = CheckSpelling::Util::get_file_from_env('INPUT_DISABLE_CHECKS', '');
228
9
10
  our $disable_word_collating = $disable_flags =~ /(?:^|,|\s)word-collating(?:,|\s|$)/;
229
9
6
  our $disable_minified_file = $disable_flags =~ /(?:^|,|\s)minified-file(?:,|\s|$)/;
230
9
4
  our $disable_single_line_file = $disable_flags =~ /(?:^|,|\s)single-line-file(?:,|\s|$)/;
231
232
9
5
  our $check_file_names = CheckSpelling::Util::get_file_from_env('check_file_names', '');
233
234
9
9
  our $use_magic_file = CheckSpelling::Util::get_val_from_env('INPUT_USE_MAGIC_FILE', '');
235
236
9
9
  $word_match = valid_word();
237
238
9
15
  our $base_dict = CheckSpelling::Util::get_file_from_env('dict', "$configuration/words");
239
9
42
  $base_dict = '/usr/share/dict/words' unless -e $base_dict;
240
9
7
  load_dictionary($base_dict);
241}
242
243sub split_line {
244
1164
445
  our (%dictionary, $word_match, $disable_word_collating);
245
1164
508
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
246
1164
457
  our @hunspell_dictionaries;
247
1164
454
  our $shortest;
248
1164
576
  my $pattern = '.';
249  # $pattern = "(?:$upper_pattern){$shortest,}|$upper_pattern(?:$lower_pattern){2,}\n";
250
251  # https://www.fileformat.info/info/unicode/char/2019/
252
1164
488
  my $rsqm = "\xE2\x80\x99";
253
254
1164
622
  my ($words, $unrecognized) = (0, 0);
255
1164
735
  my ($line, $unique_ref, $unique_unrecognized_ref, $unrecognized_line_items_ref) = @_;
256
1164
5733
    $line =~ s/(?:$rsqm|&apos;|&#39;|\%27|&#8217;|&#x2019;|&rsquo;|\\u2019|\x{2019}|')+/'/g;
257
1164
2480
    $line =~ s/(?:$ignore_pattern)+/ /g;
258
1164
1809
    while ($line =~ s/($upper_pattern{2,})($upper_pattern$lower_pattern{2,})/ $1 $2 /g) {}
259
1164
4360
    while ($line =~ s/((?:$lower_pattern|$punctuation_pattern)+)($upper_pattern)/$1 $2/g) {}
260
1164
1495
    for my $token (split /\s+/, $line) {
261
5851
5002
      next unless $token =~ /$pattern/;
262
4689
3792
      $token =~ s/^(?:'|$rsqm)+//g;
263
4689
4468
      $token =~ s/(?:'|$rsqm)+s?$//g;
264
4689
2616
      my $raw_token = $token;
265
4689
2543
      $token =~ s/^[^Ii]?'+(.*)/$1/;
266
4689
2231
      $token =~ s/(.*?)'+$/$1/;
267
4689
6555
      next unless $token =~ $word_match;
268
4554
3798
      if (defined $dictionary{$token}) {
269
1028
459
        ++$words;
270
1028
554
        $unique_ref->{$token}=1;
271
1028
826
        next;
272      }
273
3526
2317
      if (@hunspell_dictionaries) {
274
3504
1622
        my $found = 0;
275
3504
1712
        for my $hunspell_dictionary (@hunspell_dictionaries) {
276          my $token_encoded = defined $hunspell_dictionary->{'encoding'} ?
277
3504
2567
            encode($hunspell_dictionary->{'encoding'}, $token) : $token;
278
3504
5840
          next unless ($hunspell_dictionary->{'engine'}->check($token_encoded));
279
0
0
          ++$words;
280
0
0
          $dictionary{$token} = 1;
281
0
0
          $unique_ref->{$token}=1;
282
0
0
          $found = 1;
283
0
0
          last;
284        }
285
3504
2303
        next if $found;
286      }
287
3526
2212
      my $key = lc $token;
288
3526
2778
      if (defined $dictionary{$key}) {
289
3
2
        ++$words;
290
3
3
        $unique_ref->{$key}=1;
291
3
4
        next;
292      }
293
3523
2314
      unless ($disable_word_collating) {
294
3523
1788
        $key =~ s/''+/'/g;
295
3523
1935
        $key =~ s/'[sd]$//;
296      }
297
3523
2622
      if (defined $dictionary{$key}) {
298
0
0
        ++$words;
299
0
0
        $unique_ref->{$key}=1;
300
0
0
        next;
301      }
302
3523
1638
      ++$unrecognized;
303
3523
1969
      $unique_unrecognized_ref->{$raw_token}=1;
304
3523
3945
      $unrecognized_line_items_ref->{$raw_token}=1;
305    }
306
1164
1551
    return ($words, $unrecognized);
307}
308
309sub skip_file {
310
15
25
  my ($temp_dir, $reason) = @_;
311
15
484
  open(SKIPPED, '>:utf8', "$temp_dir/skipped");
312
15
76
  print SKIPPED $reason;
313
15
359
  close SKIPPED;
314}
315
316sub split_file {
317
15
7204
  my ($file) = @_;
318  our (
319
15
8
    $unrecognized, $shortest, $largest_file, $words,
320    $word_match, %unique, %unique_unrecognized, $forbidden_re,
321    @forbidden_re_list, $patterns_re, %dictionary,
322    $candidates_re, @candidates_re_list, $check_file_names, $use_magic_file, $disable_minified_file,
323    $disable_single_line_file,
324    $sandbox,
325  );
326
15
9
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
327
328  # https://www.fileformat.info/info/unicode/char/2019/
329
15
12
  my $rsqm = "\xE2\x80\x99";
330
331
15
16
  my @candidates_re_hits = (0) x scalar @candidates_re_list;
332
15
13
  my @candidates_re_lines = (0) x scalar @candidates_re_list;
333
15
8
  my @forbidden_re_hits = (0) x scalar @forbidden_re_list;
334
15
12
  my @forbidden_re_lines = (0) x scalar @forbidden_re_list;
335
15
23
  my $temp_dir = tempdir(DIR=>$sandbox);
336
15
2327
  print STDERR "checking file: $file\n" if defined $ENV{'DEBUG'};
337
15
347
  open(NAME, '>', "$temp_dir/name");
338
15
27
    print NAME $file;
339
15
161
  close NAME;
340
15
49
  my $file_size = -s $file;
341
15
14
  if (defined $largest_file) {
342
15
22
    unless ($check_file_names eq $file) {
343
15
10
      if ($file_size > $largest_file) {
344
1
2
        skip_file($temp_dir, "size `$file_size` exceeds limit `$largest_file` (large-file)\n");
345
1
3
        return $temp_dir;
346      }
347    }
348  }
349
14
10
  if ($use_magic_file) {
350
8
10840
    if (open(my $file_fh, '-|',
351              '/usr/bin/file',
352              '-b',
353              '--mime',
354              '-e', 'cdf',
355              '-e', 'compress',
356              '-e', 'csv',
357              '-e', 'elf',
358              '-e', 'json',
359              '-e', 'tar',
360              $file)) {
361
8
28126
      my $file_kind = <$file_fh>;
362
8
4905
      close $file_fh;
363
8
123
      if ($file_kind =~ /^(.*?); charset=binary/) {
364
2
28
        skip_file($temp_dir, "it appears to be a binary file (`$1`) (binary-file)\n");
365
2
38
        return $temp_dir;
366      }
367    }
368  }
369
12
112
  open FILE, '<', $file;
370
12
9
  binmode FILE;
371
12
6
  my $head;
372
12
83
  read(FILE, $head, 4096);
373
12
777
  $head =~ s/(?:\r|\n)+$//;
374
12
42
  my $dos_new_lines = () = $head =~ /\r\n/gi;
375
12
28
  my $unix_new_lines = () = $head =~ /\n/gi;
376
12
84
  my $mac_new_lines = () = $head =~ /\r/gi;
377
12
48
  local $/;
378
12
59
  if ($unix_new_lines == 0 && $mac_new_lines == 0) {
379
3
6
    $/ = "\n";
380  } elsif ($dos_new_lines >= $unix_new_lines && $dos_new_lines >= $mac_new_lines) {
381
1
6
    $/ = "\r\n";
382  } elsif ($mac_new_lines > $unix_new_lines) {
383
2
6
    $/ = "\r";
384  } else {
385
6
6
    $/ = "\n";
386  }
387
12
20
  seek(FILE, 0, 0);
388
12
27
  ($words, $unrecognized) = (0, 0);
389
12
31
  %unique = ();
390
12
21
  %unique_unrecognized = ();
391
392  local $SIG{__WARN__} = sub {
393
0
0
    my $message = shift;
394
0
0
    $message =~ s/> line/> in $file - line/;
395
0
0
    chomp $message;
396
0
0
    print STDERR "$message\n";
397
12
94
  };
398
399
12
290
  open(WARNINGS, '>:utf8', "$temp_dir/warnings");
400
12
10
  our $timeout;
401
12
6
  eval {
402
12
0
77
0
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
403
12
24
    alarm $timeout;
404
405
12
13
    my $offset = 0;
406
12
82
    while (<FILE>) {
407
1167
972
      if ($. == 1) {
408
12
17
        unless ($disable_minified_file) {
409
12
43
          if ($file_size >= 512 && length($_) == $file_size) {
410
1
7
            skip_file($temp_dir, "file only has a single line (single-line-file)\n");
411
1
2
            last;
412          }
413        }
414      }
415
1166
2365
      $_ = decode_utf8($_, FB_DEFAULT);
416
1166
2635
      if (/[\x{D800}-\x{DFFF}]/) {
417
0
0
        skip_file($temp_dir, "file contains a UTF-16 surrogate -- UTF-16 surrogates are not supported (utf16-surrogate-file)\n");
418
0
0
        last;
419      }
420
1166
1372
      s/\R$//;
421
1166
1074
      s/^\x{FEFF}// if $. == 1;
422
1166
1017
      next unless /./;
423
1164
756
      my $raw_line = $_;
424
425      # hook for custom line based text exclusions:
426
1164
794
      if (defined $patterns_re) {
427
2
6
12
8
        s/($patterns_re)/"="x length($1)/ge;
428      }
429
1164
662
      my $previous_line_state = $_;
430
1164
560
      my $line_flagged;
431
1164
777
      if ($forbidden_re) {
432
9
4
56
8
        while (s/($forbidden_re)/"="x length($1)/e) {
433
4
2
          $line_flagged = 1;
434
4
8
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
435
4
3
          my $found_trigger_re;
436
4
3
          for my $i (0 .. $#forbidden_re_list) {
437
4
4
            my $forbidden_re_singleton = $forbidden_re_list[$i];
438
4
2
            my $test_line = $previous_line_state;
439
4
3
31
4
            if ($test_line =~ s/($forbidden_re_singleton)/"="x length($1)/e) {
440
3
4
              next unless $test_line eq $_;
441
3
5
              my ($begin_test, $end_test, $match_test) = ($-[0] + 1, $+[0] + 1, $1);
442
3
3
              next unless $begin == $begin_test;
443
3
2
              next unless $end == $end_test;
444
3
2
              next unless $match eq $match_test;
445
3
3
              $found_trigger_re = $forbidden_re_singleton;
446
3
5
              my $hit = "$.:$begin:$end";
447
3
3
              $forbidden_re_hits[$i]++;
448
3
3
              $forbidden_re_lines[$i] = $hit unless $forbidden_re_lines[$i];
449
3
4
              last;
450            }
451          }
452
4
5
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
453
4
4
          if ($found_trigger_re) {
454
3
7
            $found_trigger_re =~ s/^\(\?:(.*)\)$/$1/;
455
3
3
            my $quoted_trigger_re = CheckSpelling::Util::wrap_in_backticks($found_trigger_re);
456
3
12
            print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns entry: $quoted_trigger_re (forbidden-pattern)\n";
457          } else {
458
1
3
            print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns entry (forbidden-pattern)\n";
459          }
460
4
19
          $previous_line_state = $_;
461        }
462      }
463      # This is to make it easier to deal w/ rules:
464
1164
1147
      s/^/ /;
465
1164
649
      my %unrecognized_line_items = ();
466
1164
832
      my ($new_words, $new_unrecognized) = split_line($_, \%unique, \%unique_unrecognized, \%unrecognized_line_items);
467
1164
618
      $words += $new_words;
468
1164
523
      $unrecognized += $new_unrecognized;
469
1164
760
      my $line_length = length($raw_line);
470
1164
1402
      for my $token (sort CheckSpelling::Util::case_biased keys %unrecognized_line_items) {
471
1029
450
        my $found_token = 0;
472
1029
494
        my $raw_token = $token;
473
1029
516
        $token =~ s/'/(?:'|\x{2019}|\&apos;|\&#39;)+/g;
474
1029
414
        my $before;
475
1029
1516
        if ($token =~ /^$upper_pattern$lower_pattern/) {
476
5
2
          $before = '(?<=.)';
477        } elsif ($token =~ /^$upper_pattern/) {
478
0
0
          $before = "(?<!$upper_pattern)";
479        } else {
480
1024
546
          $before = "(?<=$not_lower_pattern)";
481        }
482
1029
1142
        my $after = ($token =~ /$upper_pattern$/) ? "(?=$not_upper_or_lower_pattern)|(?=$upper_pattern$lower_pattern)" : "(?=$not_lower_pattern)";
483
1029
1997
        while ($raw_line =~ /(?:\b|$before)($token)(?:\b|$after)/g) {
484
3520
1587
          $line_flagged = 1;
485
3520
1331
          $found_token = 1;
486
3520
4846
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
487
3520
3132
          next unless $match =~ /./;
488
3520
2126
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
489
3520
11923
          print WARNINGS ":$.:$begin ... $end: $wrapped\n";
490        }
491
1029
1080
        unless ($found_token) {
492
3
27
          if ($raw_line !~ /$token.*$token/ && $raw_line =~ /($token)/) {
493
3
5
            my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
494
3
2
            my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token);
495
3
12
            print WARNINGS ":$.:$begin ... $end: $wrapped\n";
496          } else {
497
0
0
            my $offset = $line_length + 1;
498
0
0
            my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token);
499
0
0
            print WARNINGS ":$.:1 ... $offset, Warning - Could not identify whole word $wrapped in line (token-is-substring)\n";
500          }
501        }
502      }
503
1164
1865
      if ($line_flagged && $candidates_re) {
504
1
1
        $_ = $previous_line_state;
505
1
1
17
2
        s/($candidates_re)/"="x length($1)/ge;
506
1
2
        if ($_ ne $previous_line_state) {
507
1
1
          $_ = $previous_line_state;
508
1
2
          for my $i (0 .. $#candidates_re_list) {
509
2
2
            my $candidate_re = $candidates_re_list[$i];
510
2
14
            next unless $candidate_re =~ /./ && $raw_line =~ /$candidate_re/;
511
1
1
5
2
            if (($_ =~ s/($candidate_re)/"="x length($1)/e)) {
512
1
1
              my ($begin, $end) = ($-[0] + 1, $+[0] + 1);
513
1
3
              my $hit = "$.:$begin:$end";
514
1
1
              $_ = $previous_line_state;
515
1
1
5
1
              my $replacements = ($_ =~ s/($candidate_re)/"="x length($1)/ge);
516
1
1
              $candidates_re_hits[$i] += $replacements;
517
1
2
              $candidates_re_lines[$i] = $hit unless $candidates_re_lines[$i];
518
1
2
              $_ = $previous_line_state;
519            }
520          }
521        }
522      }
523
1164
810
      unless ($disable_minified_file) {
524
1164
922
        s/={3,}//g;
525
1164
824
        $offset += length;
526
1164
1125
        my $ratio = int($offset / $.);
527
1164
551
        my $ratio_threshold = 1000;
528
1164
2825
        if ($ratio > $ratio_threshold) {
529
11
22
          skip_file($temp_dir, "average line width ($ratio) exceeds the threshold ($ratio_threshold) (minified-file)\n");
530        }
531      }
532    }
533
534
12
63
    alarm 0;
535  };
536
12
11
  if ($@) {
537
0
0
    die unless $@ eq "alarm\n";
538
0
0
    print WARNINGS ":$.:1 ... 1, Warning - Could not parse file within time limit (slow-file)\n";
539
0
0
    skip_file($temp_dir, "it could not be parsed file within time limit (slow-file)\n");
540  }
541
542
12
32
  close FILE;
543
12
113
  close WARNINGS;
544
545
12
36
  if ($unrecognized || @candidates_re_hits || @forbidden_re_hits) {
546
11
283
    open(STATS, '>:utf8', "$temp_dir/stats");
547
11
136
      print STATS "{words: $words, unrecognized: $unrecognized, unknown: ".(keys %unique_unrecognized).
548      ", unique: ".(keys %unique).
549      (@candidates_re_hits ? ", candidates: [".(join ',', @candidates_re_hits)."]" : "").
550      (@candidates_re_lines ? ", candidate_lines: [".(join ',', @candidates_re_lines)."]" : "").
551      (@forbidden_re_hits ? ", forbidden: [".(join ',', @forbidden_re_hits)."]" : "").
552      (@forbidden_re_lines ? ", forbidden_lines: [".(join ',', @forbidden_re_lines)."]" : "").
553      "}";
554
11
126
    close STATS;
555
11
231
    open(UNKNOWN, '>:utf8', "$temp_dir/unknown");
556
11
19
40
29
      print UNKNOWN map { "$_\n" } sort CheckSpelling::Util::case_biased keys %unique_unrecognized;
557
11
105
    close UNKNOWN;
558  }
559
560
12
93
  return $temp_dir;
561}
562
563sub main {
564
2
385
  my ($configuration, @ARGV) = @_;
565
2
1
  our %dictionary;
566
2
2
  unless (%dictionary) {
567
1
1
    init($configuration);
568  }
569
570  # read all input
571
2
2
  my @reports;
572
573
2
2
  for my $file (@ARGV) {
574
2
2
    my $temp_dir = split_file($file);
575
2
4
    push @reports, "$temp_dir\n";
576  }
577
2
6
  print join '', @reports;
578}
579
5801;