Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 823496044d9fe8ba694b63ce4cfdab37617ccaca (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
/*******************************************************************************
 * Copyright (c) 2000, 2020 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.core;

import org.eclipse.core.text.StringMatcher;

/**
 * A string pattern matcher. Supports '*' and '?' wildcards.
 */
public class WildcardStringMatcher {

	private final StringMatcher fMatcher;

	private final boolean fPathPattern;

	/**
	 * Constructs a wildcard matcher for a pattern that may contain '*' for 0 and
	 * many characters and '?' for exactly one character. Character matching is
	 * case-insensitive.
	 *
	 * Literal '*' and '?' characters must be escaped in the pattern e.g., "\*"
	 * means literal "*", etc.
	 *
	 * The escape character '\' is an escape only if followed by '*', '?', or '\'.
	 * All other occurrences are taken literally.
	 *
	 * If invoking the StringMatcher with string literals in Java, don't forget
	 * escape characters are represented by "\\".
	 *
	 * @param pattern the pattern to match text against
	 * @throws IllegalArgumentException if {@code pattern == null}
	 */
	public WildcardStringMatcher(String pattern) {
		fMatcher = new StringMatcher(pattern, true, false);
		fPathPattern = pattern.indexOf('/') != -1;
	}

	/**
	 * Determines whether the patterns contains a forward slash.
	 *
	 * @return {@code true} if the pattern contains a '/', {@code false} otherwise
	 */
	public boolean isPathPattern() {
		return fPathPattern;
	}

	/**
	 * Determines whether the given {@code text} matches the pattern.
	 *
	 * @param text String to match; must not be {@code null}
	 * @return {@code true} if the whole {@code text} matches the pattern;
	 *         {@code false} otherwise
	 * @throws IllegalArgumentException if {@code text == null}
	 */
	public boolean match(String text) {
		return fMatcher.match(text);
	}

}

Back to the top