view bin/len.pl @ 12092:ceb43c4d37de draft

<fizzie> ` for w in benvenuto bienvenido bienvenue bonvenon tervetuloa v\xc3\xa4lkommen velkomin velkommen welcome welkom wercome willkommen \xd0\xb4\xd0\xbe\xd0\xb1\xd1\x80\xd0\xbe-\xd0\xbf\xd0\xbe\xd0\xb6\xd0\xb0\xd0\xbb\xd0\xbe\xd0\xb2\xd0\xb0\xd1\x82\xd1\x8c; do sed -i -e \'2s|^|my $bin = $ENV{"HACKENV"}."/bin"; |;s|bin/[@?]|$&|g\' /hackenv/bin/$w; done
author HackEso <hackeso@esolangs.org>
date Sat, 16 Nov 2019 22:19:36 +0000
parents c989a1669243
children
line wrap: on
line source

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Encode qw/decode encode FB_CROAK LEAVE_SRC/;

sub put {
    my ($count, $item) = @_;
    if ($count == 1) {
        say "1 $item";
    } else {
        say "$count ${item}s";
    }
}

my $line = "@ARGV";

my $unicode;
eval {
    $unicode = decode "UTF-8", $line, FB_CROAK | LEAVE_SRC;
};
# Not valid UTF-8
if ($@) {
    my $modifier = length($line) == 1 ? '' : 's';
    say length($line)." byte$modifier (UTF-8 not valid)";
} else {
    my @output;
    my @graphemes = $unicode =~ /\X/g;
    my @ucs2 = $unicode =~ /[\x{10000}-\x{10FFFF}]/g;
    my $ucs2chars = @ucs2 + length $unicode;
    if (@graphemes != length $unicode) {
        put scalar @graphemes, 'grapheme';
    }
    put length $unicode, 'codepoint';
    if ($ucs2chars != length $unicode) {
        put $ucs2chars, 'Java character';
    }
    if (length $unicode != length $line) {
        put length $line, 'UTF-8 byte';
    }
}