File: unicode

package info (click to toggle)
libdate-manip-perl 6.98-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,928 kB
  • sloc: perl: 222,846; sh: 54; ansic: 26; makefile: 8
file content (104 lines) | stat: -rwxr-xr-x 1,812 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/perl

# http://perlmonks.org/?node_id=1084067
# Script by Jim
#use v5.10;
use strict;
use warnings;
use utf8;

use Encode qw( encode_utf8 );
use Unicode::UCD qw( charblock );

binmode STDOUT, ':encoding(UTF-8)';

while (my $word = <DATA>) {
    chomp $word;

    my $length_in_bytes       = length_in_bytes($word);
    my $length_in_code_points = length_in_code_points($word);
    my $length_in_graphemes   = length_in_graphemes($word);
    my $code_points_in_blocks = code_points_in_blocks($word);

    printf "%-12s | Bytes: %2d | Code Points: %2d | Graphemes: %2d | Blocks: %s\n",
        $word,
        $length_in_bytes,
        $length_in_code_points,
        $length_in_graphemes,
        $code_points_in_blocks;
}

exit 0;

sub length_in_bytes {
    my $word = shift;

    my $length = length encode_utf8($word);

    return $length;
}

sub length_in_code_points {
    my $word = shift;

    my $length = length $word;

    return $length;
}

sub length_in_graphemes {
    my $word = shift;

    my $length = () = $word =~ m/\X/g;

    return $length;
}

sub code_points_in_blocks {
    my $word = shift;

    my %total_code_points_by;
    my $blocks = '';

    for my $character (split m//, $word) {
        my $block = charblock(ord $character);

        $total_code_points_by{$block}++;
    }

    for my $block (sort keys %total_code_points_by) {
        my $total = $total_code_points_by{$block};

        $blocks .= sprintf "%s%s (%d)",
                   (length $blocks ? ', ' : ''), $block, $total;
    }

    return $blocks;
}

__DATA__
æ
æð
æða
æðaber
æðahnútur
æðakölkun
æðardúnn
æðarfugl
æðarkolla
æðarkóngur
æðarvarp
æði
æðimargur
æðisgenginn
æðiskast
æðislegur
æðrast
æðri
æðrulaus
æðruleysi
æðruorð
æðrutónn
æðstur
æður
æfa