liyujie
2025-08-29 87c7c0d90966d729ca3d39cbfca77a39a43960eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/perl
 
sub usage {
    print STDERR "Usage: findunusedtranslations values/strings.xml\n";
    print STDERR "\n";
    print STDERR "Will read values/strings.xml and rewrite\n";
    print STDERR "values-xx/strings.xml and values-xx-rYY/strings.xml\n";
    print STDERR "files to remove strings that no longer appear in the\n";
    print STDERR "base strings file.\n";
 
    exit 1;
}
 
if ($#ARGV != 0) {
    usage();
}
 
unless ($ARGV[0] =~ /^(.*)\/values([^\/]*)\/(.*\.xml)/) {
    print STDERR "Bad format for $ARGV[0]\n";
    usage();
}
 
unless (-f $ARGV[0]) {
    print STDERR "$0: $ARGV[0]: No such file\n";
}
 
$prefix = $1;
$values = $2;
$suffix = $3;
 
if ($values =~ /^(-mcc[^-]*)*(-mnc[^-]*)*(.*)$/) {
    $pattern1 = "$prefix/values$1$2-??$3/$suffix";
    $pattern2 = "$prefix/values$1$2-??-r??$3/$suffix";
} else {
    $pattern1 = "$prefix/values-??$values/$suffix";
    $pattern2 = "$prefix/values-??-r??$values/$suffix";
}
 
@matches = (glob($pattern1), glob($pattern2));
 
open(IN, "<$ARGV[0]");
while (<IN>) {
    if (/<string [^>]*name="([^"]*)"/) {
        $string{$1} = 1;
    }
    if (/<string-array [^>]*name="([^"]*)"/) {
        $stringarray{$1} = 1;
    }
    if (/<plurals [^>]*name="([^"]*)"/) {
        $plurals{$1} = 1;
    }
}
close(IN);
 
for $match (@matches) {
    print "Rewriting $match\n";
    $suppress = 0;
    $text = "";
    $changes = 0;
 
    open(IN, "<$match");
    while (<IN>) {
        if (/<string [^>]*name="([^"]*)"/) {
            if ($string{$1} == 0) {
                $suppress = 1;
                $changes = 1;
            }
        }
        if (/<string-array [^>]*name="([^"]*)"/) {
            if ($stringarray{$1} == 0) {
                $suppress = 1;
                $changes = 1;
            }
        }
        if (/<plurals [^>]*name="([^"]*)"/) {
            if ($plurals{$1} == 0) {
                $suppress = 1;
                $changes = 1;
            }
        }
 
        $text .= $_ unless ($suppress);
 
        if (/<\/string/) {
            $suppress = 0;
        }
        if (/<\/string-array/) {
            $suppress = 0;
        }
        if (/<\/plurals/) {
            $suppress = 0;
        }
    }
    close(IN);
 
    if ($changes) {
        open(OUT, ">$match");
        print OUT $text;
        close(OUT);
    } else {
        print "(no changes)\n";
    }
}