Skip to content

Commit ad80b79

Browse files
committed
Add script to unroll C++11 range based for loops
1 parent 189d92e commit ad80b79

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

script/replace-range-for-loops.pl

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use strict;
2+
use warnings;
3+
4+
use File::Slurp;
5+
use File::Basename;
6+
use File::Spec::Functions;
7+
8+
my $root = $ENV{'SASS_LIBSASS_PATH'} || catfile(dirname($0), '..');
9+
10+
sub process($)
11+
{
12+
13+
my $count = 0;
14+
my ($file) = @_;
15+
16+
my $cpp = read_file($file, { binmode => ':raw' });
17+
18+
my $org = $cpp;
19+
20+
my $re_decl = qr/(?:const\s*)?\w+(?:\:\:\w+)*(?:\s*[\*\&])?/;
21+
my $re_val = qr/\w+(?:\(\))?(?:(?:->|\.)\w+(?:\(\))?)*/;
22+
23+
$cpp =~ s/for\s*\(\s*($re_decl)\s*(\w+)\s*:\s*(\(\*?$re_val\)|$re_val)\s*\)\s*{/
24+
$count ++;
25+
"for (auto __$2 = $3.begin(); __$2 != $3.end(); ++__$2) { $1 $2 = *(__$2); ";
26+
/gex;
27+
28+
return if $org eq $cpp || $count == 0;
29+
30+
warn sprintf "made %02d replacements in %s\n", $count, $file;
31+
32+
write_file($file, { binmode => ':raw' }, $cpp);
33+
34+
}
35+
36+
my $rv = opendir(my $dh, catfile($root, "src"));
37+
die "not found ", catfile($root, "src") unless $rv;
38+
while (my $entry = readdir($dh)) {
39+
next if $entry eq "." || $entry eq "..";
40+
next unless $entry =~ m/\.[hc]pp$/;
41+
process(catfile($root, "src", $entry));
42+
}

0 commit comments

Comments
 (0)