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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/* *******************************************************************
* Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*
* Contributors:
* PARC initial implementation
* ******************************************************************/
package org.aspectj.weaver.patterns;
import java.io.IOException;
import org.aspectj.weaver.CompressingDataOutputStream;
import org.aspectj.weaver.VersionedDataInputStream;
public class NamePattern extends PatternNode {
char[] pattern;
int starCount = 0;
private int hashcode = -1;
public static final NamePattern ELLIPSIS = new NamePattern("");
public static final NamePattern ANY = new NamePattern("*");
public NamePattern(String name) {
this(name.toCharArray());
}
public NamePattern(char[] pattern) {
this.pattern = pattern;
for (char c : pattern) {
if (c == '*') {
starCount++;
}
}
hashcode = new String(pattern).hashCode();
}
public boolean matches(char[] a2) {
char[] a1 = pattern;
int len1 = a1.length;
int len2 = a2.length;
if (starCount == 0) {
if (len1 != len2) {
return false;
}
for (int i = 0; i < len1; i++) {
if (a1[i] != a2[i]) {
return false;
}
}
return true;
} else if (starCount == 1) {
// just '*' matches anything
if (len1 == 1) {
return true;
}
if (len1 > len2 + 1) {
return false;
}
int i2 = 0;
for (int i1 = 0; i1 < len1; i1++) {
char c1 = a1[i1];
if (c1 == '*') {
i2 = len2 - (len1 - (i1 + 1));
} else if (c1 != a2[i2++]) {
return false;
}
}
return true;
} else {
// System.err.print("match(\"" + pattern + "\", \"" + target + "\") -> ");
boolean b = outOfStar(a1, a2, 0, 0, len1 - starCount, len2, starCount);
// System.err.println(b);
return b;
}
}
private static boolean outOfStar(final char[] pattern, final char[] target, int pi, int ti, int pLeft, int tLeft,
final int starsLeft) {
if (pLeft > tLeft) {
return false;
}
while (true) {
// invariant: if (tLeft > 0) then (ti < target.length && pi < pattern.length)
if (tLeft == 0) {
return true;
}
if (pLeft == 0) {
return (starsLeft > 0);
}
if (pattern[pi] == '*') {
return inStar(pattern, target, pi + 1, ti, pLeft, tLeft, starsLeft - 1);
}
if (target[ti] != pattern[pi]) {
return false;
}
pi++;
ti++;
pLeft--;
tLeft--;
}
}
private static boolean inStar(final char[] pattern, final char[] target, int pi, int ti, final int pLeft, int tLeft,
int starsLeft) {
// invariant: pLeft > 0, so we know we'll run out of stars and find a real char in pattern
char patternChar = pattern[pi];
while (patternChar == '*') {
starsLeft--;
patternChar = pattern[++pi];
}
while (true) {
// invariant: if (tLeft > 0) then (ti < target.length)
if (pLeft > tLeft) {
return false;
}
if (target[ti] == patternChar) {
if (outOfStar(pattern, target, pi + 1, ti + 1, pLeft - 1, tLeft - 1, starsLeft)) {
return true;
}
}
ti++;
tLeft--;
}
}
public boolean matches(String other) {
if (starCount == 1 && pattern.length == 1) {
// optimize for wildcard
return true;
}
return matches(other.toCharArray());
}
@Override
public String toString() {
return new String(pattern);
}
@Override
public boolean equals(Object other) {
if (other instanceof NamePattern) {
NamePattern otherPat = (NamePattern) other;
if (otherPat.starCount != this.starCount) {
return false;
}
if (otherPat.pattern.length != this.pattern.length) {
return false;
}
for (int i = 0; i < this.pattern.length; i++) {
if (this.pattern[i] != otherPat.pattern[i]) {
return false;
}
}
return true;
}
return false;
}
@Override
public int hashCode() {
return hashcode;
}
@Override
public void write(CompressingDataOutputStream out) throws IOException {
out.writeUTF(new String(pattern));
}
public static NamePattern read(VersionedDataInputStream in) throws IOException {
String s = in.readUTF();
if (s.length() == 0) {
return ELLIPSIS;
}
return new NamePattern(s);
}
/**
* Method maybeGetSimpleName.
*
* @return String
*/
public String maybeGetSimpleName() {
if (starCount == 0 && pattern.length > 0) {
return new String(pattern);
}
return null;
}
/**
* Method isAny.
*
* @return boolean
*/
public boolean isAny() {
return starCount == 1 && pattern.length == 1;
}
@Override
public Object accept(PatternNodeVisitor visitor, Object data) {
return visitor.visit(this, data);
}
}
|