<commit_before>package net.sf.jabref.imports;

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

/**
 * This class provides the reformatting needed when reading BibTeX fields formatted
 * in JabRef style. The reformatting must undo all formatting done by JabRef when
 * writing the same fields.
 */
public class FieldContentParser {

    private static Pattern wrap = Pattern.compile("\\n\\t");

    /**
     * Performs the reformatting
     * @param content StringBuffer containing the field to format.
     * @return The formatted field content. NOTE: the StringBuffer returned may
     * or may not be the same as the argument given.
     */
    public StringBuffer format(StringBuffer content) {

        int prev = -1;
        int i=0;
        while (i<content.length()) {

            int c = content.charAt(i);
            if (c == '\n') {

                if ((content.length()>i+2) && (content.charAt(i+1)=='\t')
                    && !Character.isWhitespace(content.charAt(i+2))) {
                    // We have \n\t followed by non-whitespace, which indicates
                    // a wrap made by JabRef. Remove and insert space if necessary.

                    content.deleteCharAt(i); // \n
                    content.deleteCharAt(i); // \t
                    // Add space only if necessary:
                    if ((i>0) && !Character.isWhitespace(content.charAt(i-1))) {
                        content.insert(i-1, ' ');
                        // Increment i because of the inserted character:
                        i++;
                    }
                }
                if ((content.length()>i+3) && (content.charAt(i+1)=='\t')
                    && (content.charAt(i+2)==' ')
                    && !Character.isWhitespace(content.charAt(i+3))) {
                    // We have \n\t followed by ' ' followed by non-whitespace, which indicates
                    // a wrap made by JabRef <= 1.7.1. Remove:

                    content.deleteCharAt(i); // \n
                    content.deleteCharAt(i); // \t
                    // Remove space only if necessary:
                    if ((i>0) && Character.isWhitespace(content.charAt(i-1))) {
                        content.deleteCharAt(i);
                    }
                }
                else if ((content.length()>i+3) && (content.charAt(i+1)=='\t')
                        && (content.charAt(i+2)=='\n') && (content.charAt(i+3)=='\t')) {
                    // We have \n\t\n\t, which looks like a JabRef-formatted empty line.
                    // Remove the tabs and keep the line breaks:
                    content.deleteCharAt(i+1);
                    content.deleteCharAt(i+2); // i+3 is now i+2 since we removed one char.
                    // Skip past the line breaks:
                    i += 2;
                }
                else
                    i++;
                //content.deleteCharAt(i);
            }<commit_msg>Fixed misplaced space problem with FieldContentParser.<commit_after>package net.sf.jabref.imports;

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

/**
 * This class provides the reformatting needed when reading BibTeX fields formatted
 * in JabRef style. The reformatting must undo all formatting done by JabRef when
 * writing the same fields.
 */
public class FieldContentParser {

    private static Pattern wrap = Pattern.compile("\\n\\t");

    /**
     * Performs the reformatting
     * @param content StringBuffer containing the field to format.
     * @return The formatted field content. NOTE: the StringBuffer returned may
     * or may not be the same as the argument given.
     */
    public StringBuffer format(StringBuffer content) {

        int prev = -1;
        int i=0;
        while (i<content.length()) {

            int c = content.charAt(i);
            if (c == '\n') {

                if ((content.length()>i+2) && (content.charAt(i+1)=='\t')
                    && !Character.isWhitespace(content.charAt(i+2))) {
                    // We have \n\t followed by non-whitespace, which indicates
                    // a wrap made by JabRef. Remove and insert space if necessary.

                    content.deleteCharAt(i); // \n
                    content.deleteCharAt(i); // \t
                    // Add space only if necessary:
                    if ((i>0) && !Character.isWhitespace(content.charAt(i-1))) {
                        content.insert(i, ' ');
                        // Increment i because of the inserted character:
                        i++;
                    }
                }
                if ((content.length()>i+3) && (content.charAt(i+1)=='\t')
                    && (content.charAt(i+2)==' ')
                    && !Character.isWhitespace(content.charAt(i+3))) {
                    // We have \n\t followed by ' ' followed by non-whitespace, which indicates
                    // a wrap made by JabRef <= 1.7.1. Remove:

                    content.deleteCharAt(i); // \n
                    content.deleteCharAt(i); // \t
                    // Remove space only if necessary:
                    if ((i>0) && Character.isWhitespace(content.charAt(i-1))) {
                        content.deleteCharAt(i);
                    }
                }
                else if ((content.length()>i+3) && (content.charAt(i+1)=='\t')
                        && (content.charAt(i+2)=='\n') && (content.charAt(i+3)=='\t')) {
                    // We have \n\t\n\t, which looks like a JabRef-formatted empty line.
                    // Remove the tabs and keep the line breaks:
                    content.deleteCharAt(i+1);
                    content.deleteCharAt(i+2); // i+3 is now i+2 since we removed one char.
                    // Skip past the line breaks:
                    i += 2;
                }
                else
                    i++;
                //content.deleteCharAt(i);
            }
{'fraction_non_alphanumeric': 0.12245584524810765, 'fraction_numerical': 0.008578637510513036, 'mean_word_length': 1.4151096669374492, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '3104274', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1560, 'n_words': 433}