| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# http://perlmonks.org/?node_id=1084067 |
| 4 |
# Script by Jim |
| 5 |
#use v5.10; |
| 6 |
use strict; |
| 7 |
use warnings; |
| 8 |
use utf8; |
| 9 |
|
| 10 |
use Encode qw( encode_utf8 ); |
| 11 |
use Unicode::UCD qw( charblock ); |
| 12 |
|
| 13 |
binmode STDOUT, ':encoding(UTF-8)'; |
| 14 |
|
| 15 |
while (my $word = <DATA>) { |
| 16 |
chomp $word; |
| 17 |
|
| 18 |
my $length_in_bytes = length_in_bytes($word); |
| 19 |
my $length_in_code_points = length_in_code_points($word); |
| 20 |
my $length_in_graphemes = length_in_graphemes($word); |
| 21 |
my $code_points_in_blocks = code_points_in_blocks($word); |
| 22 |
|
| 23 |
printf "%-12s | Bytes: %2d | Code Points: %2d | Graphemes: %2d | Blocks: %s\n", |
| 24 |
$word, |
| 25 |
$length_in_bytes, |
| 26 |
$length_in_code_points, |
| 27 |
$length_in_graphemes, |
| 28 |
$code_points_in_blocks; |
| 29 |
} |
| 30 |
|
| 31 |
exit 0; |
| 32 |
|
| 33 |
sub length_in_bytes { |
| 34 |
my $word = shift; |
| 35 |
|
| 36 |
my $length = length encode_utf8($word); |
| 37 |
|
| 38 |
return $length; |
| 39 |
} |
| 40 |
|
| 41 |
sub length_in_code_points { |
| 42 |
my $word = shift; |
| 43 |
|
| 44 |
my $length = length $word; |
| 45 |
|
| 46 |
return $length; |
| 47 |
} |
| 48 |
|
| 49 |
sub length_in_graphemes { |
| 50 |
my $word = shift; |
| 51 |
|
| 52 |
my $length = () = $word =~ m/\X/g; |
| 53 |
|
| 54 |
return $length; |
| 55 |
} |
| 56 |
|
| 57 |
sub code_points_in_blocks { |
| 58 |
my $word = shift; |
| 59 |
|
| 60 |
my %total_code_points_by; |
| 61 |
my $blocks = ''; |
| 62 |
|
| 63 |
for my $character (split m//, $word) { |
| 64 |
my $block = charblock(ord $character); |
| 65 |
|
| 66 |
$total_code_points_by{$block}++; |
| 67 |
} |
| 68 |
|
| 69 |
for my $block (sort keys %total_code_points_by) { |
| 70 |
my $total = $total_code_points_by{$block}; |
| 71 |
|
| 72 |
$blocks .= sprintf "%s%s (%d)", |
| 73 |
(length $blocks ? ', ' : ''), $block, $total; |
| 74 |
} |
| 75 |
|
| 76 |
return $blocks; |
| 77 |
} |
| 78 |
|
| 79 |
__DATA__ |
| 80 |
æ |
| 81 |
æð |
| 82 |
æða |
| 83 |
æðaber |
| 84 |
æðahnútur |
| 85 |
æðakölkun |
| 86 |
æðardúnn |
| 87 |
æðarfugl |
| 88 |
æðarkolla |
| 89 |
æðarkóngur |
| 90 |
æðarvarp |
| 91 |
æði |
| 92 |
æðimargur |
| 93 |
æðisgenginn |
| 94 |
æðiskast |
| 95 |
æðislegur |
| 96 |
æðrast |
| 97 |
æðri |
| 98 |
æðrulaus |
| 99 |
æðruleysi |
| 100 |
æðruorð |
| 101 |
æðrutónn |
| 102 |
æðstur |
| 103 |
æður |
| 104 |
æfa |