📄 enc2xs
字号:
}, @inc); warn join("\n", keys %e2x_dir), "\n"; for my $d (sort {$e2x_dir{$a} <=> $e2x_dir{$b}} keys %e2x_dir){ $_E2X = $d; # warn "$_E2X => ", scalar localtime($e2x_dir{$d}); return $_E2X; }}sub make_makefile_pl{ eval { require Encode; }; $@ and die "You need to install Encode to use enc2xs -M\nerror: $@\n"; # our used for variable expanstion $_Enc2xs = $0; $_Version = $VERSION; $_E2X = find_e2x(); $_Name = shift; $_TableFiles = join(",", map {qq('$_')} @_); $_Now = scalar localtime(); eval { require File::Spec; }; _print_expand(File::Spec->catfile($_E2X,"Makefile_PL.e2x"),"Makefile.PL"); _print_expand(File::Spec->catfile($_E2X,"_PM.e2x"), "$_Name.pm"); _print_expand(File::Spec->catfile($_E2X,"_T.e2x"), "t/$_Name.t"); _print_expand(File::Spec->catfile($_E2X,"README.e2x"), "README"); _print_expand(File::Spec->catfile($_E2X,"Changes.e2x"), "Changes"); exit;}use vars qw( $_ModLines $_LocalVer );sub make_configlocal_pm { eval { require Encode; }; $@ and die "Unable to require Encode: $@\n"; eval { require File::Spec; }; # our used for variable expanstion my %in_core = map { $_ => 1 } ( 'ascii', 'iso-8859-1', 'utf8', 'ascii-ctrl', 'null', 'utf-8-strict' ); my %LocalMod = (); # check @enc; use File::Find (); my $wanted = sub{ -f $_ or return; $File::Find::name =~ /\A\./ and return; $File::Find::name =~ /\.pm\z/ or return; $File::Find::name =~ m/\bEncode\b/ or return; my $mod = $File::Find::name; $mod =~ s/.*\bEncode\b/Encode/o; $mod =~ s/\.pm\z//o; $mod =~ s,/,::,og; warn qq{ require $mod;\n}; eval qq{ require $mod; }; $@ and die "Can't require $mod: $@\n"; for my $enc ( Encode->encodings() ) { no warnings; $in_core{$enc} and next; $Encode::Config::ExtModule{$enc} and next; $LocalMod{$enc} ||= $mod; } }; File::Find::find({wanted => $wanted}, @INC); $_ModLines = ""; for my $enc ( sort keys %LocalMod ) { $_ModLines .= qq(\$Encode::ExtModule{'$enc'} = "$LocalMod{$enc}";\n); } warn $_ModLines; $_LocalVer = _mkversion(); $_E2X = find_e2x(); $_Inc = $INC{"Encode.pm"}; $_Inc =~ s/\.pm$//o; _print_expand( File::Spec->catfile( $_E2X, "ConfigLocal_PM.e2x" ), File::Spec->catfile( $_Inc, "ConfigLocal.pm" ), 1 ); exit;}sub _mkversion{ # v-string is now depreciated; use time() instead; #my ($ss,$mm,$hh,$dd,$mo,$yyyy) = localtime(); #$yyyy += 1900, $mo +=1; #return sprintf("v%04d.%04d.%04d", $yyyy, $mo*100+$dd, $hh*100+$mm); return time();}sub _print_expand{ eval { require File::Basename; }; $@ and die "File::Basename needed. Are you on miniperl?;\nerror: $@\n"; File::Basename->import(); my ($src, $dst, $clobber) = @_; if (!$clobber and -e $dst){ warn "$dst exists. skipping\n"; return; } warn "Generating $dst...\n"; open my $in, $src or die "$src : $!"; if ((my $d = dirname($dst)) ne '.'){ -d $d or mkdir $d, 0755 or die "mkdir $d : $!"; } open my $out, ">$dst" or die "$!"; my $asis = 0; while (<$in>){ if (/^#### END_OF_HEADER/){ $asis = 1; next; } s/(\$_[A-Z][A-Za-z0-9]+)_/$1/gee unless $asis; print $out $_; }}__END__=head1 NAMEenc2xs -- Perl Encode Module Generator=head1 SYNOPSIS enc2xs -[options] enc2xs -M ModName mapfiles... enc2xs -C=head1 DESCRIPTIONF<enc2xs> builds a Perl extension for use by Encode from eitherUnicode Character Mapping files (.ucm) or Tcl Encoding Files (.enc).Besides being used internally during the build process of the Encodemodule, you can use F<enc2xs> to add your own encoding to perl.No knowledge of XS is necessary.=head1 Quick GuideIf you want to know as little about Perl as possible but need toadd a new encoding, just read this chapter and forget the rest.=over 4=item 0.Have a .ucm file ready. You can get it from somewhere or you can writeyour own from scratch or you can grab one from the Encode distributionand customize it. For the UCM format, see the next Chapter. In theexample below, I'll call my theoretical encoding myascii, definedin I<my.ucm>. C<$> is a shell prompt. $ ls -F my.ucm=item 1.Issue a command as follows; $ enc2xs -M My my.ucm generating Makefile.PL generating My.pm generating README generating ChangesNow take a look at your current directory. It should look like this. $ ls -F Makefile.PL My.pm my.ucm t/The following files were created. Makefile.PL - MakeMaker script My.pm - Encode submodule t/My.t - test file=over 4=item 1.1.If you want *.ucm installed together with the modules, do as follows; $ mkdir Encode $ mv *.ucm Encode $ enc2xs -M My Encode/*ucm=back=item 2.Edit the files generated. You don't have to if you have no time AND nointention to give it to someone else. But it is a good idea to editthe pod and to add more tests.=item 3.Now issue a command all Perl Mongers love: $ perl Makefile.PL Writing Makefile for Encode::My=item 4.Now all you have to do is make. $ make cp My.pm blib/lib/Encode/My.pm /usr/local/bin/perl /usr/local/bin/enc2xs -Q -O \ -o encode_t.c -f encode_t.fnm Reading myascii (myascii) Writing compiled form 128 bytes in string tables 384 bytes (75%) saved spotting duplicates 1 bytes (0.775%) saved using substrings .... chmod 644 blib/arch/auto/Encode/My/My.bs $The time it takes varies depending on how fast your machine is andhow large your encoding is. Unless you are working on something biglike euc-tw, it won't take too long.=item 5.You can "make install" already but you should test first. $ make test PERL_DL_NONLAZY=1 /usr/local/bin/perl -Iblib/arch -Iblib/lib \ -e 'use Test::Harness qw(&runtests $verbose); \ $verbose=0; runtests @ARGV;' t/*.t t/My....ok All tests successful. Files=1, Tests=2, 0 wallclock secs ( 0.09 cusr + 0.01 csys = 0.09 CPU)=item 6.If you are content with the test result, just "make install"=item 7.If you want to add your encoding to Encode's demand-loading list(so you don't have to "use Encode::YourEncoding"), run enc2xs -Cto update Encode::ConfigLocal, a module that controls local settings.After that, "use Encode;" is enough to load your encodings on demand.=back=head1 The Unicode Character MapEncode uses the Unicode Character Map (UCM) format for source charactermappings. This format is used by IBM's ICU package and was adoptedby Nick Ing-Simmons for use with the Encode module. Since UCM ismore flexible than Tcl's Encoding Map and far more user-friendly,this is the recommended format for Encode now.A UCM file looks like this. # # Comments # <code_set_name> "US-ascii" # Required <code_set_alias> "ascii" # Optional <mb_cur_min> 1 # Required; usually 1 <mb_cur_max> 1 # Max. # of bytes/char <subchar> \x3F # Substitution char # CHARMAP <U0000> \x00 |0 # <control> <U0001> \x01 |0 # <control> <U0002> \x02 |0 # <control> .... <U007C> \x7C |0 # VERTICAL LINE <U007D> \x7D |0 # RIGHT CURLY BRACKET <U007E> \x7E |0 # TILDE <U007F> \x7F |0 # <control> END CHARMAP=over 4=item *Anything that follows C<#> is treated as a comment.=item *The header section continues until a line containing the wordCHARMAP. This section has a form of I<E<lt>keywordE<gt> value>, onepair per line. Strings used as values must be quoted. Barewords aretreated as numbers. I<\xXX> represents a byte.Most of the keywords are self-explanatory. I<subchar> meanssubstitution character, not subcharacter. When you decode a Unicodesequence to this encoding but no matching character is found, the bytesequence defined here will be used. For most cases, the value here is\x3F; in ASCII, this is a question mark.=item *CHARMAP starts the character map section. Each line has a form asfollows: <UXXXX> \xXX.. |0 # comment ^ ^ ^ | | +- Fallback flag | +-------- Encoded byte sequence +-------------- Unicode Character ID in hexThe format is roughly the same as a header section except for thefallback flag: | followed by 0..3. The meaning of the possiblevalues is as follows:=over 4=item |0 Round trip safe. A character decoded to Unicode encodes back to thesame byte sequence. Most characters have this flag.=item |1Fallback for unicode -> encoding. When seen, enc2xs adds thischaracter for the encode map only.=item |2 Skip sub-char mapping should there be no code point.=item |3 Fallback for encoding -> unicode. When seen, enc2xs adds thischaracter for the decode map only.=back=item *And finally, END OF CHARMAP ends the section.=backWhen you are manually creating a UCM file, you should copy ascii.ucmor an existing encoding which is close to yours, rather than writeyour own from scratch.When you do so, make sure you leave at least B<U0000> to B<U0020> asis, unless your environment is EBCDIC.B<CAVEAT>: not all features in UCM are implemented. For example,icu:state is not used. Because of that, you need to write a perlmodule if you want to support algorithmical encodings, notablythe ISO-2022 series. Such modules include L<Encode::JP::2022_JP>,L<Encode::KR::2022_KR>, and L<Encode::TW::HZ>.=head2 Coping with duplicate mappingsWhen you create a map, you SHOULD make your mappings round-trip safe.That is, C<encode('your-encoding', decode('your-encoding', $data)) eq$data> stands for all characters that are marked as C<|0>. Here ishow to make sure:=over 4=item * Sort your map in Unicode order.=item *When you have a duplicate entry, mark either one with '|1' or '|3'. =item * And make sure the '|1' or '|3' entry FOLLOWS the '|0' entry.=backHere is an example from big5-eten. <U2550> \xF9\xF9 |0 <U2550> \xA2\xA4 |3Internally Encoding -> Unicode and Unicode -> Encoding Map looks likethis; E to U U to E -------------------------------------- \xF9\xF9 => U2550 U2550 => \xF9\xF9 \xA2\xA4 => U2550 So it is round-trip safe for \xF9\xF9. But if the line above is upsidedown, here is what happens. E to U U to E -------------------------------------- \xA2\xA4 => U2550 U2550 => \xF9\xF9 (\xF9\xF9 => U2550 is now overwritten!)The Encode package comes with F<ucmlint>, a crude but sufficientutility to check the integrity of a UCM file. Check under theEncode/bin directory for this.When in doubt, you can use F<ucmsort>, yet another utility underEncode/bin directory.=head1 Bookmarks=over 4=item *ICU Home Page L<http://oss.software.ibm.com/icu/>=item *ICU Character Mapping TablesL<http://oss.software.ibm.com/icu/charset/>=item *ICU:Conversion DataL<http://oss.software.ibm.com/icu/userguide/conversion-data.html>=back=head1 SEE ALSOL<Encode>,L<perlmod>,L<perlpod>=cut# -Q to disable the duplicate codepoint test# -S make mapping errors fatal# -q to remove comments written to output files# -O to enable the (brute force) substring optimiser# -o <output> to specify the output file name (else it's the first arg)# -f <inlist> to give a file with a list of input files (else use the args)# -n <name> to name the encoding (else use the basename of the input file.With %seen holding array refs: 865.66 real 28.80 user 8.79 sys 7904 maximum resident set size 1356 average shared memory size 18566 average unshared data size 229 average unshared stack size 46080 page reclaims 33373 page faultsWith %seen holding simple scalars: 342.16 real 27.11 user 3.54 sys 8388 maximum resident set size 1394 average shared memory size 14969 average unshared data size 236 average unshared stack size 28159 page reclaims 9839 page faultsYes, 5 minutes is faster than 15. Above is for CP936 in CN. Only difference ishow %seen is storing things its seen. So it is pathalogically bad on a 16MRAM machine, but it's going to help even on modern machines.Swapping is bad, m'kay :-)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -