У меня было довольно простое решение, если бы #include
можно было считать пробкой:
use strict;
use warnings;
use English qw<$RS>;
use English qw<@LAST_MATCH_START>;
*::STDIN = *DATA{IO};
my $old_rs = $RS;
local $RS = "*/";
local $_;
while ( <> ) {
if ( m{/[*](?:.(?!:[*]/))*\n(?sm-ix:.(?!:[*]/))*[*]/}m ) {
substr( $_, $LAST_MATCH_START[0] ) = '';
print;
last;
}
print;
last if m/^\s*#include\b/m;
}
$RS = $old_rs;
print while <>;
__DATA__
##include<stdio.h>.
// This is a c File
// This File does sampling the frequency
/* A mline comment not multi */
/* This uses the logic ...
.....
.....
*/
#include<stdio.h>
Обратите внимание, что мне пришлось изменить начальный #include
, чтобы скрипт работал. Мне показалось простым, что, если бы я хотел многострочных комментариев, самым простым решением было бы сделать '*/'
моим разделителем записей, вместо того, чтобы делать много переключений на отдельных строках.
Но оглядываясь назад, требует буферизации и сделал грязное решение:
use strict;
use warnings;
use English qw<$RS>;
use English qw<@LAST_MATCH_START>;
*::STDIN = *DATA{IO};
my $old_rs = $RS;
local $RS = "*/";
local $_;
my ( $rin, $rie );
my $mline = '';
my $buffer;
while ( <> ) {
if ( m{/[*](?:.(?!:[*]/))*\n(?sm-ix:.(?!:[*]/))*[*]/}m ) {
my $split = $LAST_MATCH_START[0];
print substr( $_, 0, $split );
$mline = substr( $_, $split );
last;
}
print;
}
$RS = $old_rs;
while ( <> ) {
$buffer .= $_;
if ( /^\s*#include\b/ ) {
$mline = '';
last;
}
}
print $mline, $buffer;
print while <>;
__DATA__
#include<stdio.h>.
// This is a c File
// This File does sampling the frequency
/* A mutli-line comment not on multiple lines */
/* This uses the logic ...
.....
.....
*/
#include<stdio.h>