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 patterns ending with an asterisk, e.g. "Makefile.*"
14   */
15  public class TrailingAsteriskMatcher extends NameMatcher {
16  
17  	TrailingAsteriskMatcher(String pattern, Character pathSeparator, boolean dirOnly) {
18  		super(pattern, pathSeparator, dirOnly, true);
19  
20  		if (subPattern.charAt(subPattern.length() - 1) != '*')
21  			throw new IllegalArgumentException(
22  					"Pattern must have trailing 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  		// we don't need to count '*' character itself
31  		int subLenth = s.length() - 1;
32  		// simple /*/ pattern
33  		if (subLenth == 0)
34  			return true;
35  
36  		if (subLenth > (endExcl - startIncl))
37  			return false;
38  
39  		for (int i = 0; i < subLenth; i++) {
40  			char c1 = s.charAt(i);
41  			char c2 = segment.charAt(i + startIncl);
42  			if (c1 != c2)
43  				return false;
44  		}
45  		return true;
46  	}
47  
48  }