<commit_before>#pragma once
#ifndef TARIGO_MSGPACK_VALUES_HPP
#define TARIGO_MSGPACK_VALUES_HPP

#include <string>
#include <memory>

#include <msgpack/value_base.hpp>

namespace tarigo {
namespace msgpack {

// -----------------------------------------------------------------------------
// Nil.
// -----------------------------------------------------------------------------
class value_nil final : public value_base<type::NIL, nil_struct> {
public:
    explicit value_nil() : value_base({}) {}
    enum format format() const override { return format::NIL; }
};

// -----------------------------------------------------------------------------
// Boolean.
// -----------------------------------------------------------------------------
class value_boolean final : public value_base<type::BOOLEAN, bool> {
public:
    explicit value_boolean(bool value) : value_base(value) {}
    enum format format() const override {
        return value_ ? format::TRUE : format::FALSE;
    }
};

// -----------------------------------------------------------------------------
// String.
// -----------------------------------------------------------------------------
class value_string final : public value_base<type::RAW_STRING, std::string> {
public:
    explicit value_string(const std::string &value) : value_base(value) {}
    explicit value_string(std::string &&value) : value_base(std::move(value)) {}

    enum format format() const override {
        const size_t len = value_.size();
        if (len <= 0x1f) return format::FIXSTR;
        if (len <= 0xff) return format::STR8;
        if (len <= 0xffff) return format::STR16;
        return format::STR32;
    }

    const std::string &string_value() const override { return value_; }
};

// -----------------------------------------------------------------------------
// Generic numeric value.
// -----------------------------------------------------------------------------

template <type MsgPackType, typename NativeType>
class value_number: public value_base<MsgPackType, NativeType> {
protected:
    explicit value_number(const NativeType &value)
            : value_base<MsgPackType, NativeType>(value) {}
    explicit value_number(NativeType &&value)
            : value_base<MsgPackType, NativeType>(std::move(value)) {}

    //FIXME:
    bool equals(const value * other) const override {
        switch( other->type() )
        {
            case type::INTEGER:
            case type::FLOAT:
            {
                return double_value() == other->double_value();
            } break;
            default:
            {
                return value_base<MsgPackType,NativeType>::equals(other);
            } break;
        }
    }

    //FIXME:
    bool less(const value * other) const override {
        switch( other->type() )
        {
            case type::INTEGER:
            case type::FLOAT:
            {
                return double_value() < other->double_value();
            } break;
            default:
            {
                return value_base<MsgPackType,NativeType>::equals(other);
            } break;
        }
    }

    float float_value() const override {
        return static_cast<float>(value_base<MsgPackType,NativeType>::value_);
    }

    double double_value() const override {
        return static_cast<double>(value_base<MsgPackType,NativeType>::value_);
    }

    int8_t int8_value() const override {
        return static_cast<int8_t>(value_base<MsgPackType,NativeType>::value_);
    }

    int16_t int16_value() const override {
        return static_cast<int16_t>(value_base<MsgPackType,NativeType>::value_);
    }
    int32_t int32_value() const override {
        return static_cast<int32_t>(value_base<MsgPackType,NativeType>::value_);
    }

    int64_t int64_value() const override {
        return static_cast<int64_t>(value_base<MsgPackType,NativeType>::value_);
    }

    uint8_t uint8_value() const override {
        return static_cast<uint8_t>(value_base<MsgPackType,NativeType>::value_);
    }

    uint16_t uint16_value() const override {
        return static_cast<uint16_t>(value_base<MsgPackType,NativeType>::value_);
    }

    uint32_t uint32_value() const override {
        return static_cast<uint32_t>(value_base<MsgPackType,NativeType>::value_);
    }

    uint64_t uint64_value() const override {
        return static_cast<uint64_t>(value_base<MsgPackType,NativeType>::value_);
    }
};

template <typename T,
        class = typename std::enable_if<std::is_integral<T>::value>::type>
format integer_format(T i) {
    if (i >= 0 && i <= 127) return format::POSITIVE_FIXINT;

    if (std::is_signed<T>::value) {
        if (i >= -32 && i <= -1) return format::NEGATIVE_FIXINT;
    }

    if (std::is_same<T, int8_t>::value) return format::INT8;
    if (std::is_same<T, int16_t>::value) return format::INT16;
    if (std::is_same<T, int32_t>::value) return format::INT32;
    if (std::is_same<T, int64_t>::value) return format::INT64;

    if (std::is_same<T, uint8_t>::value) return format::UINT8;
    if (std::is_same<T, uint16_t>::value) return format::UINT16;
    if (std::is_same<T, uint32_t>::value) return format::UINT32;
    if (std::is_same<T, uint64_t>::value) return format::UINT64;
<commit_msg>Fix integer_format().<commit_after>#pragma once
#ifndef TARIGO_MSGPACK_VALUES_HPP
#define TARIGO_MSGPACK_VALUES_HPP

#include <string>
#include <memory>

#include <msgpack/value_base.hpp>

namespace tarigo {
namespace msgpack {

// -----------------------------------------------------------------------------
// Nil.
// -----------------------------------------------------------------------------
class value_nil final : public value_base<type::NIL, nil_struct> {
public:
    explicit value_nil() : value_base({}) {}
    enum format format() const override { return format::NIL; }
};

// -----------------------------------------------------------------------------
// Boolean.
// -----------------------------------------------------------------------------
class value_boolean final : public value_base<type::BOOLEAN, bool> {
public:
    explicit value_boolean(bool value) : value_base(value) {}
    enum format format() const override {
        return value_ ? format::TRUE : format::FALSE;
    }
};

// -----------------------------------------------------------------------------
// String.
// -----------------------------------------------------------------------------
class value_string final : public value_base<type::RAW_STRING, std::string> {
public:
    explicit value_string(const std::string &value) : value_base(value) {}
    explicit value_string(std::string &&value) : value_base(std::move(value)) {}

    enum format format() const override {
        const size_t len = value_.size();
        if (len <= 0x1f) return format::FIXSTR;
        if (len <= 0xff) return format::STR8;
        if (len <= 0xffff) return format::STR16;
        return format::STR32;
    }

    const std::string &string_value() const override { return value_; }
};

// -----------------------------------------------------------------------------
// Generic numeric value.
// -----------------------------------------------------------------------------

template <type MsgPackType, typename NativeType>
class value_number: public value_base<MsgPackType, NativeType> {
protected:
    explicit value_number(const NativeType &value)
            : value_base<MsgPackType, NativeType>(value) {}
    explicit value_number(NativeType &&value)
            : value_base<MsgPackType, NativeType>(std::move(value)) {}

    //FIXME:
    bool equals(const value * other) const override {
        switch( other->type() )
        {
            case type::INTEGER:
            case type::FLOAT:
            {
                return double_value() == other->double_value();
            } break;
            default:
            {
                return value_base<MsgPackType,NativeType>::equals(other);
            } break;
        }
    }

    //FIXME:
    bool less(const value * other) const override {
        switch( other->type() )
        {
            case type::INTEGER:
            case type::FLOAT:
            {
                return double_value() < other->double_value();
            } break;
            default:
            {
                return value_base<MsgPackType,NativeType>::equals(other);
            } break;
        }
    }

    float float_value() const override {
        return static_cast<float>(value_base<MsgPackType,NativeType>::value_);
    }

    double double_value() const override {
        return static_cast<double>(value_base<MsgPackType,NativeType>::value_);
    }

    int8_t int8_value() const override {
        return static_cast<int8_t>(value_base<MsgPackType,NativeType>::value_);
    }

    int16_t int16_value() const override {
        return static_cast<int16_t>(value_base<MsgPackType,NativeType>::value_);
    }
    int32_t int32_value() const override {
        return static_cast<int32_t>(value_base<MsgPackType,NativeType>::value_);
    }

    int64_t int64_value() const override {
        return static_cast<int64_t>(value_base<MsgPackType,NativeType>::value_);
    }

    uint8_t uint8_value() const override {
        return static_cast<uint8_t>(value_base<MsgPackType,NativeType>::value_);
    }

    uint16_t uint16_value() const override {
        return static_cast<uint16_t>(value_base<MsgPackType,NativeType>::value_);
    }

    uint32_t uint32_value() const override {
        return static_cast<uint32_t>(value_base<MsgPackType,NativeType>::value_);
    }

    uint64_t uint64_value() const override {
        return static_cast<uint64_t>(value_base<MsgPackType,NativeType>::value_);
    }
};

template <typename T,
        class = typename std::enable_if<std::is_integral<T>::value>::type>
format integer_format(T i) {
    if (i >= 0) {
        if (i < 128) {
            return format::POSITIVE_FIXINT;
        } else if (i <= std::numeric_limits<uint8_t>::max()) {
            return format::UINT8;
        } else if (i <= std::numeric_limits<uint16_t>::max()) {
            return format::UINT16;
        } else if (i <= std::numeric_limits<uint32_t>::max()) {
            return format::UINT32;
        } else if (i <= std::numeric_limits<uint64_t>::max()) {
            return format::UINT64;
        }
    } else {
        if (i >= -32) {
            return format::NEGATIVE_FIXINT;
        } else if (i >= std::numeric_limits<int8_t>::min() and
                   i <= std::numeric_limits<int8_t>::max()) {
            return format::INT8;
        } else if (i >= std::numeric_limits<int16_t>::min() and
                   i <= std::numeric_limits<int16_t>::max()) {
            return format::INT16;
        } else if (i >= std::numeric_limits<int32_t>::min() and
                   i <= std::numeric_limits<int32_t>::max()) {
            return format::INT32;
        } else if (i >= std::numeric_limits<int64_t>::min() and
                   i <= std::numeric_limits<int64_t>::max()) {
            return format::INT64;
        }
    }
{'fraction_non_alphanumeric': 0.22641680246242984, 'fraction_numerical': 0.016114430563099764, 'mean_word_length': 1.9325723387310858, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 110, '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': '4083680', 'n_tokens_mistral': 3148, 'n_tokens_neox': 3023, 'n_words': 613}