Why failing correcting new line at end of text file

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" );
 
O

O_TEXT

O_TEXT a écrit :
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?

$v =~ s/((?:$x000a)?)\z/\n/smg ;

In fact, it is related to an extra g in the regular expression. test3
corrects this.


sub printString ($$)
{
my ($h, $v)=@_;
my @val = unpack("U0C*", $v);
printf "$h: " ;
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 test3($)
{
my ($v)=@_;
my $x000a = pack("U0C*", 0x0a); # U+000A <control>

$v =~ s/((?:$x000a)?)\z/\n/sm ;
return $v;
}


sub test($)
{
my ($v)=@_;
printString ('input', $v);
my $r1 = test1($v);
my $r2 = test2($v);
my $r3 = test3($v);
printString ('test1', $r1);
printString ('test2', $r2);
printString ('test3', $r3);
}

test (" y\ny\n" );
test (" x\nx" );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top