<commit_before>/*
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.functions;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.apache.oro.text.PatternCacheLRU;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.Util;

public class RegexFunction extends AbstractFunction implements Serializable {
	transient private static Logger log = LoggingManager.getLoggerForClass();

	public static final String ALL = "ALL";

	public static final String RAND = "RAND";

	public static final String KEY = "__regexFunction";

	private Object[] values;// Parameters are stored here

	private static Random rand = new Random();

	private static final List desc = new LinkedList();

	private static PatternCacheLRU patternCache = new PatternCacheLRU(1000, new Perl5Compiler());

	private Pattern templatePattern;// initialised to the regex \$(\d+)\$

	private static ThreadLocal localMatcher = new ThreadLocal() {
		protected Object initialValue() {
			return new Perl5Matcher();
		}
	};

	// Number of parameters expected - used to reject invalid calls
	private static final int MIN_PARAMETER_COUNT = 2;

	private static final int MAX_PARAMETER_COUNT = 6;
	static {
		desc.add(JMeterUtils.getResString("regexfunc_param_1"));// regex
		desc.add(JMeterUtils.getResString("regexfunc_param_2"));// template
		desc.add(JMeterUtils.getResString("regexfunc_param_3"));// which match
		desc.add(JMeterUtils.getResString("regexfunc_param_4"));// between text
		desc.add(JMeterUtils.getResString("regexfunc_param_5"));// default text
		desc.add(JMeterUtils.getResString("function_name_param"));
	}

	public RegexFunction() {
		templatePattern = patternCache.getPattern("\\$(\\d+)\\$", Perl5Compiler.READ_ONLY_MASK);
	}

	public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
			throws InvalidVariableException {
		String valueIndex = "", defaultValue = "", between = "";
		String name = "";
		Pattern searchPattern;
		Object[] tmplt;
		try {
			searchPattern = patternCache.getPattern(((CompoundVariable) values[0]).execute(),<commit_msg>PatternCacheLRU is not Serialisable, so make transient and create in readResolve()<commit_after>/*
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.functions;

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.apache.oro.text.PatternCacheLRU;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.Util;

public class RegexFunction extends AbstractFunction implements Serializable {
	transient private static Logger log = LoggingManager.getLoggerForClass();

	public static final String ALL = "ALL";

	public static final String RAND = "RAND";

	public static final String KEY = "__regexFunction";

	private Object[] values;// Parameters are stored here

	private static Random rand = new Random();

	private static final List desc = new LinkedList();

	private static PatternCacheLRU patternCache = new PatternCacheLRU(1000, new Perl5Compiler());

	private transient Pattern templatePattern;// initialised to the regex \$(\d+)\$

	private static ThreadLocal localMatcher = new ThreadLocal() {
		protected Object initialValue() {
			return new Perl5Matcher();
		}
	};

	// Number of parameters expected - used to reject invalid calls
	private static final int MIN_PARAMETER_COUNT = 2;

	private static final int MAX_PARAMETER_COUNT = 6;
	static {
		desc.add(JMeterUtils.getResString("regexfunc_param_1"));// regex
		desc.add(JMeterUtils.getResString("regexfunc_param_2"));// template
		desc.add(JMeterUtils.getResString("regexfunc_param_3"));// which match
		desc.add(JMeterUtils.getResString("regexfunc_param_4"));// between text
		desc.add(JMeterUtils.getResString("regexfunc_param_5"));// default text
		desc.add(JMeterUtils.getResString("function_name_param"));
	}

	public RegexFunction() {
		templatePattern = patternCache.getPattern("\\$(\\d+)\\$", Perl5Compiler.READ_ONLY_MASK);
	}

    // For serialised objects, do the same work as the constructor:
    private Object readResolve() throws ObjectStreamException {
        templatePattern = patternCache.getPattern("\\$(\\d+)\\$", Perl5Compiler.READ_ONLY_MASK);
        return this;
    }


	public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
			throws InvalidVariableException {
		String valueIndex = "", defaultValue = "", between = "";
		String name = "";
		Pattern searchPattern;
		Object[] tmplt;
		try {
			searchPattern = patternCache.getPattern(((CompoundVariable) values[0]).execute(),
{'fraction_non_alphanumeric': 0.10272362781695009, 'fraction_numerical': 0.008157057928936817, 'mean_word_length': 5.038397328881469, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6270422', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2025, 'n_words': 584}