<commit_before>/*******************************************************************************
 * Copyright (c) 2014 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * Contributor:  <NAME> - initial API and implementation
 *******************************************************************************/
package de.loskutov.gitignore;

import static de.loskutov.gitignore.Strings.*;

public class FastIgnoreRule {
	private static final NoResultMatcher NO_MATCH = new NoResultMatcher();
	private final IgnoreMatcher matcher;
	private final boolean inverse;
	private final boolean isDirectory;

	public FastIgnoreRule(String pattern) {
		if(pattern == null){
			throw new IllegalArgumentException("Pattern must be not null!");
		}
		pattern = pattern.trim();
		checkEmptyRule(pattern);
		inverse = pattern.charAt(0) == '!';
		if(inverse){
			pattern = pattern.substring(1);
			checkEmptyRule(pattern);
		}
		if(pattern.charAt(0) == '#'){
			this.matcher = NO_MATCH;
			isDirectory = false;
		} else {
			isDirectory = pattern.charAt(pattern.length() - 1) == '/';
			if(isDirectory) {
				pattern = stripSlashes(pattern);
				checkEmptyRule(pattern);
			}
			this.matcher = createMatcher(pattern, isDirectory);
		}
	}



	private static void checkEmptyRule(String pattern) {
		if(pattern.isEmpty()){
			throw new IllegalArgumentException("Pattern must be not empty!");
		}
	}

	private static IgnoreMatcher createMatcher(String pattern, boolean dirOnly) {
		pattern = pattern.trim();
		// ignore possible leading and trailing slash
		int slash = pattern.indexOf('/', 1);
		if(slash > 0 && slash < pattern.length() - 1){
			return new PathMatcher(pattern, dirOnly);
		}
		if(isWildCard(pattern)){
			return new WildCardMatcher(pattern, dirOnly);
		}
		return new NameMatcher(pattern, dirOnly);
	}

	public boolean isMatch(String path, boolean directory) {
		if(path == null){
			return false;<commit_msg>Allow empty patterns<commit_after>/*******************************************************************************
 * Copyright (c) 2014 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * Contributor:  <NAME> - initial API and implementation
 *******************************************************************************/
package de.loskutov.gitignore;

import static de.loskutov.gitignore.Strings.*;

public class FastIgnoreRule {
	private static final NoResultMatcher NO_MATCH = new NoResultMatcher();
	private final IgnoreMatcher matcher;
	private final boolean inverse;
	private final boolean isDirectory;

	public FastIgnoreRule(String pattern) {
		if(pattern == null){
			throw new IllegalArgumentException("Pattern must be not null!");
		}
		pattern = pattern.trim();
		if(pattern.isEmpty()){
			isDirectory = false;
			inverse = false;
			this.matcher = NO_MATCH;
			return;
		}
		inverse = pattern.charAt(0) == '!';
		if(inverse){
			pattern = pattern.substring(1);
			if(pattern.isEmpty()){
				isDirectory = false;
				this.matcher = NO_MATCH;
				return;
			}
		}
		if(pattern.charAt(0) == '#'){
			this.matcher = NO_MATCH;
			isDirectory = false;
		} else {
			isDirectory = pattern.charAt(pattern.length() - 1) == '/';
			if(isDirectory) {
				pattern = stripSlashes(pattern);
				if(pattern.isEmpty()){
					this.matcher = NO_MATCH;
					return;
				}
			}
			this.matcher = createMatcher(pattern, isDirectory);
		}
	}

	private static IgnoreMatcher createMatcher(String pattern, boolean dirOnly) {
		pattern = pattern.trim();
		// ignore possible leading and trailing slash
		int slash = pattern.indexOf('/', 1);
		if(slash > 0 && slash < pattern.length() - 1){
			return new PathMatcher(pattern, dirOnly);
		}
		if(isWildCard(pattern)){
			return new WildCardMatcher(pattern, dirOnly);
		}
		return new NameMatcher(pattern, dirOnly);
	}

	public boolean isMatch(String path, boolean directory) {
		if(path == null){
			return false;
{'fraction_non_alphanumeric': 0.18175422138836772, 'fraction_numerical': 0.007035647279549718, 'mean_word_length': 4.43312101910828, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2977152', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1169, 'n_words': 333}