c – 如何在不使用perl / awk / etc更改’long long’的情况下将’long’更改为’int’
作者:互联网
我想将所有长整数改为整数(为64位代码做准备).我有一个perl行:
s/(\s*long\s+)(?!long)/ int /g
接近.它变长为int和long long to long int.
任何编辑方法都是好的,只要它可以编写脚本或在多个文件上操作.
*Sample: input desired output*
long abd; int abd;
int longname; int longname;
int reallylongname; int reallylongname;
float reallylong longname; float reallylong longname;
double namelong double namelong
long abc long int abc int
long long abc long long abc
long long hhhh long long hhhh
long long hhhh long long hhhh
long abd( long kjhh int abd( int kjhh
解决方法:
如果支持可变宽度的lookbehinds,以下将是理想的:
s/
(?<! \b long \s+)
\b long \b
(?! \s+ long \b)
/int/xg
这会让你更加困惑:
s/
\b ( (?: long \s+ )* ) long \b
/
length($1) ? $1."long" : "int"
/xeg
更新:这也有效:
s/
(?<! \b long )
(?<! \s ) \s* \K
\b long \b
(?! \s+ long \b)
/int/xg
更新:使用以下方法测试:
use strict;
use warnings;
use Test::More;
sub fix {
my ($s) = @_;
$s =~ s/
\b ( (?: long \s+ )* ) long \b
/
length($1) ? $1."long" : "int"
/xeg;
return $s;
}
my @tests = (
[ ' long abd;', ' int abd;' ],
[ 'int longname;', 'int longname;' ],
[ 'int reallylongname;', 'int reallylongname;' ],
[ 'float reallylong longname;', 'float reallylong longname;' ],
[ 'double namelong', 'double namelong' ],
[ 'long abc long', 'int abc int' ],
[ 'long long abc', 'long long abc' ],
[ 'long long hhhh', 'long long hhhh' ],
[ 'long long hhhh', 'long long hhhh' ],
[ 'long abd( long kjhh', 'int abd( int kjhh' ],
);
plan tests => 0+@tests;
for (@tests) {
my ($i,$e) = @$_;
my $g = fix($i);
is($g, $e, $i);
}
1;
标签:32bit-64bit,editing,c,perl,scripting 来源: https://codeday.me/bug/20190826/1734068.html