<commit_before><?php defined('BASEPATH') or exit('No direct script access allowed');

/**
 * @author <NAME> <<EMAIL>>, 2015-2016
 * @license The MIT License, http://opensource.org/licenses/MIT
 */

class Parser_Lex_Extension_Helper extends Parser_Lex_Extension {

    public static $_counter_increment = true;
    protected $parser_allowed_functions;

    public function __construct() {

        parent::__construct();

        $this->parser_allowed_functions =
            isset($this->extension_config['allowed_functions'])
                ? $this->extension_config['allowed_functions']
                : array();

    }

    public function __call($name, $args) {

        if (function_exists($name) && $this->_is_function_allowed($name)) {

            $attributes = $this->get_attributes();

            return call_user_func_array($name, $attributes);
        }

        return $this->_function_not_found($name);
    }

    protected function _is_function_allowed($name, & $message = null) {

        if (in_array($name, $this->parser_allowed_functions)) {
            return true;
        }

        $message = $this->_function_not_found($name);

        return false;
    }

    protected function _function_not_found($name) {

        return 'The function '.$name.'() has not been found or it is not allowed.';
    }

    protected function _set_type(& $value, $type) {

        // "boolean" (or, since PHP 4.2.0, "bool")
        // "integer" (or, since PHP 4.2.0, "int")
        // "float" (only possible since PHP 4.2.0, for older versions use the deprecated variant "double")
        // "string"
        // "array"
        // "object"
        // "null" (since PHP 4.2.0)
        $type = isset($type) ? (trim(strtolower(@ (string) $type))) : null;

        if ($type === null) {
            return true;
        }

        switch ($type) {

            case 'bool':

                $type = 'boolean';
                break;

            case 'int':

                $type = 'integer';
                break;

            case 'double':

                $type = 'float';
                break;
        }

        return @ settype($value, $type);
    }

    protected function _set_display(& $value, $mode) {

        // "print_d"
        // "print_r"
        // "var_dump"
        // "var_export"
        $mode = isset($mode) ? (strtolower(@ (string) $mode)) : null;

        switch ($mode) {

            case 'print_d':

                $value = print_d($value, true);
                break;

            case 'print_r':

                $value = print_r($value, true);
                break;

            case 'var_dump':

                ob_start();
                var_dump($value);
                $result = ob_get_clean();
                $value = $result;
                break;

            case 'var_export':

                $value = var_export($value, true);
                break;
        }
    }

    protected function _type($type) {

        $attributes = $this->get_attribute_values();

        $value = isset($attributes[0]) ? $attributes[0] : null;
        $display = isset($attributes[1]) ? $attributes[1] : null;

        $this->_set_type($value, $type);
        $this->_set_display($value, $display);

        return $value;
    }

    protected function _display($display) {

        $attributes = $this->get_attribute_values();

        $value = isset($attributes[0]) ? $attributes[0] : null;

        $this->_set_display($value, $display);

        return $value;
    }

    protected function _utf8($function) {

        if (!$this->_is_function_allowed($function, $message)) {
            return $message;
        }

        $attributes = $this->get_attributes();

        return IS_UTF8_CHARSET
            ? call_user_func_array(array('UTF8', $function), $attributes)
            : call_user_func_array($function, $attributes);
    }

    //--------------------------------------------------------------------------

    public function _func_array() {

        return $this->_type('array');
    }

    public function bool() {

        return $this->_type('boolean');
    }

    public function boolean() {

        return $this->_type('boolean');
    }

    public function config() {

        $item = $this->get_attribute('item');

        return config_item($item);
    }

    public function counter() {

        static $count = array();

        $key = $this->get_attribute('identifier', 'default');

        if (!isset($count[$key])) {
            $count[$key] = $this->get_attribute('start', 1);
        } elseif (self::$_counter_increment) {
            ($this->get_attribute('mode') == 'subtract') ? $count[$key]-- : $count[$key]++;
        }

        self::$_counter_increment = true;

        return (str_to_bool($this->get_attribute('return', true))) ? $count[$key] : null;
    }

    public function show_counter() {

        self::$_counter_increment = false;<commit_msg>helper:character_limiter - automatic loading.<commit_after><?php defined('BASEPATH') or exit('No direct script access allowed');

/**
 * @author <NAME> <<EMAIL>>, 2015-2016
 * @license The MIT License, http://opensource.org/licenses/MIT
 */

class Parser_Lex_Extension_Helper extends Parser_Lex_Extension {

    public static $_counter_increment = true;
    protected $parser_allowed_functions;

    public function __construct() {

        parent::__construct();

        $this->parser_allowed_functions =
            isset($this->extension_config['allowed_functions'])
                ? $this->extension_config['allowed_functions']
                : array();

    }

    public function __call($name, $args) {

        if (function_exists($name) && $this->_is_function_allowed($name)) {

            $attributes = $this->get_attributes();

            return call_user_func_array($name, $attributes);
        }

        return $this->_function_not_found($name);
    }

    protected function _is_function_allowed($name, & $message = null) {

        if (in_array($name, $this->parser_allowed_functions)) {
            return true;
        }

        $message = $this->_function_not_found($name);

        return false;
    }

    protected function _function_not_found($name) {

        return 'The function '.$name.'() has not been found or it is not allowed.';
    }

    protected function _set_type(& $value, $type) {

        // "boolean" (or, since PHP 4.2.0, "bool")
        // "integer" (or, since PHP 4.2.0, "int")
        // "float" (only possible since PHP 4.2.0, for older versions use the deprecated variant "double")
        // "string"
        // "array"
        // "object"
        // "null" (since PHP 4.2.0)
        $type = isset($type) ? (trim(strtolower(@ (string) $type))) : null;

        if ($type === null) {
            return true;
        }

        switch ($type) {

            case 'bool':

                $type = 'boolean';
                break;

            case 'int':

                $type = 'integer';
                break;

            case 'double':

                $type = 'float';
                break;
        }

        return @ settype($value, $type);
    }

    protected function _set_display(& $value, $mode) {

        // "print_d"
        // "print_r"
        // "var_dump"
        // "var_export"
        $mode = isset($mode) ? (strtolower(@ (string) $mode)) : null;

        switch ($mode) {

            case 'print_d':

                $value = print_d($value, true);
                break;

            case 'print_r':

                $value = print_r($value, true);
                break;

            case 'var_dump':

                ob_start();
                var_dump($value);
                $result = ob_get_clean();
                $value = $result;
                break;

            case 'var_export':

                $value = var_export($value, true);
                break;
        }
    }

    protected function _type($type) {

        $attributes = $this->get_attribute_values();

        $value = isset($attributes[0]) ? $attributes[0] : null;
        $display = isset($attributes[1]) ? $attributes[1] : null;

        $this->_set_type($value, $type);
        $this->_set_display($value, $display);

        return $value;
    }

    protected function _display($display) {

        $attributes = $this->get_attribute_values();

        $value = isset($attributes[0]) ? $attributes[0] : null;

        $this->_set_display($value, $display);

        return $value;
    }

    protected function _utf8($function) {

        if (!$this->_is_function_allowed($function, $message)) {
            return $message;
        }

        $attributes = $this->get_attributes();

        return IS_UTF8_CHARSET
            ? call_user_func_array(array('UTF8', $function), $attributes)
            : call_user_func_array($function, $attributes);
    }

    //--------------------------------------------------------------------------

    public function _func_array() {

        return $this->_type('array');
    }

    public function bool() {

        return $this->_type('boolean');
    }

    public function boolean() {

        return $this->_type('boolean');
    }

    public function character_limiter() {

        if (!$this->_is_function_allowed($name = __FUNCTION__, $message)) {
            return $message;
        }

        $this->load->helper('text');

        return call_user_func_array($name, $this->get_attributes());
    }

    public function config() {

        $item = $this->get_attribute('item');

        return config_item($item);
    }

    public function counter() {

        static $count = array();

        $key = $this->get_attribute('identifier', 'default');

        if (!isset($count[$key])) {
            $count[$key] = $this->get_attribute('start', 1);
        } elseif (self::$_counter_increment) {
            ($this->get_attribute('mode') == 'subtract') ? $count[$key]-- : $count[$key]++;
        }

        self::$_counter_increment = true;

        return (str_to_bool($this->get_attribute('return', true))) ? $count[$key] : null;
    }

    public function show_counter() {

        self::$_counter_increment = false;
{'fraction_non_alphanumeric': 0.15798799094399055, 'fraction_numerical': 0.005906093119401516, 'mean_word_length': 1.8912919749573136, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 59, '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': '5004936', 'n_tokens_mistral': 2985, 'n_tokens_neox': 2786, 'n_words': 604}