File Coverage

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

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