annotate bin/len.pl @ 8041:7ae5d5b9baca

<xfix> mv len.pl bin && chmod +x bin/len
author HackBot
date Sat, 14 May 2016 14:03:52 +0000
parents len.pl@3021689c6749
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8040
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
1 #!/usr/bin/perl
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
2 use strict;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
3 use warnings;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
4 use 5.010;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
5 use Encode qw/decode encode FB_CROAK LEAVE_SRC/;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
6
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
7 sub put {
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
8 my ($count, $item) = @_;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
9 if ($count == 1) {
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
10 say "1 $item";
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
11 } else {
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
12 say "$count ${item}s";
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
13 }
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
14 }
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
15
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
16 my $line = "@ARGV";
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
17
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
18 my $unicode;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
19 eval {
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
20 $unicode = decode "UTF-8", $line, FB_CROAK | LEAVE_SRC;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
21 };
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
22 # Not valid UTF-8
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
23 if ($@) {
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
24 my $modifier = length($line) == 1 ? '' : 's';
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
25 say length($line)." byte$modifier (UTF-8 not valid)";
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
26 } else {
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
27 my @output;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
28 my @graphemes = $unicode =~ /\X/g;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
29 my @ucs2 = $unicode =~ /[\x{10000}-\x{10FFFF}]/g;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
30 my $ucs2chars = @ucs2 + length $unicode;
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
31 if (@graphemes != length $unicode) {
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
32 put scalar @graphemes, 'grapheme';
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
33 }
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
34 put length $unicode, 'codepoint';
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
35 if ($ucs2chars != length $unicode) {
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
36 put $ucs2chars, 'Java character';
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
37 }
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
38 if (length $unicode != length $line) {
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
39 put length $line, 'UTF-8 byte';
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
40 }
3021689c6749 <xfix> fetch https://gist.githubusercontent.com/xfix/3c688ed7c63e1dde5777fa7cb268d613/raw/4f9b4b4d5ff0bf5da67788058081844f890d61fd/len.pl
HackBot
parents:
diff changeset
41 }