Skip to content

Instantly share code, notes, and snippets.

@dankogai
Created March 29, 2017 04:54
Show Gist options
  • Save dankogai/1c5602447629db8cfd41d196dfe67ea7 to your computer and use it in GitHub Desktop.
Save dankogai/1c5602447629db8cfd41d196dfe67ea7 to your computer and use it in GitHub Desktop.
NFD Test
fn =\x{304b}\x{3070}\x{3093}.txt
fn_nfd =\x{304b}\x{306f}\x{3099}\x{3093}.txt
かばん.txt:No such file or directory at ./test.pl line 32.
.
..
\x{304b}\x{3070}\x{3093}.txt
fn =\x{304b}\x{3070}\x{3093}.txt
fn_nfd =\x{304b}\x{306f}\x{3099}\x{3093}.txt
食べないでくださーい
.
..
\x{304b}\x{306f}\x{3099}\x{3093}.txt
#!/usr/bin/env perl
use strict;
use warnings;
use feature ':all';
use Encode;
use Unicode::Normalize;
use utf8;
binmode STDOUT, ':utf8';
sub listdir {
my $dn = shift;
opendir my $dh, $dn or die "$dn: $!";
my @fn = map { decode_utf8($_) } readdir $dh;
closedir $dh;
map { encode( 'ascii', $_, Encode::FB_PERLQQ ) } @fn;
}
my $dir = shift or die "usage: $0 dirname";
my $fn = 'かばん.txt';
my $fn_nfd = NFD($fn);
say 'fn =', encode( 'ascii', $fn, Encode::FB_PERLQQ );
say 'fn_nfd =', encode( 'ascii', $fn_nfd, Encode::FB_PERLQQ );
eval {
my $path = "$dir/$fn";
open my $fh, '>:utf8', $path or die "$path:$!";
say $fh "食べないでくださーい";
close $fh;
};
say $@ if $@;
eval {
my $path = "$dir/$fn_nfd";
open my $fh, '<:utf8', "$dir/$fn_nfd" or die "$fn:$!";
say <$fh>;
close $fh;
};
say $@ if $@;
{
say for listdir($dir);
unlink "$dir/$fn";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment