<commit_before>package aQute.libg.sed;

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class Sed {
    final File                 file;
    final Replacer             macro;
    File                       output;
    boolean backup = true;

    final Map<Pattern, String> replacements = new LinkedHashMap<Pattern, String>();

    public Sed(Replacer macro, File file) {
        assert file.isFile();
        this.file = file;
        this.macro = macro;
    }
    
    public Sed(File file) {
        assert file.isFile();
        this.file = file;
        this.macro = null;
    }

    public void setOutput(File f) {
        output = f;
    }

    public void replace(String pattern, String replacement) {
        replacements.put(Pattern.compile(pattern), replacement);
    }

    public int doIt() throws IOException {
    	int actions = 0;
        BufferedReader brdr = new BufferedReader(new FileReader(file));
        File out;
        if (output != null)
            out = output;
        else
            out = new File(file.getAbsolutePath() + ".tmp");
        File bak = new File(file.getAbsolutePath() + ".bak");
        PrintWriter pw = new PrintWriter(new FileWriter(out));
        try {
            String line;
            while ((line = brdr.readLine()) != null) {
                for (Pattern p : replacements.keySet()) {
                    String replace = replacements.get(p);
                    Matcher m = p.matcher(line);

                    StringBuffer sb = new StringBuffer();
                    while (m.find()) {
                        String tmp = setReferences(m, replace);
                        if ( macro != null)
                        	tmp = Matcher.quoteReplacement(macro.process(tmp));
                        m.appendReplacement(sb, tmp);
                        actions++;
                    }
                    m.appendTail(sb);

                    line = sb.toString();
                }
                pw.println(line);
            }
            pw.close();
            if (output == null) {
                file.renameTo(bak);
                out.renameTo(file);
            }
        } finally {
            brdr.close();
            pw.close();
        }
        return actions;
    }

    private String setReferences(Matcher m, String replace) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < replace.length(); i++) {
            char c = replace.charAt(i);
            if (c == '$' && i < replace.length() - 1
                    && Character.isDigit(replace.charAt(i + 1))) {
                int n = replace.charAt(i + 1) - '0';
                if ( n <= m.groupCount() )
                    sb.append(m.group(n));
                i++;
            } else
                sb.append(c);
        }
        return sb.toString();
    }

    public void setBackup(boolean b) {
    	this.backup=b;
    }
}
<commit_msg>Made it possible to not create a backup<commit_after>package aQute.libg.sed;

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class Sed {
    final File                 file;
    final Replacer             macro;
    File                       output;
    boolean backup = true;

    final Map<Pattern, String> replacements = new LinkedHashMap<Pattern, String>();

    public Sed(Replacer macro, File file) {
        assert file.isFile();
        this.file = file;
        this.macro = macro;
    }
    
    public Sed(File file) {
        assert file.isFile();
        this.file = file;
        this.macro = null;
    }

    public void setOutput(File f) {
        output = f;
    }

    public void replace(String pattern, String replacement) {
        replacements.put(Pattern.compile(pattern), replacement);
    }

    public int doIt() throws IOException {
    	int actions = 0;
        BufferedReader brdr = new BufferedReader(new FileReader(file));
        File out;
        if (output != null)
            out = output;
        else
            out = new File(file.getAbsolutePath() + ".tmp");
        PrintWriter pw = new PrintWriter(new FileWriter(out));
        try {
            String line;
            while ((line = brdr.readLine()) != null) {
                for (Pattern p : replacements.keySet()) {
                    String replace = replacements.get(p);
                    Matcher m = p.matcher(line);

                    StringBuffer sb = new StringBuffer();
                    while (m.find()) {
                        String tmp = setReferences(m, replace);
                        if ( macro != null)
                        	tmp = Matcher.quoteReplacement(macro.process(tmp));
                        m.appendReplacement(sb, tmp);
                        actions++;
                    }
                    m.appendTail(sb);

                    line = sb.toString();
                }
                pw.println(line);
            }
            pw.close();
            if (output == null) {
            	if ( backup ) {
                    File bak = new File(file.getAbsolutePath() + ".bak");
            		file.renameTo(bak);
            	}
                out.renameTo(file);
            }
        } finally {
            brdr.close();
            pw.close();
        }
        return actions;
    }

    private String setReferences(Matcher m, String replace) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < replace.length(); i++) {
            char c = replace.charAt(i);
            if (c == '$' && i < replace.length() - 1
                    && Character.isDigit(replace.charAt(i + 1))) {
                int n = replace.charAt(i + 1) - '0';
                if ( n <= m.groupCount() )
                    sb.append(m.group(n));
                i++;
            } else
                sb.append(c);
        }
        return sb.toString();
    }

    public void setBackup(boolean b) {
    	this.backup=b;
    }
}

{'fraction_non_alphanumeric': 0.1136210384356035, 'fraction_numerical': 0.0020229265003371545, 'mean_word_length': 1.3248432601880877, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, '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': '510003', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1543, 'n_words': 397}