View Javadoc
1   /*
2    * Copyright (C) 2014, Andrey Loskutov <loskutov@gmx.de> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.ignore.internal;
11  
12  import static org.eclipse.jgit.ignore.internal.Strings.convertGlob;
13  
14  import java.util.regex.Pattern;
15  
16  import org.eclipse.jgit.errors.InvalidPatternException;
17  
18  /**
19   * Matcher built from path segments containing wildcards. This matcher converts
20   * glob wildcards to Java {@link java.util.regex.Pattern}'s.
21   * <p>
22   * This class is immutable and thread safe.
23   */
24  public class WildCardMatcher extends NameMatcher {
25  
26  	final Pattern p;
27  
28  	WildCardMatcher(String pattern, Character pathSeparator, boolean dirOnly)
29  			throws InvalidPatternException {
30  		super(pattern, pathSeparator, dirOnly, false);
31  		p = convertGlob(subPattern);
32  	}
33  
34  	/** {@inheritDoc} */
35  	@Override
36  	public boolean matches(String segment, int startIncl, int endExcl) {
37  		return p.matcher(segment.substring(startIncl, endExcl)).matches();
38  	}
39  }