On 07/30/2015 04:14 PM, David Emanuel da Costa Santiago wrote:
my @YENC_CHAR_MAP = map{($_+42)%256;} (0..0xffff);
my $YENC_NNTP_LINESIZE=128;
sub _yenc_encode{
my ($string) = @_;
my $column = 0;
my $content = '';
my @hexString = unpack('W*',$string); #Converts binary string to hex
for my $hexChar (@hexString) {
my $char= $YENC_CHAR_MAP[$hexChar];
when checking multiple values like this use a hash. it will be quicker
than all those == ops. or even a array using ord() for the index. you
can populate it outside the sub like this:
my @is_special ;
$is_special[0] = 1 ; # NUL
$is_special[10] = 1 ; #LF
#null || LF || CR || =
if ($char == 0 || $char == 10 || $char == 13 || $char == 61 ||
# TAB || SPC
(($char == 9 || $char == 32) && ($column == $YENC_NNTP_LINESIZE
|| $column==0)) ||
($char==46 && $column==0) # .
then a single check will do:
if ( $is_special[$char] && blah blah ) {
) {
$content =$content. '=';
ever heard of .=? it is one of my favorite perl ops. it just means
append. building strings is best done with .=. in fact a rule i use is
for one sub to build some result string but its caller decides what to
do with it. that way you can print, log, email, whatever to the string
and not change the builder code. output should be done only when you
have the full result as then you can decide what to do with the result.
too often you see code where text is generated and output immediately.
when they need to fork the output they have to resort to tied handles
and other wacky stuff. the rule to remember is
print rarely, print late.
$column+=1;
$column++ ;
$char=($char + 64);#%256;
you used += before so why not there too?
also use more horizontal white space to make your code easier to read.
}
$content = $content.chr $char;
like that looks like a .method call but perl doesn't use that syntax. .=
is the win here again.
$column+=1;
if ($column>= $YENC_NNTP_LINESIZE ) {
you can merge thos two lines:
if( ++$column >= $YENC_NNTP_LINESIZE ) {
$column=0;
$content = $content."\r\n";
.= again. see how common its usage is? because it is a assignment op
which some newbies don't learn early on, it is missed. it is just append
and simple and very needed in almost all code that builds up text.
uri
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/