<commit_before>// Copyright 2010-2013 RethinkDB, all rights reserved.
#include "rdb_protocol/op.hpp"

#include "rdb_protocol/func.hpp"
#include "rdb_protocol/minidriver.hpp"

#include "debug.hpp"

namespace ql {
argspec_t::argspec_t(int n) : min(n), max(n), eval_flags(NO_FLAGS) { }
argspec_t::argspec_t(int _min, int _max)
    : min(_min), max(_max), eval_flags(NO_FLAGS) { }
argspec_t::argspec_t(int _min, int _max, eval_flags_t _eval_flags)
    : min(_min), max(_max), eval_flags(_eval_flags) { }
std::string argspec_t::print() {
    if (min == max) {
        return strprintf("%d argument(s)", min);
    } else if (max == -1) {
        return strprintf("%d or more argument(s)", min);
    } else {
        return strprintf("between %d and %d arguments", min, max);
    }
}
bool argspec_t::contains(int n) const {
    return min <= n && (max < 0 || n <= max);
}

optargspec_t::optargspec_t(std::initializer_list<const char *> args) {
    init(args.size(), args.begin());
}


optargspec_t::optargspec_t(bool _is_make_object_val)
    : is_make_object_val(_is_make_object_val) { }

void optargspec_t::init(int num_args, const char *const *args) {
    is_make_object_val = false;
    for (int i = 0; i < num_args; ++i) {
        legal_args.insert(args[i]);
    }
}

optargspec_t optargspec_t::make_object() {
    return optargspec_t(true);
}
bool optargspec_t::is_make_object() const {
    return is_make_object_val;
}
bool optargspec_t::contains(const std::string &key) const {
    r_sanity_check(!is_make_object());
    return legal_args.count(key) != 0;
}

optargspec_t optargspec_t::with(std::initializer_list<const char *> args) const {
    optargspec_t ret(args);
    ret.legal_args.insert(legal_args.begin(), legal_args.end());
    return ret;
}

class faux_term_t : public term_t {
public:
    faux_term_t(protob_t<const Term> src, counted_t<const datum_t> _d)
        : term_t(std::move(src)), d(std::move(_d)) { }
    virtual const char *name() const { return "<EXPANDED FROM r.args>"; }
    virtual bool is_deterministic() const { return true; }
    virtual void accumulate_captures(var_captures_t *) const { }
private:
    virtual counted_t<val_t> term_eval(scope_env_t *, eval_flags_t) const {
        return new_val(d);
    }
    counted_t<const datum_t> d;
};


class args_t : public pb_rcheckable_t {
public:
    args_t(protob_t<const Term> _src,
           argspec_t _argspec,
           std::vector<counted_t<const term_t> > _original_args);
    // Must be called before `eval_arg`.
    void start_eval(scope_env_t *env, eval_flags_t flags,
                    counted_t<val_t> _arg0 = counted_t<val_t>());
    counted_t<val_t> eval_arg(scope_env_t *env, size_t i, eval_flags_t flags);
    size_t size() { return args.size(); }

    counted_t<grouped_data_t> maybe_grouped_data(
        scope_env_t *env, bool is_grouped_seq_op, eval_flags_t flags);
    const std::vector<counted_t<const term_t> > &get_original_args() {
        return original_args;
    }
private:
    counted_t<const term_t> get(size_t i);
    protob_t<const Term> src;
    argspec_t argspec;
    counted_t<val_t> arg0;
    std::vector<counted_t<const term_t> > original_args;
    std::vector<counted_t<const term_t> > args;
};

counted_t<grouped_data_t> args_t::maybe_grouped_data(
    scope_env_t *env, bool is_grouped_seq_op, eval_flags_t flags) {
    counted_t<grouped_data_t> gd;
    if (args.size() != 0) {
        if (!arg0.has()) {
            arg0 = get(0)->eval(env, flags);
        }
        gd = is_grouped_seq_op
            ? arg0->maybe_as_grouped_data()
            : arg0->maybe_as_promiscuous_grouped_data(env->env);<commit_msg>Marked a method const, whatever.<commit_after>// Copyright 2010-2013 RethinkDB, all rights reserved.
#include "rdb_protocol/op.hpp"

#include "rdb_protocol/func.hpp"
#include "rdb_protocol/minidriver.hpp"

#include "debug.hpp"

namespace ql {
argspec_t::argspec_t(int n) : min(n), max(n), eval_flags(NO_FLAGS) { }
argspec_t::argspec_t(int _min, int _max)
    : min(_min), max(_max), eval_flags(NO_FLAGS) { }
argspec_t::argspec_t(int _min, int _max, eval_flags_t _eval_flags)
    : min(_min), max(_max), eval_flags(_eval_flags) { }
std::string argspec_t::print() {
    if (min == max) {
        return strprintf("%d argument(s)", min);
    } else if (max == -1) {
        return strprintf("%d or more argument(s)", min);
    } else {
        return strprintf("between %d and %d arguments", min, max);
    }
}
bool argspec_t::contains(int n) const {
    return min <= n && (max < 0 || n <= max);
}

optargspec_t::optargspec_t(std::initializer_list<const char *> args) {
    init(args.size(), args.begin());
}


optargspec_t::optargspec_t(bool _is_make_object_val)
    : is_make_object_val(_is_make_object_val) { }

void optargspec_t::init(int num_args, const char *const *args) {
    is_make_object_val = false;
    for (int i = 0; i < num_args; ++i) {
        legal_args.insert(args[i]);
    }
}

optargspec_t optargspec_t::make_object() {
    return optargspec_t(true);
}
bool optargspec_t::is_make_object() const {
    return is_make_object_val;
}
bool optargspec_t::contains(const std::string &key) const {
    r_sanity_check(!is_make_object());
    return legal_args.count(key) != 0;
}

optargspec_t optargspec_t::with(std::initializer_list<const char *> args) const {
    optargspec_t ret(args);
    ret.legal_args.insert(legal_args.begin(), legal_args.end());
    return ret;
}

class faux_term_t : public term_t {
public:
    faux_term_t(protob_t<const Term> src, counted_t<const datum_t> _d)
        : term_t(std::move(src)), d(std::move(_d)) { }
    virtual const char *name() const { return "<EXPANDED FROM r.args>"; }
    virtual bool is_deterministic() const { return true; }
    virtual void accumulate_captures(var_captures_t *) const { }
private:
    virtual counted_t<val_t> term_eval(scope_env_t *, eval_flags_t) const {
        return new_val(d);
    }
    counted_t<const datum_t> d;
};


class args_t : public pb_rcheckable_t {
public:
    args_t(protob_t<const Term> _src,
           argspec_t _argspec,
           std::vector<counted_t<const term_t> > _original_args);
    // Must be called before `eval_arg`.
    void start_eval(scope_env_t *env, eval_flags_t flags,
                    counted_t<val_t> _arg0 = counted_t<val_t>());
    counted_t<val_t> eval_arg(scope_env_t *env, size_t i, eval_flags_t flags);
    size_t size() { return args.size(); }

    counted_t<grouped_data_t> maybe_grouped_data(
        scope_env_t *env, bool is_grouped_seq_op, eval_flags_t flags);
    const std::vector<counted_t<const term_t> > &get_original_args() const {
        return original_args;
    }
private:
    counted_t<const term_t> get(size_t i);
    protob_t<const Term> src;
    argspec_t argspec;
    counted_t<val_t> arg0;
    std::vector<counted_t<const term_t> > original_args;
    std::vector<counted_t<const term_t> > args;
};

counted_t<grouped_data_t> args_t::maybe_grouped_data(
    scope_env_t *env, bool is_grouped_seq_op, eval_flags_t flags) {
    counted_t<grouped_data_t> gd;
    if (args.size() != 0) {
        if (!arg0.has()) {
            arg0 = get(0)->eval(env, flags);
        }
        gd = is_grouped_seq_op
            ? arg0->maybe_as_grouped_data()
            : arg0->maybe_as_promiscuous_grouped_data(env->env);
{'fraction_non_alphanumeric': 0.1319796954314721, 'fraction_numerical': 0.005487721223761833, 'mean_word_length': 3.935680433310765, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, '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': '3912564', 'n_tokens_mistral': 2776, 'n_tokens_neox': 2749, 'n_words': 534}