1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.util;
22
23/**
24 * This StringMatcher tests whether strings start with a specified variable
25 * string and then match another given StringMatcher.
26 *
27 * @author Eric Lafortune
28 */
29public class VariableStringMatcher implements StringMatcher
30{
31    private final char[]        allowedCharacters;
32    private final char[]        disallowedCharacters;
33    private final int           minimumLength;
34    private final int           maximumLength;
35    private final StringMatcher nextMatcher;
36
37
38    public VariableStringMatcher(char[]        allowedCharacters,
39                                 char[]        disallowedCharacters,
40                                 int           minimumLength,
41                                 int           maximumLength,
42                                 StringMatcher nextMatcher)
43    {
44        this.allowedCharacters    = allowedCharacters;
45        this.disallowedCharacters = disallowedCharacters;
46        this.minimumLength        = minimumLength;
47        this.maximumLength        = maximumLength;
48        this.nextMatcher          = nextMatcher;
49    }
50
51    // Implementations for StringMatcher.
52
53    public boolean matches(String string)
54    {
55        if (string.length() < minimumLength)
56        {
57            return false;
58        }
59
60        // Check the first minimum number of characters.
61        for (int index = 0; index < minimumLength; index++)
62        {
63            if (!isAllowedCharacter(string.charAt(index)))
64            {
65                return false;
66            }
67        }
68
69        int maximumLength = Math.min(this.maximumLength, string.length());
70
71        // Check the remaining characters, up to the maximum number.
72        for (int index = minimumLength; index < maximumLength; index++)
73        {
74            if (nextMatcher.matches(string.substring(index)))
75            {
76                return true;
77            }
78
79            if (!isAllowedCharacter(string.charAt(index)))
80            {
81                return false;
82            }
83        }
84
85        // Check the remaining characters in the string.
86        return nextMatcher.matches(string.substring(maximumLength));
87    }
88
89
90    // Small utility methods.
91
92    /**
93     * Returns whether the given character is allowed in the variable string.
94     */
95    private boolean isAllowedCharacter(char character)
96    {
97        // Check the allowed characters.
98        if (allowedCharacters != null)
99        {
100            for (int index = 0; index < allowedCharacters.length; index++)
101            {
102                if (allowedCharacters[index] == character)
103                {
104                    return true;
105                }
106            }
107
108            return false;
109        }
110
111        // Check the disallowed characters.
112        if (disallowedCharacters != null)
113        {
114            for (int index = 0; index < disallowedCharacters.length; index++)
115            {
116                if (disallowedCharacters[index] == character)
117                {
118                    return false;
119                }
120            }
121        }
122
123        // Any remaining character is allowed.
124        return true;
125    }
126}
127