-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move character validation to separate file #846
- Loading branch information
1 parent
718c0a0
commit ecf0098
Showing
2 changed files
with
53 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env perl | ||
use warnings; | ||
use strict; | ||
use utf8; | ||
my @INFILES = glob(shift); | ||
binmode(STDOUT,'utf8'); | ||
binmode(STDERR,'utf8'); | ||
|
||
foreach my $file (@INFILES) { | ||
chars($file); | ||
} | ||
|
||
# Check if $file contains bad characters | ||
sub chars { | ||
my $file = shift; | ||
my %c; | ||
my @bad = (); | ||
my ($fName) = $file =~ m|([^/]+)$| | ||
or die "FATAL ERROR: Bad file '$file'\n"; | ||
print STDERR "INFO: Char validation for $fName\n"; | ||
open(IN, '<:utf8', $file); | ||
undef $/; | ||
my $txt = <IN>; | ||
undef %c; | ||
for my $c (split(//, $txt)) {$c{$c}++} | ||
for my $c (sort keys %c) { | ||
if (ord($c) == hex('00A0') or #NO-BREAK SPACE | ||
ord($c) == hex('2011') or #NON-BREAKING HYPHEN | ||
ord($c) == hex('00AD') or #SOFT HYPHEN | ||
ord($c) == hex('FFFD') or #REPLACEMENT CHAR | ||
(ord($c) >= hex('2000') and ord($c) <= hex('200A')) or #NON-STANDARD SPACES | ||
(ord($c) >= hex('E000') and ord($c) <= hex('F8FF')) #PUA | ||
) { | ||
my $message = sprintf("U+%X (%dx)", ord($c), $c{$c}); | ||
push(@bad, $message) | ||
} | ||
} | ||
print STDERR "WARN: File $fName contains bad chars: " . join('; ', @bad) . "\n" | ||
if @bad | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters