aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/LeadingAsteriskMatcher.java
blob: 84376be53ee3a829dbd6583481d8bc029307e13b (plain)
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
/*
 * Copyright (C) 2014, Andrey Loskutov <loskutov@gmx.de> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.ignore.internal;

/**
 * Matcher for simple regex patterns starting with an asterisk, e.g. "*.tmp"
 */
public class LeadingAsteriskMatcher extends NameMatcher {

	LeadingAsteriskMatcher(String pattern, Character pathSeparator, boolean dirOnly) {
		super(pattern, pathSeparator, dirOnly, true);

		if (subPattern.charAt(0) != '*')
			throw new IllegalArgumentException(
					"Pattern must have leading asterisk: " + pattern); //$NON-NLS-1$
	}

	@Override
	public boolean matches(String segment, int startIncl, int endExcl) {
		// faster local access, same as in string.indexOf()
		String s = subPattern;

		// we don't need to count '*' character itself
		int subLength = s.length() - 1;
		// simple /*/ pattern
		if (subLength == 0)
			return true;

		if (subLength > (endExcl - startIncl))
			return false;

		for (int i = subLength, j = endExcl - 1; i > 0; i--, j--) {
			char c1 = s.charAt(i);
			char c2 = segment.charAt(j);
			if (c1 != c2)
				return false;
		}
		return true;
	}

}