<commit_before>package org.exist.xquery;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.dom.persistent.NodeSet;
import org.exist.xquery.value.AbstractSequence;
import org.exist.xquery.value.IntegerValue;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.MemoryNodeSet;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceIterator;
import org.exist.xquery.value.Type;

public class RangeSequence extends AbstractSequence {

    private final static Logger LOG = LogManager.getLogger(AbstractSequence.class);

    private final IntegerValue start;
    private final IntegerValue end;

    public RangeSequence(final IntegerValue start, final IntegerValue end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public void add(final Item item) throws XPathException {
        throw new XPathException("Internal error: adding to an immutable sequence");
    }

    @Override
    public void addAll(final Sequence other) throws XPathException {
        throw new XPathException("Internal error: adding to an immutable sequence");
    }

    public int getItemType() {
        return Type.INTEGER;
    }

    @Override
    public SequenceIterator iterate() {
        return new RangeSequenceIterator(start.getLong(), end.getLong());
    }

    @Override
    public SequenceIterator unorderedIterator() {
        return new RangeSequenceIterator(start.getLong(), end.getLong());
    }

    public SequenceIterator iterateInReverse() {
        return new ReverseRangeSequenceIterator(start.getLong(), end.getLong());
    }

    private static class RangeSequenceIterator implements SequenceIterator {
        private long current;
        private final long end;

        private RangeSequenceIterator(final long start, final long end) {
            this.current = start;
            this.end = end;
        }

        @Override
        public Item nextItem() {
            if (current <= end) {
                return new IntegerValue(current++);
            } else {
                return null;
            }
        }

        @Override
        public boolean hasNext() {
            return current <= end;
        }
    }

    private static class ReverseRangeSequenceIterator implements SequenceIterator {
        private final long start;
        private long current;

        private ReverseRangeSequenceIterator(final long start, final long end) {
            this.start = start;
            this.current = end;
        }

        @Override
        public Item nextItem() {
            if (current >= start) {
                return new IntegerValue(current--);
            } else {
                return null;
            }
        }

        @Override
        public boolean hasNext() {
            return current >= start;
        }
    }

    @Override
    public int getItemCount() {
        if (start.compareTo(end) > 0) {
            return 0;
        }
        try {
            return ((IntegerValue) end.minus(start)).getInt() + 1;
        } catch (final XPathException e) {
            LOG.warn("Unexpected exception when processing result of range expression: " + e.getMessage(), e);
            return 0;
        }
    }

    @Override
    public boolean isEmpty() {
        return getItemCount() == 0;
    }

    @Override
    public boolean hasOne() {
        return getItemCount() == 1;
    }

    @Override
    public boolean hasMany() {
        return getItemCount() > 1;
    }

    @Override
    public Item itemAt(final int pos) {
        if (pos <= getItemCount()) {
            return new IntegerValue(start.getLong() + pos);
        }
        return null;
    }

    @Override
    public NodeSet toNodeSet() throws XPathException {
        throw new XPathException("Type error: the sequence cannot be converted into" +
                " a node set. Item type is xs:integer");
    }

    @Override
    public MemoryNodeSet toMemNodeSet() throws XPathException {
        throw new XPathException("Type error: the sequence cannot be converted into" +
                " a node set. Item type is xs:integer");
    }

    @Override
    public void removeDuplicates() {
    }
}<commit_msg>[bugfix] Don't realize sequence when generating a string description of RangeSequence<commit_after>package org.exist.xquery;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.dom.persistent.NodeSet;
import org.exist.xquery.value.AbstractSequence;
import org.exist.xquery.value.IntegerValue;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.MemoryNodeSet;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceIterator;
import org.exist.xquery.value.Type;

public class RangeSequence extends AbstractSequence {

    private final static Logger LOG = LogManager.getLogger(AbstractSequence.class);

    private final IntegerValue start;
    private final IntegerValue end;

    public RangeSequence(final IntegerValue start, final IntegerValue end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public void add(final Item item) throws XPathException {
        throw new XPathException("Internal error: adding to an immutable sequence");
    }

    @Override
    public void addAll(final Sequence other) throws XPathException {
        throw new XPathException("Internal error: adding to an immutable sequence");
    }

    public int getItemType() {
        return Type.INTEGER;
    }

    @Override
    public SequenceIterator iterate() {
        return new RangeSequenceIterator(start.getLong(), end.getLong());
    }

    @Override
    public SequenceIterator unorderedIterator() {
        return new RangeSequenceIterator(start.getLong(), end.getLong());
    }

    public SequenceIterator iterateInReverse() {
        return new ReverseRangeSequenceIterator(start.getLong(), end.getLong());
    }

    private static class RangeSequenceIterator implements SequenceIterator {
        private long current;
        private final long end;

        private RangeSequenceIterator(final long start, final long end) {
            this.current = start;
            this.end = end;
        }

        @Override
        public Item nextItem() {
            if (current <= end) {
                return new IntegerValue(current++);
            } else {
                return null;
            }
        }

        @Override
        public boolean hasNext() {
            return current <= end;
        }
    }

    private static class ReverseRangeSequenceIterator implements SequenceIterator {
        private final long start;
        private long current;

        private ReverseRangeSequenceIterator(final long start, final long end) {
            this.start = start;
            this.current = end;
        }

        @Override
        public Item nextItem() {
            if (current >= start) {
                return new IntegerValue(current--);
            } else {
                return null;
            }
        }

        @Override
        public boolean hasNext() {
            return current >= start;
        }
    }

    @Override
    public int getItemCount() {
        if (start.compareTo(end) > 0) {
            return 0;
        }
        try {
            return ((IntegerValue) end.minus(start)).getInt() + 1;
        } catch (final XPathException e) {
            LOG.warn("Unexpected exception when processing result of range expression: " + e.getMessage(), e);
            return 0;
        }
    }

    @Override
    public boolean isEmpty() {
        return getItemCount() == 0;
    }

    @Override
    public boolean hasOne() {
        return getItemCount() == 1;
    }

    @Override
    public boolean hasMany() {
        return getItemCount() > 1;
    }

    @Override
    public Item itemAt(final int pos) {
        if (pos <= getItemCount()) {
            return new IntegerValue(start.getLong() + pos);
        }
        return null;
    }

    @Override
    public NodeSet toNodeSet() throws XPathException {
        throw new XPathException("Type error: the sequence cannot be converted into" +
                " a node set. Item type is xs:integer");
    }

    @Override
    public MemoryNodeSet toMemNodeSet() throws XPathException {
        throw new XPathException("Type error: the sequence cannot be converted into" +
                " a node set. Item type is xs:integer");
    }

    @Override
    public void removeDuplicates() {
    }

	/**
	 * Generates a string representation of the Range Sequence.
	 *
	 * Range sequences can potentially be
	 * very large, so we generate a summary here
	 * rather than evaluating to generate a (possibly)
	 * huge sequence of objects.
	 *
	 * @return a string representation of the range sequence.
	 */
	@Override
	public String toString() {
		return "Range(" + start + " to " + end + ")";
	}
}
{'fraction_non_alphanumeric': 0.08628021486123545, 'fraction_numerical': 0.0020143240823634737, 'mean_word_length': 2.243920145190563, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '7473313', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2250, 'n_words': 699}