<commit_before><?php

namespace PHLAK\Twine\Traits;

use PHLAK\Twine\Config;

trait Encodable
{
    /**
     * Encode the string to or decode from a base64 encoded value.
     *
     * @param string $mode A base64 mode flag
     *
     * Available base64 modes:
     *
     *   - Twine\Config\Base64::ENCODE - Encode the string to base64
     *   - Twin\Config\Base64::DECODE - Decode the string from base64
     *
     * @throws \PHLAK\Twine\Exceptions\InvalidConfigOptionException
     *
     * @return self
     */
    public function base64(string $mode = Config\Base64::ENCODE) : self
    {
        Config\Base64::validateOption($mode);

        return new static($mode($this->string));
    }

    /**
     * Encode the string to a URL safe string.
     *
     * @return self
     */
    public function urlencode() : self
    {
        return new static(urlencode($this->string));
    }

    /**
     * Encode and decode the string to and from hex.
     *
     * @param int $mode A hex mode flag
     *
     * Available hex modes:
     *
     *   - Twine\Config\Hex::ENCODE - Encode the string to hex
     *   - Twine\Config\Hex::DECODE - Decode the string from hex
     *
     * @return self
     */
    public function hex(int $mode = Config\Hex::ENCODE) : self
    {
        Config\Hex::validateOption($mode);

        switch ($mode) {
            case Config\Hex::ENCODE:
                $characters = array_map(function ($char) {
                    return '\x' . dechex(ord($char));
                }, str_split($this->string));

                $string = implode($characters);
                break;
<commit_msg>Optimize hex encoding by using array_reduce.<commit_after><?php

namespace PHLAK\Twine\Traits;

use PHLAK\Twine\Config;

trait Encodable
{
    /**
     * Encode the string to or decode from a base64 encoded value.
     *
     * @param string $mode A base64 mode flag
     *
     * Available base64 modes:
     *
     *   - Twine\Config\Base64::ENCODE - Encode the string to base64
     *   - Twin\Config\Base64::DECODE - Decode the string from base64
     *
     * @throws \PHLAK\Twine\Exceptions\InvalidConfigOptionException
     *
     * @return self
     */
    public function base64(string $mode = Config\Base64::ENCODE) : self
    {
        Config\Base64::validateOption($mode);

        return new static($mode($this->string));
    }

    /**
     * Encode the string to a URL safe string.
     *
     * @return self
     */
    public function urlencode() : self
    {
        return new static(urlencode($this->string));
    }

    /**
     * Encode and decode the string to and from hex.
     *
     * @param int $mode A hex mode flag
     *
     * Available hex modes:
     *
     *   - Twine\Config\Hex::ENCODE - Encode the string to hex
     *   - Twine\Config\Hex::DECODE - Decode the string from hex
     *
     * @return self
     */
    public function hex(int $mode = Config\Hex::ENCODE) : self
    {
        Config\Hex::validateOption($mode);

        switch ($mode) {
            case Config\Hex::ENCODE:
                $string = array_reduce(str_split($this->string), function ($str, $char) {
                    $str .= '\x' . dechex(ord($char));

                    return $str;
                }, '');
                break;

{'fraction_non_alphanumeric': 0.11691086691086691, 'fraction_numerical': 0.01221001221001221, 'mean_word_length': 1.8897707231040564, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '889227', 'n_tokens_mistral': 1023, 'n_tokens_neox': 981, 'n_words': 256}