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
112278
3
use 5.022;
11
1
1
1
3
0
57
use feature 'unicode_strings';
12
1
1
1
2
1
9
use strict;
13
1
1
1
2
0
16
use warnings;
14
1
1
1
3
1
16
no warnings qw(experimental::vlb);
15
1
1
1
1
1
26
use Encode qw/decode_utf8 encode FB_DEFAULT/;
16
1
1
1
1
1
26
use File::Basename;
17
1
1
1
2
0
16
use Cwd 'abs_path';
18
1
1
1
2
0
20
use File::Temp qw/ tempfile tempdir /;
19
1
1
1
265
1
2801
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
5
  my ($expression) = @_;
39
11
11
7
116
  return eval { qr /$expression/ };
40}
41
42sub quote_re {
43
11
9
  my ($expression) = @_;
44
11
12
  return $expression if $expression =~ /\?\{/;
45
11
24
  $expression =~ s/
46   \G
47   (
48      (?:[^\\]|\\[^Q])*
49   )
50   (?:
51      \\Q
52      (?:[^\\]|\\[^E])*
53      (?:\\E)?
54   )?
55/
56
22
40
   $1 . (defined($2) ? quotemeta($2) : '')
57/xge;
58
11
14
  return $expression;
59}
60
61sub file_to_list {
62
3
1262
  my ($re) = @_;
63
3
2
  my @file;
64
3
25
  return @file unless open(FILE, '<:utf8', $re);
65
66
3
6
  local $/=undef;
67
3
18
  my $file=<FILE>;
68
3
7
  close FILE;
69
3
1
  my $line_number = 0;
70
3
12
  for (split /\R/, $file) {
71
12
8
    ++$line_number;
72
12
14
    next if /^#/;
73
7
5
    chomp;
74
7
21
    next unless s/^(.+)/(?:$1)/;
75
5
16
    my $quoted = quote_re($1);
76
5
5
    if (test_re $quoted) {
77
4
8
      push @file, $_;
78    } else {
79
1
2
      my $error = $@;
80
1
45
      my $home = dirname(__FILE__);
81
1
20
      $error =~ s/$home.*?\.pm line \d+\./$re line $line_number (bad-regex)/;
82
1
12
      print STDERR $error;
83
1
2
      push @file, '(?:\$^ - skipped because bad-regex)';
84    }
85  }
86
87
3
12
  return @file;
88}
89
90sub list_to_re {
91
2
2
  my (@list) = @_;
92
2
4
4
2
4
5
  @list = map { my $quoted = quote_re($_); test_re($quoted) ? $quoted : '' } @list;
93
2
4
2
4
  @list = grep { $_ ne '' } @list;
94
2
1
  return '$^' unless scalar @list;
95
2
5
  return join "|", (@list);
96}
97
98sub not_empty {
99
51
49
  my ($thing) = @_;
100
51
150
  return defined $thing && $thing ne ''
101}
102
103sub valid_word {
104  # shortest_word is an absolute
105
22
12
  our ($shortest, $longest, $shortest_word, $longest_word);
106
22
17
  $shortest = $shortest_word if $shortest_word;
107
22
17
  if ($longest_word) {
108    # longest_word is an absolute
109
20
23
    $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
12
  our ($upper_pattern, $lower_pattern, $punctuation_pattern);
116
22
66
17
144
  my $word_pattern = join '|', (grep { defined $_ && /./ } ($upper_pattern, $lower_pattern, $punctuation_pattern));
117
22
17
  $word_pattern = q<\\w|'> unless $word_pattern;
118
22
39
  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
22
  $shortest = 3 unless defined $shortest;
124
22
17
  $longest = '' unless defined $longest;
125
22
68
  $word_match = "(?:$word_pattern){$shortest,$longest}";
126
22
167
  return qr/\b$word_match\b/;
127}
128
129sub load_dictionary {
130
12
1994
  my ($dict) = @_;
131
12
7
  our ($word_match, $longest, $shortest, $longest_word, $shortest_word, %dictionary);
132
12
11
  $longest_word = CheckSpelling::Util::get_val_from_env('INPUT_LONGEST_WORD', undef);
133
12
10
  $shortest_word = CheckSpelling::Util::get_val_from_env('INPUT_SHORTEST_WORD', undef);
134
12
5
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
135
12
17
  $ignore_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_IGNORE_PATTERN', q<[^a-zA-Z']>);
136
12
37
  $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]');
137
12
24
  $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]');
138
12
21
  $not_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_LOWER_PATTERN', '[^a-z]');
139
12
21
  $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
368
  open(DICT, '<:utf8', $dict);
144
12
56
  while (!eof(DICT)) {
145
31
38
    my $word = <DICT>;
146
31
22
    chomp $word;
147
31
79
    next unless $word =~ $word_match;
148
28
26
    my $l = length $word;
149
28
20
    $longest = -1 unless not_empty($longest);
150
28
30
    $longest = $l if $l > $longest;
151
28
27
    $shortest = $l if $l < $shortest;
152
28
75
    $dictionary{$word}=1;
153  }
154
12
21
  close DICT;
155
156
12
13
  $word_match = valid_word();
157}
158
159sub hunspell_dictionary {
160
3
3
  my ($dict) = @_;
161
3
3
  my $name = $dict;
162
3
4
  $name =~ s{/src/index/hunspell/index\.dic$}{};
163
3
12
  $name =~ s{.*/}{};
164
3
4
  my $aff = $dict;
165
3
3
  my $encoding;
166
3
10
  $aff =~ s/\.dic$/.aff/;
167
3
31
  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
7
    close AFF;
174  }
175  return {
176
3
272
    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
9105
  my ($configuration) = @_;
186
9
10
  our ($word_match, %unique, $patterns_re, @forbidden_re_list, $forbidden_re, @candidates_re_list, $candidates_re);
187
9
28
  our $sandbox = CheckSpelling::Util::get_file_from_env('sandbox', '');
188
9
12
  our $hunspell_dictionary_path = CheckSpelling::Util::get_file_from_env('hunspell_dictionary_path', '');
189
9
12
  our $timeout = CheckSpelling::Util::get_val_from_env('splitter_timeout', 30);
190
9
14
  if ($hunspell_dictionary_path) {
191
3
24
    our @hunspell_dictionaries = ();
192
1
1
1
1
1
1
1
1
1
3
201
961
22
5
0
13
7
1
14
148
    if (eval 'use Text::Hunspell; 1') {
193
3
105
      my @hunspell_dictionaries_list = glob("$hunspell_dictionary_path/*.dic");
194
3
7
      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
12
  my (@patterns_re_list, %in_patterns_re_list);
202
9
56
  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
10
    $patterns_re = undef;
208  }
209
210
9
35
  if (-e "$configuration/forbidden.txt") {
211
1
2
    @forbidden_re_list = file_to_list "$configuration/forbidden.txt";
212
1
2
    $forbidden_re = list_to_re @forbidden_re_list;
213  } else {
214
8
4
    $forbidden_re = undef;
215  }
216
217
9
41
  if (-e "$configuration/candidates.txt") {
218
1
1
    @candidates_re_list = file_to_list "$configuration/candidates.txt";
219
1
2
2
1
2
6
    @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
11
  our $largest_file = CheckSpelling::Util::get_val_from_env('INPUT_LARGEST_FILE', 1024*1024);
226
227
9
9
  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
10
  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
8
  our $check_file_names = CheckSpelling::Util::get_file_from_env('check_file_names', '');
233
234
9
8
  our $use_magic_file = CheckSpelling::Util::get_val_from_env('INPUT_USE_MAGIC_FILE', '');
235
236
9
11
  $word_match = valid_word();
237
238
9
17
  our $base_dict = CheckSpelling::Util::get_file_from_env('dict', "$configuration/words");
239
9
63
  $base_dict = '/usr/share/dict/words' unless -e $base_dict;
240
9
14
  load_dictionary($base_dict);
241}
242
243sub split_line {
244
1164
462
  our (%dictionary, $word_match, $disable_word_collating);
245
1164
467
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
246
1164
449
  our @hunspell_dictionaries;
247
1164
517
  our $shortest;
248
1164
567
  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
503
  my $rsqm = "\xE2\x80\x99";
253
254
1164
635
  my ($words, $unrecognized) = (0, 0);
255
1164
784
  my ($line, $unique_ref, $unique_unrecognized_ref, $unrecognized_line_items_ref) = @_;
256
1164
6066
    $line =~ s/(?:$rsqm|&apos;|&#39;|\%27|&#8217;|&#x2019;|&rsquo;|\\u2019|\x{2019}|')+/'/g;
257
1164
2730
    $line =~ s/(?:$ignore_pattern)+/ /g;
258
1164
1765
    while ($line =~ s/($upper_pattern{2,})($upper_pattern$lower_pattern{2,})/ $1 $2 /g) {}
259
1164
4454
    while ($line =~ s/((?:$lower_pattern|$punctuation_pattern)+)($upper_pattern)/$1 $2/g) {}
260
1164
1513
    for my $token (split /\s+/, $line) {
261
5851
5277
      next unless $token =~ /$pattern/;
262
4689
3776
      $token =~ s/^(?:'|$rsqm)+//g;
263
4689
4566
      $token =~ s/(?:'|$rsqm)+s?$//g;
264
4689
2755
      my $raw_token = $token;
265
4689
2626
      $token =~ s/^[^Ii]?'+(.*)/$1/;
266
4689
2344
      $token =~ s/(.*?)'+$/$1/;
267
4689
6643
      next unless $token =~ $word_match;
268
4554
3875
      if (defined $dictionary{$token}) {
269
1028
467
        ++$words;
270
1028
530
        $unique_ref->{$token}=1;
271
1028
849
        next;
272      }
273
3526
2290
      if (@hunspell_dictionaries) {
274
3504
1581
        my $found = 0;
275
3504
1855
        for my $hunspell_dictionary (@hunspell_dictionaries) {
276          my $token_encoded = defined $hunspell_dictionary->{'encoding'} ?
277
3504
2687
            encode($hunspell_dictionary->{'encoding'}, $token) : $token;
278
3504
6084
          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
2290
        next if $found;
286      }
287
3526
2222
      my $key = lc $token;
288
3526
2938
      if (defined $dictionary{$key}) {
289
3
0
        ++$words;
290
3
3
        $unique_ref->{$key}=1;
291
3
4
        next;
292      }
293
3523
2312
      unless ($disable_word_collating) {
294
3523
1949
        $key =~ s/''+/'/g;
295
3523
1820
        $key =~ s/'[sd]$//;
296      }
297
3523
2635
      if (defined $dictionary{$key}) {
298
0
0
        ++$words;
299
0
0
        $unique_ref->{$key}=1;
300
0
0
        next;
301      }
302
3523
1774
      ++$unrecognized;
303
3523
1990
      $unique_unrecognized_ref->{$raw_token}=1;
304
3523
4199
      $unrecognized_line_items_ref->{$raw_token}=1;
305    }
306
1164
1654
    return ($words, $unrecognized);
307}
308
309sub skip_file {
310
15
26
  my ($temp_dir, $reason) = @_;
311
15
485
  open(SKIPPED, '>:utf8', "$temp_dir/skipped");
312
15
77
  print SKIPPED $reason;
313
15
405
  close SKIPPED;
314}
315
316sub split_file {
317
15
6575
  my ($file) = @_;
318  our (
319
15
9
    $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
5
  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
11
  my $rsqm = "\xE2\x80\x99";
330
331
15
15
  my @candidates_re_hits = (0) x scalar @candidates_re_list;
332
15
9
  my @candidates_re_lines = (0) x scalar @candidates_re_list;
333
15
11
  my @forbidden_re_hits = (0) x scalar @forbidden_re_list;
334
15
9
  my @forbidden_re_lines = (0) x scalar @forbidden_re_list;
335
15
23
  my $temp_dir = tempdir(DIR=>$sandbox);
336
15
2345
  print STDERR "checking file: $file\n" if defined $ENV{'DEBUG'};
337
15
348
  open(NAME, '>', "$temp_dir/name");
338
15
31
    print NAME $file;
339
15
164
  close NAME;
340
15
47
  my $file_size = -s $file;
341
15
27
  if (defined $largest_file) {
342
15
16
    unless ($check_file_names eq $file) {
343
15
12
      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
12
  if ($use_magic_file) {
350
8
11877
    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
29056
      my $file_kind = <$file_fh>;
362
8
4794
      close $file_fh;
363
8
117
      if ($file_kind =~ /^(.*?); charset=binary/) {
364
2
29
        skip_file($temp_dir, "it appears to be a binary file (`$1`) (binary-file)\n");
365
2
40
        return $temp_dir;
366      }
367    }
368  }
369
12
135
  open FILE, '<', $file;
370
12
13
  binmode FILE;
371
12
6
  my $head;
372
12
86
  read(FILE, $head, 4096);
373
12
893
  $head =~ s/(?:\r|\n)+$//;
374
12
40
  my $dos_new_lines = () = $head =~ /\r\n/gi;
375
12
38
  my $unix_new_lines = () = $head =~ /\n/gi;
376
12
98
  my $mac_new_lines = () = $head =~ /\r/gi;
377
12
49
  local $/;
378
12
68
  if ($unix_new_lines == 0 && $mac_new_lines == 0) {
379
3
5
    $/ = "\n";
380  } elsif ($dos_new_lines >= $unix_new_lines && $dos_new_lines >= $mac_new_lines) {
381
1
5
    $/ = "\r\n";
382  } elsif ($mac_new_lines > $unix_new_lines) {
383
2
6
    $/ = "\r";
384  } else {
385
6
7
    $/ = "\n";
386  }
387
12
45
  seek(FILE, 0, 0);
388
12
21
  ($words, $unrecognized) = (0, 0);
389
12
43
  %unique = ();
390
12
28
  %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
103
  };
398
399
12
323
  open(WARNINGS, '>:utf8', "$temp_dir/warnings");
400
12
7
  our $timeout;
401
12
8
  eval {
402
12
0
72
0
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
403
12
28
    alarm $timeout;
404
405
12
9
    my $offset = 0;
406
12
91
    while (<FILE>) {
407
1167
1015
      if ($. == 1) {
408
12
14
        unless ($disable_minified_file) {
409
12
47
          if ($file_size >= 512 && length($_) == $file_size) {
410
1
10
            skip_file($temp_dir, "file only has a single line (single-line-file)\n");
411
1
4
            last;
412          }
413        }
414      }
415
1166
2478
      $_ = decode_utf8($_, FB_DEFAULT);
416
1166
2718
      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
1461
      s/\R$//;
421
1166
1018
      s/^\x{FEFF}// if $. == 1;
422
1166
1079
      next unless /./;
423
1164
753
      my $raw_line = $_;
424
425      # hook for custom line based text exclusions:
426
1164
812
      if (defined $patterns_re) {
427
2
6
12
8
        s/($patterns_re)/"="x length($1)/ge;
428      }
429
1164
620
      my $previous_line_state = $_;
430
1164
538
      my $line_flagged;
431
1164
779
      if ($forbidden_re) {
432
9
4
57
8
        while (s/($forbidden_re)/"="x length($1)/e) {
433
4
4
          $line_flagged = 1;
434
4
23
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
435
4
3
          my $found_trigger_re;
436
4
4
          for my $i (0 .. $#forbidden_re_list) {
437
4
3
            my $forbidden_re_singleton = $forbidden_re_list[$i];
438
4
3
            my $test_line = $previous_line_state;
439
4
3
33
4
            if ($test_line =~ s/($forbidden_re_singleton)/"="x length($1)/e) {
440
3
4
              next unless $test_line eq $_;
441
3
6
              my ($begin_test, $end_test, $match_test) = ($-[0] + 1, $+[0] + 1, $1);
442
3
2
              next unless $begin == $begin_test;
443
3
2
              next unless $end == $end_test;
444
3
4
              next unless $match eq $match_test;
445
3
1
              $found_trigger_re = $forbidden_re_singleton;
446
3
7
              my $hit = "$.:$begin:$end";
447
3
2
              $forbidden_re_hits[$i]++;
448
3
3
              $forbidden_re_lines[$i] = $hit unless $forbidden_re_lines[$i];
449
3
5
              last;
450            }
451          }
452
4
3
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
453
4
3
          if ($found_trigger_re) {
454
3
8
            $found_trigger_re =~ s/^\(\?:(.*)\)$/$1/;
455
3
3
            my $quoted_trigger_re = CheckSpelling::Util::wrap_in_backticks($found_trigger_re);
456
3
14
            print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns entry: $quoted_trigger_re (forbidden-pattern)\n";
457          } else {
458
1
4
            print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns entry (forbidden-pattern)\n";
459          }
460
4
18
          $previous_line_state = $_;
461        }
462      }
463      # This is to make it easier to deal w/ rules:
464
1164
1099
      s/^/ /;
465
1164
683
      my %unrecognized_line_items = ();
466
1164
988
      my ($new_words, $new_unrecognized) = split_line($_, \%unique, \%unique_unrecognized, \%unrecognized_line_items);
467
1164
646
      $words += $new_words;
468
1164
496
      $unrecognized += $new_unrecognized;
469
1164
809
      my $line_length = length($raw_line);
470
1164
1477
      for my $token (sort CheckSpelling::Util::case_biased keys %unrecognized_line_items) {
471
1029
483
        my $found_token = 0;
472
1029
465
        my $raw_token = $token;
473
1029
524
        $token =~ s/'/(?:'|\x{2019}|\&apos;|\&#39;)+/g;
474
1029
429
        my $before;
475
1029
1516
        if ($token =~ /^$upper_pattern$lower_pattern/) {
476
5
3
          $before = '(?<=.)';
477        } elsif ($token =~ /^$upper_pattern/) {
478
0
0
          $before = "(?<!$upper_pattern)";
479        } else {
480
1024
608
          $before = "(?<=$not_lower_pattern)";
481        }
482
1029
1147
        my $after = ($token =~ /$upper_pattern$/) ? "(?=$not_upper_or_lower_pattern)|(?=$upper_pattern$lower_pattern)" : "(?=$not_lower_pattern)";
483
1029
2285
        while ($raw_line =~ /(?:\b|$before)($token)(?:\b|$after)/g) {
484
3520
1644
          $line_flagged = 1;
485
3520
1414
          $found_token = 1;
486
3520
4966
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
487
3520
2938
          next unless $match =~ /./;
488
3520
2202
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
489
3520
11941
          print WARNINGS ":$.:$begin ... $end: $wrapped\n";
490        }
491
1029
1126
        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
3
            my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token);
495
3
11
            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
1850
      if ($line_flagged && $candidates_re) {
504
1
1
        $_ = $previous_line_state;
505
1
1
19
2
        s/($candidates_re)/"="x length($1)/ge;
506
1
2
        if ($_ ne $previous_line_state) {
507
1
1
          $_ = $previous_line_state;
508
1
1
          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
6
2
            if (($_ =~ s/($candidate_re)/"="x length($1)/e)) {
512
1
2
              my ($begin, $end) = ($-[0] + 1, $+[0] + 1);
513
1
2
              my $hit = "$.:$begin:$end";
514
1
1
              $_ = $previous_line_state;
515
1
1
4
2
              my $replacements = ($_ =~ s/($candidate_re)/"="x length($1)/ge);
516
1
1
              $candidates_re_hits[$i] += $replacements;
517
1
1
              $candidates_re_lines[$i] = $hit unless $candidates_re_lines[$i];
518
1
3
              $_ = $previous_line_state;
519            }
520          }
521        }
522      }
523
1164
897
      unless ($disable_minified_file) {
524
1164
974
        s/={3,}//g;
525
1164
795
        $offset += length;
526
1164
1011
        my $ratio = int($offset / $.);
527
1164
628
        my $ratio_threshold = 1000;
528
1164
2854
        if ($ratio > $ratio_threshold) {
529
11
23
          skip_file($temp_dir, "average line width ($ratio) exceeds the threshold ($ratio_threshold) (minified-file)\n");
530        }
531      }
532    }
533
534
12
76
    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
37
  close FILE;
543
12
121
  close WARNINGS;
544
545
12
28
  if ($unrecognized || @candidates_re_hits || @forbidden_re_hits) {
546
11
330
    open(STATS, '>:utf8', "$temp_dir/stats");
547
11
150
      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
122
    close STATS;
555
11
247
    open(UNKNOWN, '>:utf8', "$temp_dir/unknown");
556
11
19
64
33
      print UNKNOWN map { "$_\n" } sort CheckSpelling::Util::case_biased keys %unique_unrecognized;
557
11
98
    close UNKNOWN;
558  }
559
560
12
118
  return $temp_dir;
561}
562
563sub main {
564
2
407
  my ($configuration, @ARGV) = @_;
565
2
0
  our %dictionary;
566
2
4
  unless (%dictionary) {
567
1
1
    init($configuration);
568  }
569
570  # read all input
571
2
1
  my @reports;
572
573
2
2
  for my $file (@ARGV) {
574
2
3
    my $temp_dir = split_file($file);
575
2
4
    push @reports, "$temp_dir\n";
576  }
577
2
6
  print join '', @reports;
578}
579
5801;