|
| 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