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  /**
13   * Matcher for simple regex patterns starting with an asterisk, e.g. "*.tmp"
14   */
15  public class LeadingAsteriskMatcher extends NameMatcher {
16  
17  	LeadingAsteriskMatcher(String pattern, Character pathSeparator, boolean dirOnly) {
18  		super(pattern, pathSeparator, dirOnly, true);
19  
20  		if (subPattern.charAt(0) != '*')
21  			throw new IllegalArgumentException(
22  					"Pattern must have leading asterisk: " + pattern); //$NON-NLS-1$
23  	}
24  
25  	/** {@inheritDoc} */
26  	@Override
27  	public boolean matches(String segment, int startIncl, int endExcl) {
28  		// faster local access, same as in string.indexOf()
29  		String s = subPattern;
30  
31  		// we don't need to count '*' character itself
32  		int subLength = s.length() - 1;
33  		// simple /*/ pattern
34  		if (subLength == 0)
35  			return true;
36  
37  		if (subLength > (endExcl - startIncl))
38  			return false;
39  
40  		for (int i = subLength, j = endExcl - 1; i > 0; i--, j--) {
41  			char c1 = s.charAt(i);
42  			char c2 = segment.charAt(j);
43  			if (c1 != c2)
44  				return false;
45  		}
46  		return true;
47  	}
48  
49  }