<commit_before>#ifndef _C4_YML_TO_STR_HPP_
#define _C4_YML_TO_STR_HPP_

#include <stdio.h>
#include <inttypes.h>
#include <type_traits>
#include <utility>

#include "./subs.hpp"

namespace c4 {
namespace yml {

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

// Helper macro: appends chars to the buffer, without overflow. Always counts.
#define _c4append(c) { if(pos < buf.len) { buf.str[pos++] = (c); } else { ++pos; } }

template< class T >
size_t itoa(subs buf, T v)
{
    static_assert(std::is_integral<T>::value, "must be integral type");
    size_t pos = 0;
    if(v < 0)
    {
        _c4append('-');
        v = -v;
        do {
            _c4append((char)(v % 10) + '0');
            v /= 10;
        } while(v);
        buf.reverse_range(1, pos <= buf.len ? pos : buf.len);
    }
    else<commit_msg>fix bug in atoi which tried to reverse empty buffers<commit_after>#ifndef _C4_YML_TO_STR_HPP_
#define _C4_YML_TO_STR_HPP_

#include <stdio.h>
#include <inttypes.h>
#include <type_traits>
#include <utility>

#include "./subs.hpp"

namespace c4 {
namespace yml {

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

// Helper macro: appends chars to the buffer, without overflow. Always counts.
#define _c4append(c) { if(pos < buf.len) { buf.str[pos++] = (c); } else { ++pos; } }

template< class T >
size_t itoa(subs buf, T v)
{
    static_assert(std::is_integral<T>::value, "must be integral type");
    size_t pos = 0;
    if(v < 0)
    {
        _c4append('-');
        v = -v;
        do {
            _c4append((char)(v % 10) + '0');
            v /= 10;
        } while(v);
        if(buf.len)
        {
            buf.reverse_range(1, pos <= buf.len ? pos : buf.len);
        }
    }
    else
{'fraction_non_alphanumeric': 0.34543717152412806, 'fraction_numerical': 0.013377926421404682, 'mean_word_length': 1.2371794871794872, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, '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': '2603782', 'n_tokens_mistral': 659, 'n_tokens_neox': 634, 'n_words': 151}