Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2b088c0775c943fd11f0d2acf1d7dc462f9fd7ba (plain) (blame)
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
209
210
211
212
213
214
215
216
217
218
/*******************************************************************************
 * Copyright (c) 2009, 2017 Cloudsmith Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.metadata.expression;

import java.io.Serializable;

/**
 * A simple compiled pattern. It supports two kinds of wildcards. The '*' (any character zero to many times)
 * and the '?' (any character exactly one time).
 * @since 2.0
 */
public class SimplePattern implements Serializable, Comparable<SimplePattern> {
	private static final long serialVersionUID = -2477990705739062410L;

	/**
	 * Matches the <code>value</code> with the compiled expression. The value
	 * is considered matching if all characters are matched by the expression. A
	 * partial match is not enough.
	 * @param value The value to match
	 * @return <code>true</code> if the value was a match.
	 */
	public boolean isMatch(CharSequence value) {
		if (node == null)
			node = parse(pattern, 0);
		return node.match(value, 0);
	}

	@Override
	public String toString() {
		return pattern;
	}

	@Override
	public int compareTo(SimplePattern o) {
		return pattern.compareTo(o.pattern);
	}

	@Override
	public boolean equals(Object o) {
		return o == this || (o instanceof SimplePattern && ((SimplePattern) o).pattern.equals(pattern));
	}

	@Override
	public int hashCode() {
		return 3 * pattern.hashCode();
	}

	private final String pattern;
	private transient Node node;

	private SimplePattern(String pattern) {
		this.pattern = pattern;
	}

	static class AllNode extends Node {
		@Override
		boolean match(CharSequence value, int pos) {
			return true;
		}
	}

	static class RubberBandNode extends Node {
		final Node next;

		RubberBandNode(Node next) {
			this.next = next;
		}

		@Override
		boolean match(CharSequence value, int pos) {
			int top = value.length();
			String ending = next.getEndingConstant();
			if (ending != null) {
				// value must end with this constant. It will be faster
				// to scan backwards from the end.
				int clen = ending.length();
				if (clen > top - pos)
					return false;
				while (clen > 0)
					if (ending.charAt(--clen) != value.charAt(--top))
						return false;
				return true;
			}

			while (pos < top) {
				if (next.match(value, pos++))
					return true;
			}
			return false;
		}
	}

	static class AnyCharacterNode extends Node {
		final Node next;

		AnyCharacterNode(Node next) {
			this.next = next;
		}

		@Override
		boolean match(CharSequence value, int pos) {
			int top = value.length();
			return next == null ? pos + 1 == top : next.match(value, pos + 1);
		}
	}

	static class EndConstantNode extends Node {
		final String constant;

		EndConstantNode(String constant) {
			this.constant = constant;
		}

		@Override
		boolean match(CharSequence value, int pos) {
			int max = constant.length() + pos;
			int top = value.length();
			if (top != max)
				return false;

			int idx = 0;
			while (pos < max)
				if (value.charAt(pos++) != constant.charAt(idx++))
					return false;
			return true;
		}

		@Override
		String getEndingConstant() {
			return constant;
		}
	}

	static class ConstantNode extends Node {
		final Node next;
		final String constant;

		ConstantNode(Node next, String constant) {
			this.next = next;
			this.constant = constant;
		}

		@Override
		boolean match(CharSequence value, int pos) {
			int max = constant.length() + pos;
			int top = value.length();
			if (top < max)
				return false;

			int idx = 0;
			while (pos < max)
				if (value.charAt(pos++) != constant.charAt(idx++))
					return false;
			return next == null ? (pos == top) : next.match(value, pos);
		}
	}

	static abstract class Node {
		abstract boolean match(CharSequence value, int pos);

		String getEndingConstant() {
			return null;
		}
	}

	public static SimplePattern compile(String pattern) {
		if (pattern == null)
			throw new IllegalArgumentException("Pattern can not be null"); //$NON-NLS-1$
		return new SimplePattern(pattern);
	}

	private static Node parse(String pattern, int pos) {
		int top = pattern.length();
		StringBuffer bld = null;
		Node parsedNode = null;
		while (pos < top) {
			char c = pattern.charAt(pos);
			switch (c) {
				case '*' :
					++pos;
					parsedNode = pos == top ? new AllNode() : new RubberBandNode(parse(pattern, pos));
					break;
				case '?' :
					parsedNode = new AnyCharacterNode(parse(pattern, pos + 1));
					break;
				case '\\' :
					if (++pos == top)
						throw new IllegalArgumentException("Pattern ends with escape"); //$NON-NLS-1$
					c = pattern.charAt(pos);
					// fall through
				default :
					if (bld == null)
						bld = new StringBuffer();
					bld.append(c);
					++pos;
					continue;
			}
			break;
		}

		if (bld != null) {
			String constant = bld.toString();
			parsedNode = parsedNode == null ? new EndConstantNode(constant) : new ConstantNode(parsedNode, constant);
		}
		return parsedNode;
	}
}

Back to the top