O
O_TEXT
I did two functions to correct new line at end of text file. One works
the other does not.
A text file generally ends by one new line, but sometimes does not, for
whatever reason. This leaded perl to have both \z and \Z.
The aim of my function is to transform a buffer, appending a new line at
the end of buffer, if missing.
Function/routine test1 does it successfully.
Function/routine test2 appends an excessive new line when new line is
yet present as illustrated by example here after.
Even if I will try to do the job an other way, I wonder why test2 fails?
sub printString ($)
{
my ($v)=@_;
my @val = unpack("U0C*", $v);
for my $i ( @val)
{
printf "%x " , $i ;
}
print "\n" ;
}
sub test1($)
{
my ($v)=@_;
$v =~ s/([^\x{000A}])\z/$1\n/smg ;
return $v;
}
sub test2($)
{
my ($v)=@_;
my $x000a = pack("U0C*", 0x0a); # U+000A <control>
$v =~ s/((?:$x000a)?)\z/\n/smg ;
return $v;
}
sub test($)
{
my ($v)=@_;
printString ($v);
my $r1 = test1($v);
my $r2 = test2($v);
printString ($r1);
printString ($r2);
print "1>" . $r1 . "<1 2>" . $r2 ."<2\n";
}
test (" y\ny\n" );
test (" x\nx" );
the other does not.
A text file generally ends by one new line, but sometimes does not, for
whatever reason. This leaded perl to have both \z and \Z.
The aim of my function is to transform a buffer, appending a new line at
the end of buffer, if missing.
Function/routine test1 does it successfully.
Function/routine test2 appends an excessive new line when new line is
yet present as illustrated by example here after.
Even if I will try to do the job an other way, I wonder why test2 fails?
sub printString ($)
{
my ($v)=@_;
my @val = unpack("U0C*", $v);
for my $i ( @val)
{
printf "%x " , $i ;
}
print "\n" ;
}
sub test1($)
{
my ($v)=@_;
$v =~ s/([^\x{000A}])\z/$1\n/smg ;
return $v;
}
sub test2($)
{
my ($v)=@_;
my $x000a = pack("U0C*", 0x0a); # U+000A <control>
$v =~ s/((?:$x000a)?)\z/\n/smg ;
return $v;
}
sub test($)
{
my ($v)=@_;
printString ($v);
my $r1 = test1($v);
my $r2 = test2($v);
printString ($r1);
printString ($r2);
print "1>" . $r1 . "<1 2>" . $r2 ."<2\n";
}
test (" y\ny\n" );
test (" x\nx" );