<commit_before>/*******************************************************************************
 * This file is part of Pebble.
 *
 * Copyright (c) 2014 by <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 ******************************************************************************/
package com.mitchellbosecke.pebble.extension.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mitchellbosecke.pebble.extension.Function;

/**
 * Range function to iterate over long or a string with a length of 1.
 *
 * @author <NAME>
 */
public class RangeFunction implements Function {
    public static final String FUNCTION_NAME = "range";
    private static final String PARAM_END = "end";
    private static final String PARAM_INCREMENT = "increment";
    private static final String PARAM_START = "start";
    private final List<String> argumentNames = new ArrayList<>();

    public RangeFunction() {
        this.argumentNames.add(PARAM_START);
        this.argumentNames.add(PARAM_END);
        this.argumentNames.add(PARAM_INCREMENT);
    }

    @Override
    public Object execute(Map<String, Object> args) {
        Object start = args.get(PARAM_START);
        Object end = args.get(PARAM_END);
        Long increment = (Long) args.get(PARAM_INCREMENT);
        if (increment == null) {
            increment = 1L;
        }

        List<Object> results = new ArrayList<>();
        // Iterating over Long
        if (start instanceof Long && end instanceof Long) {
            Long startNum = (Long) start;
            Long endNum = (Long) end;

            if (increment > 0) {
                for (Long i = startNum; i <= endNum; i += increment) {
                    results.add(i);
                }
            }
            else if (increment < 0) {
                for (Long i = startNum; i >= endNum; i += increment) {
                    results.add(i);
                }
            }
            else {
                throw new IllegalArgumentException("The increment of the range function must be different than 0");
            }
        }
        // Iterating over character
        else if (start instanceof String && end instanceof String) {
            String startStr = (String) start;
            String endStr = (String) end;
            if (startStr.length() != 1 || endStr.length() != 1) {
                throw new IllegalArgumentException("Arguments of range function must be of type Number or String with "
                        + "a length of 1");
            }

            char startChar = startStr.charAt(0);
            char endChar = endStr.charAt(0);

            Character.getNumericValue(startChar);
            if (increment > 0) {
                for (int i = startChar; i <= endChar; i += increment) {
                    results.add((char) i);
                }
            }
            else if (increment < 0) {
                for (int i = startChar; i >= endChar; i += increment) {
                    results.add((char) i);
                }
            }
            else {
                throw new IllegalArgumentException("The increment of the range function must be different than 0");
            }<commit_msg>Support to iterate over character + unit tests<commit_after>/*******************************************************************************
 * This file is part of Pebble.
 *
 * Copyright (c) 2014 by <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 ******************************************************************************/
package com.mitchellbosecke.pebble.extension.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mitchellbosecke.pebble.extension.Function;

/**
 * Range function to iterate over long or a string with a length of 1.
 *
 * @author <NAME>
 */
public class RangeFunction implements Function {
    public static final String FUNCTION_NAME = "range";
    private static final String PARAM_END = "end";
    private static final String PARAM_INCREMENT = "increment";
    private static final String PARAM_START = "start";
    private final List<String> argumentNames = new ArrayList<>();

    public RangeFunction() {
        this.argumentNames.add(PARAM_START);
        this.argumentNames.add(PARAM_END);
        this.argumentNames.add(PARAM_INCREMENT);
    }

    @Override
    public Object execute(Map<String, Object> args) {
        Object start = args.get(PARAM_START);
        Object end = args.get(PARAM_END);
        Long increment = (Long) args.get(PARAM_INCREMENT);
        if (increment == null) {
            increment = 1L;
        }

        List<Object> results = new ArrayList<>();
        // Iterating over Long
        if (start instanceof Long && end instanceof Long) {
            Long startNum = (Long) start;
            Long endNum = (Long) end;

            if (increment > 0) {
                for (Long i = startNum; i <= endNum; i += increment) {
                    results.add(i);
                }
            }
            else if (increment < 0) {
                for (Long i = startNum; i >= endNum; i += increment) {
                    results.add(i);
                }
            }
            else {
                throw new IllegalArgumentException("The increment of the range function must be different than 0");
            }
        }
        // Iterating over character
        else if (start instanceof String && end instanceof String) {
            String startStr = (String) start;
            String endStr = (String) end;
            if (startStr.length() != 1 || endStr.length() != 1) {
                throw new IllegalArgumentException("Arguments of range function must be of type Number or String with "
                        + "a length of 1");
            }

            char startChar = startStr.charAt(0);
            char endChar = endStr.charAt(0);

            if (increment > 0) {
                for (int i = startChar; i <= endChar; i += increment) {
                    results.add((char) i);
                }
            }
            else if (increment < 0) {
                for (int i = startChar; i >= endChar; i += increment) {
                    results.add((char) i);
                }
            }
            else {
                throw new IllegalArgumentException("The increment of the range function must be different than 0");
            }
{'fraction_non_alphanumeric': 0.13863947648759703, 'fraction_numerical': 0.005174250494597474, 'mean_word_length': 1.9737556561085974, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '681358', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1596, 'n_words': 543}