<commit_before>/*
 * Copyright <NAME> 2009-2011, All rights reserved.
 *
 */

package net.domesdaybook.matcher.sequence;

import net.domesdaybook.matcher.singlebyte.AnyMatcher;
import net.domesdaybook.matcher.singlebyte.SingleByteMatcher;
import net.domesdaybook.reader.Reader;

/**
 * An immutable object which matches a gap of unknown bytes.
 *
 * It always matches, even if the sequence being matched against is shorter
 * than the gap. This is true in general of all the sequence matchers, in that
 * they do not test to see if they overrun the Reader, or guarantee that an
 * IndexOutOfBounds exception will be thrown. In the case of the fixed gap matcher,
 * no access is made to the Reader at all, so no exception can ever be thrown.
 *
 * @author matt
 */
public final class FixedGapMatcher implements SequenceMatcher {

    private static final SingleByteMatcher ANY_MATCHER = new AnyMatcher();

    private final int gapLength;

   
    /**
     * Constructs a FixedGapMatcher of a given length.
     *
     * @param gapLength The length of the gap to match.
     * @throws IllegalArgumentException if the gap is less than one.
     */
    public FixedGapMatcher(final int gapLength) {
        if (gapLength < 1) {
            throw new IllegalArgumentException("FixedGapMatcher requires a gap greater than zero.");
        }
        this.gapLength = gapLength;
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public SingleByteMatcher getByteMatcherForPosition(final int position) {
        return ANY_MATCHER;
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public int length() {
        return gapLength;
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public String toRegularExpression(final boolean prettyPrint) {
        return prettyPrint ? String.format(" .{%d} ", gapLength) : String.format(".{%d}", gapLength);
    }


    /**
     * {@inheritDoc}
     * 
     */
    @Override
    public boolean matches(final Reader reader, final long matchPosition) {
        return matchPosition + gapLength < reader.length() && matchPosition >= 0;
    }
    

    /**
     * {@inheritDoc}
     * 
     */
    @Override<commit_msg>fixed gap matcher does not depend on knowing length of match (avoiding full stream read).<commit_after>/*
 * Copyright <NAME> 2009-2011, All rights reserved.
 *
 */

package net.domesdaybook.matcher.sequence;

import java.io.IOException;
import net.domesdaybook.matcher.singlebyte.AnyMatcher;
import net.domesdaybook.matcher.singlebyte.SingleByteMatcher;
import net.domesdaybook.reader.Reader;

/**
 * An immutable object which matches a gap of unknown bytes.
 *
 * It always matches, even if the sequence being matched against is shorter
 * than the gap. This is true in general of all the sequence matchers, in that
 * they do not test to see if they overrun the Reader, or guarantee that an
 * IndexOutOfBounds exception will be thrown. In the case of the fixed gap matcher,
 * no access is made to the Reader at all, so no exception can ever be thrown.
 *
 * @author matt
 */
public final class FixedGapMatcher implements SequenceMatcher {

    private static final SingleByteMatcher ANY_MATCHER = new AnyMatcher();

    private final int gapLength;

   
    /**
     * Constructs a FixedGapMatcher of a given length.
     *
     * @param gapLength The length of the gap to match.
     * @throws IllegalArgumentException if the gap is less than one.
     */
    public FixedGapMatcher(final int gapLength) {
        if (gapLength < 1) {
            throw new IllegalArgumentException("FixedGapMatcher requires a gap greater than zero.");
        }
        this.gapLength = gapLength;
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public SingleByteMatcher getByteMatcherForPosition(final int position) {
        return ANY_MATCHER;
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public int length() {
        return gapLength;
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public String toRegularExpression(final boolean prettyPrint) {
        return prettyPrint ? String.format(" .{%d} ", gapLength) : String.format(".{%d}", gapLength);
    }


    /**
     * {@inheritDoc}
     * 
     */
    @Override
    public boolean matches(final Reader reader, final long matchPosition) throws IOException {
        return reader.getWindow(matchPosition) != null && 
               reader.getWindow(matchPosition + gapLength) != null;
    }
    

    /**
     * {@inheritDoc}
     * 
     */
    @Override
{'fraction_non_alphanumeric': 0.08958517210944396, 'fraction_numerical': 0.004192409532215357, 'mean_word_length': 2.6883645240032545, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, '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': '4624858', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1215, 'n_words': 429}