<commit_before>package org.spoofax.interpreter.library.ssl;

import java.util.HashSet;
import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.spoofax.interpreter.core.IContext;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.library.AbstractPrimitive;
import org.spoofax.interpreter.stratego.Strategy;
import org.spoofax.interpreter.terms.IStrategoAppl;
import org.spoofax.interpreter.terms.IStrategoString;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.ITermFactory;

public class SSL_newname extends AbstractPrimitive {
    private static final WeakHashMap<IContext, WeakHashMap<String, AtomicInteger>> countersPerContext =
        new WeakHashMap<>();

    private final HashSet<String> prefixes = new HashSet<>();


    SSL_newname() {
        super("SSL_newname", 0, 1);
    }


    @Override public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
        final IStrategoTerm prefixTerm = tvars[0];
        final String prefix;
        if(prefixTerm instanceof IStrategoString) {
            final IStrategoString str = (IStrategoString) prefixTerm;
            prefix = str.stringValue();
        } else if(prefixTerm instanceof IStrategoAppl) {
            final IStrategoAppl appl = (IStrategoAppl) prefixTerm;
            prefix = appl.getConstructor().getName();
        } else {
            final SSLLibrary library = (SSLLibrary) env.getOperatorRegistry(SSLLibrary.REGISTRY_NAME);
            return library.get("SSL_new").call(env, svars, tvars);
        }

        final ITermFactory factory = env.getFactory();

        synchronized(countersPerContext) {
            WeakHashMap<String, AtomicInteger> counters;
            counters = countersPerContext.get(env);
            if(counters == null) {
                counters = new WeakHashMap<>();
                countersPerContext.put(env, counters);
            }

            AtomicInteger counter;
            counter = counters.get(prefix);
            prefixes.add(prefix);
            if(counter == null) {
                counter = new AtomicInteger();
                counters.put(prefix, counter);
            }

            String result;
            IStrategoTerm resultTerm;
            do {
                int counterValue = getNextValue(counter);
                result = prefix + counterValue;
            } while((resultTerm = factory.tryMakeUniqueString(result)) == null);

            env.setCurrent(resultTerm);
            return true;
        }
    }

    private int getNextValue(AtomicInteger counter) {<commit_msg>Track origins through newname call<commit_after>package org.spoofax.interpreter.library.ssl;

import java.util.HashSet;
import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.spoofax.interpreter.core.IContext;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.library.AbstractPrimitive;
import org.spoofax.interpreter.stratego.Strategy;
import org.spoofax.interpreter.terms.IStrategoAppl;
import org.spoofax.interpreter.terms.IStrategoString;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.interpreter.terms.ITermFactory;

public class SSL_newname extends AbstractPrimitive {
    private static final WeakHashMap<IContext, WeakHashMap<String, AtomicInteger>> countersPerContext =
        new WeakHashMap<>();

    private final HashSet<String> prefixes = new HashSet<>();


    SSL_newname() {
        super("SSL_newname", 0, 1);
    }


    @Override public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
        final IStrategoTerm prefixTerm = tvars[0];
        final String prefix;
        if(prefixTerm instanceof IStrategoString) {
            final IStrategoString str = (IStrategoString) prefixTerm;
            prefix = str.stringValue();
        } else if(prefixTerm instanceof IStrategoAppl) {
            final IStrategoAppl appl = (IStrategoAppl) prefixTerm;
            prefix = appl.getConstructor().getName();
        } else {
            final SSLLibrary library = (SSLLibrary) env.getOperatorRegistry(SSLLibrary.REGISTRY_NAME);
            return library.get("SSL_new").call(env, svars, tvars);
        }

        final ITermFactory factory = env.getFactory();

        synchronized(countersPerContext) {
            WeakHashMap<String, AtomicInteger> counters;
            counters = countersPerContext.get(env);
            if(counters == null) {
                counters = new WeakHashMap<>();
                countersPerContext.put(env, counters);
            }

            AtomicInteger counter;
            counter = counters.get(prefix);
            prefixes.add(prefix);
            if(counter == null) {
                counter = new AtomicInteger();
                counters.put(prefix, counter);
            }

            String result;
            IStrategoTerm resultTerm;
            do {
                int counterValue = getNextValue(counter);
                result = prefix + counterValue;
            } while((resultTerm = factory.tryMakeUniqueString(result)) == null);

            env.setCurrent(factory.replaceTerm(resultTerm, prefixTerm));
            return true;
        }
    }

    private int getNextValue(AtomicInteger counter) {
{'fraction_non_alphanumeric': 0.09211986681465038, 'fraction_numerical': 0.0011098779134295228, 'mean_word_length': 2.721266345492085, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '7155702', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1495, 'n_words': 299}