<commit_before>package ru.tehkode.permissions;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExpMatcher implements PermissionMatcher {
	protected static Pattern rangeExpression = Pattern.compile("(\\d+)-(\\d+)");

	protected static HashMap<String, Pattern> patternCache = new HashMap<String, Pattern>();

	@Override
	public boolean isMatches(String expression, String permission) {
		Pattern permissionMatcher = patternCache.get(expression);

		if (permissionMatcher == null) {
			patternCache.put(expression, permissionMatcher = createPattern(expression));
		}

		return permissionMatcher.matcher(permission).matches();
	}

	protected Pattern createPattern(String expression) {
		return Pattern.compile(prepareRegexp(expression), Pattern.CASE_INSENSITIVE);
	}

	public static String prepareRegexp(String expression) {
		if (expression.startsWith("-")) {
			expression = expression.substring(1);
		}

		if (expression.startsWith("#")) {
			expression = expression.substring(1);
		}

		String regexp = expression.replace(".", "\\.").replace("*", "(.*)");

		try {
			Matcher rangeMatcher = rangeExpression.matcher(regexp);
			while (rangeMatcher.find()) {
				StringBuilder range = new StringBuilder();
				int from = Integer.parseInt(rangeMatcher.group(1));
				int to = Integer.parseInt(rangeMatcher.group(2));

				if (from > to) {
					int temp = from;
					from = to;
					to = temp;
				} // swap them

				range.append("(");

				for (int i = from; i <= to; i++) {
					range.append(i);
					if (i < to) {
						range.append("|");
					}
				}

				range.append(")");

				regexp = regexp.replace(rangeMatcher.group(0), range.toString());
			}
		} catch (Throwable e) {<commit_msg>Add $ prefix (comes after - and #) to make PEX interpret the following permission as a raw regex and not do any conversion<commit_after>package ru.tehkode.permissions;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExpMatcher implements PermissionMatcher {
	public static final String RAW_REGEX_CHAR = "$";
	protected static Pattern rangeExpression = Pattern.compile("(\\d+)-(\\d+)");

	protected static HashMap<String, Pattern> patternCache = new HashMap<String, Pattern>();

	@Override
	public boolean isMatches(String expression, String permission) {
		Pattern permissionMatcher = patternCache.get(expression);

		if (permissionMatcher == null) {
			patternCache.put(expression, permissionMatcher = createPattern(expression));
		}

		return permissionMatcher.matcher(permission).matches();
	}

	protected Pattern createPattern(String expression) {
		return Pattern.compile(prepareRegexp(expression), Pattern.CASE_INSENSITIVE);
	}

	public static String prepareRegexp(String expression) {
		if (expression.startsWith("-")) {
			expression = expression.substring(1);
		}

		if (expression.startsWith("#")) {
			expression = expression.substring(1);
		}

		boolean rawRegexp = expression.startsWith(RAW_REGEX_CHAR);
		if (rawRegexp) {
			expression = expression.substring(1);
		}

		String regexp = rawRegexp ? expression : expression.replace(".", "\\.").replace("*", "(.*)");

		try {
			Matcher rangeMatcher = rangeExpression.matcher(regexp);
			while (rangeMatcher.find()) {
				StringBuilder range = new StringBuilder();
				int from = Integer.parseInt(rangeMatcher.group(1));
				int to = Integer.parseInt(rangeMatcher.group(2));

				if (from > to) {
					int temp = from;
					from = to;
					to = temp;
				} // swap them

				range.append("(");

				for (int i = from; i <= to; i++) {
					range.append(i);
					if (i < to) {
						range.append("|");
					}
				}

				range.append(")");

				regexp = regexp.replace(rangeMatcher.group(0), range.toString());
			}
		} catch (Throwable e) {
{'fraction_non_alphanumeric': 0.13970588235294118, 'fraction_numerical': 0.002888655462184874, 'mean_word_length': 3.9986876640419946, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3830716', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1095, 'n_words': 266}