Data-Binary
view release on metacpan or search on metacpan
lib/Data/Binary.pm view on Meta::CPAN
if (length($string) > 512) {
$string = substr($string, 0, 512);
}
return '' if (index($string, "\c@") != -1);
my $length = length($string);
my $odd = ($string =~ tr/\x01\x02\x03\x04\x05\x06\x07\x09\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f//d);
# Detecting >=128 and non-UTF-8 is interesting. Note that all UTF-8 >=128 has several bytes with
# >=128 set, so a quick test is possible by simply checking if any are >=128. However, the count
# from that is typically wrong, if this is binary data, it'll not have been decoded. So we do this
# in two steps.
my $copy = $string;
if (($copy =~ tr[\x80-\xff][]d) > 0) {
my $modified = decode_utf8($string, Encode::FB_DEFAULT);
my $substitions = ($modified =~ tr/\x{fffd}//d);
$odd += $substitions;
}
return '' if ($odd * 3 > $length);
( run in 0.258 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )