Skip to content

Commit a056e7a

Browse files
committed
Add script to unroll C++11 range based for loops
1 parent 8282da7 commit a056e7a

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

script/replace-range-for-loops.pl

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# this script may be used to replace `for(auto : xyz)` loops
2+
# with a more std compliant form that also compiles on gcc 4.4
3+
# albeit completely unsupported by libsass, you may use it
4+
# if there is no way to upgrade your local compiler. Mileage
5+
# may vary, and I want to stress again that this is unsupported.
6+
7+
use strict;
8+
use warnings;
9+
10+
use File::Slurp;
11+
use File::Basename;
12+
use File::Spec::Functions;
13+
14+
warn "YOU ARE ENTERING UNSUPPORTED LAND !!!!\n";
15+
warn "DON'T POST BUGS WHEN USING GCC 4.4 !!!!\n";
16+
17+
my $root = $ENV{'SASS_LIBSASS_PATH'} || catfile(dirname($0), '..');
18+
19+
sub process($)
20+
{
21+
22+
my $count = 0;
23+
my ($file) = @_;
24+
25+
my $cpp = read_file($file, { binmode => ':raw' });
26+
27+
my $org = $cpp;
28+
29+
my $re_decl = qr/(?:const\s*)?\w+(?:\:\:\w+)*(?:\s*[\*\&])?/;
30+
my $re_val = qr/\w+(?:\(\))?(?:(?:->|\.)\w+(?:\(\))?)*/;
31+
32+
$cpp =~ s/for\s*\(\s*($re_decl)\s*(\w+)\s*:\s*(\(\*?$re_val\)|\*?$re_val)\s*\)\s*{/
33+
$count ++;
34+
"for (auto __$2 = $3.begin(); __$2 != $3.end(); ++__$2) { $1 $2 = *(__$2);";
35+
/gex;
36+
37+
return if $org eq $cpp || $count == 0;
38+
39+
warn sprintf "made %02d replacements in %s\n", $count, $file;
40+
41+
write_file($file, { binmode => ':raw' }, $cpp);
42+
43+
}
44+
45+
sub processdir($)
46+
{
47+
my $rv = opendir(my $dh, $_[0]);
48+
die "not found ", $_[0] unless $rv;
49+
while (my $entry = readdir($dh)) {
50+
next if $entry eq "." || $entry eq "..";
51+
next unless $entry =~ m/\.[hc]pp$/;
52+
if (-d $_[0]) { process(catfile($_[0], $entry)); }
53+
elsif (-f $_[0]) { process(catfile($_[0], $entry)); }
54+
}
55+
}
56+
57+
processdir catfile($root, "src");

0 commit comments

Comments
 (0)