Created
September 5, 2009 18:34
-
-
Save sugyan/181474 to your computer and use it in GitHub Desktop.
スクリプト記号化スクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
# 変換辞書 | |
my $dict = do { | |
my $dict = {}; | |
# 使用する9種類の記号と改行コード(これらにシングルクォートが加わる) | |
my @chars = (qw/ ^ . ( ) ~ ? ! { } /, "\x0A"); | |
# 辞書の作成 | |
for my $first (@chars) { | |
for my $second (@chars) { | |
for my $third (@chars) { | |
for my $fourth (@chars) { | |
for my $fifth (@chars) { | |
my $xor = ($first ^ $second ^ $third ^ $fourth ^ $fifth); | |
push @{$dict->{$xor}}, [$first, $second, $third, $fourth, $fifth]; | |
} | |
} | |
} | |
} | |
} | |
$dict; | |
}; | |
# 記号化させるプログラム文字列 | |
my $str; | |
{ | |
local $/ = undef; | |
$str = <>; | |
} | |
$str =~ s([\"\$\@\\\{\}]) | |
(\\$&)xmsg; | |
$str = qq/eval"$str"/; | |
# "\x00"〜"\x7F"の文字列 と "\x80"〜"\xFF"の文字列 | |
# で分けてそれぞれ記号化 | |
$str =~ s{ [\x00-\x7F]+ } | |
{ '.' . symbolize($&) }xmsge; | |
$str =~ s{ [\x80-\xFF]+ } | |
{ '.~' . symbolize(~$&) }xmsge; | |
# 記号プログラムを出力する | |
print qq/''!~('(?{'$str.'})')/; | |
# 引数に渡された文字列を、記号の排他的論理和の表現に変換する | |
sub symbolize { | |
my $str = shift; | |
my ($first, $second, $third, $fourth, $fifth); | |
for my $i (0 .. length($str) - 1) { | |
my @sets = @{$dict->{substr($str, $i, 1)}}; | |
my $set = $sets[rand @sets]; | |
$first .= $set->[0]; | |
$second .= $set->[1]; | |
$third .= $set->[2]; | |
$fourth .= $set->[3]; | |
$fifth .= $set->[4]; | |
} | |
return qq/('$first'^'$second'^'$third'^'$fourth'^'$fifth')/; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment