Function itoa

Summary

#include <src/c4/charconv.hpp>

(1) template <class T>
    size_t itoa(substr buf, T v)

(2) template <class T>
    size_t itoa(substr buf, T v, T radix)

(3) template <class T>
    size_t itoa(substr buf, T v, T radix, size_t num_digits)

Function overload

Synopsis

#include <src/c4/charconv.hpp>

template <class T>
size_t itoa(substr buf, T v)

Description

convert an integral signed decimal to a string. The resulting string is NOT zero-terminated. Writing stops at the buffer's end.

Returns
the number of characters needed for the result, even if the buffer size is insufficient

Mentioned in

Source

Lines 595-615 in src/c4/charconv.hpp.

template<class T>
size_t itoa(substr buf, T v)
{
    C4_STATIC_ASSERT(std::is_signed<T>::value);
    if(v >= 0)
    {
        return write_dec(buf, v);
    }
    else if(C4_LIKELY(v != std::numeric_limits<T>::min()))
    {
        if(buf.len > 0)
        {
            buf.str[0] = '-';
            return size_t(1) + write_dec(buf.sub(1), -v);
        }
        return size_t(1) + write_dec({}, -v);
    }
    // when T is the min value (eg i8: -128), negating it
    // will overflow
    return detail::itoa_neg(buf, v);
}

Synopsis

#include <src/c4/charconv.hpp>

template <class T>
size_t itoa(substr buf, T v, T radix)

Description

convert an integral signed integer to a string, using a specific radix. The radix must be 2, 8, 10 or 16.

The resulting string is NOT zero-terminated. Writing stops at the buffer's end.

Returns
the number of characters needed for the result, even if the buffer size is insufficient

Mentioned in

Source

Lines 623-649 in src/c4/charconv.hpp.

template<class T>
size_t itoa(substr buf, T v, T radix)
{
    C4_STATIC_ASSERT(std::is_signed<T>::value);
    C4_ASSERT(radix == 2 || radix == 8 || radix == 10 || radix == 16);
    // when T is the min value (eg i8: -128), negating it
    // will overflow
    if(C4_LIKELY(v != std::numeric_limits<T>::min()))
    {
        size_t pos = 0;
        if(v < 0)
        {
            v = -v;
            _c4append('-');
        }
        switch(radix)
        {
        case 10:                                 return pos + write_dec(pos < buf.len ? buf.sub(pos) : substr(), v);
        case 16: _c4append('0'); _c4append('x'); return pos + write_hex(pos < buf.len ? buf.sub(pos) : substr(), v);
        case 2 : _c4append('0'); _c4append('b'); return pos + write_bin(pos < buf.len ? buf.sub(pos) : substr(), v);
        case 8 : _c4append('0'); _c4append('o'); return pos + write_oct(pos < buf.len ? buf.sub(pos) : substr(), v);
        }
    }
    // when T is the min value (eg i8: -128), negating it
    // will overflow
    return detail::itoa_neg<T>(buf, v, radix);
}

Synopsis

#include <src/c4/charconv.hpp>

template <class T>
size_t itoa(substr buf, T v, T radix, size_t num_digits)

Description

same as c4::itoa(), but pad with zeroes on the left such that the resulting string is num_digits wide. The radix must be 2, 8, 10 or 16. The resulting string is NOT zero-terminated. Writing stops at the buffer's end.

Returns
the number of characters needed for the result, even if the buffer size is insufficient

Mentioned in

Source

Lines 659-683 in src/c4/charconv.hpp.

template<class T>
size_t itoa(substr buf, T v, T radix, size_t num_digits)
{
    C4_STATIC_ASSERT(std::is_signed<T>::value);
    C4_ASSERT(radix == 2 || radix == 8 || radix == 10 || radix == 16);
    // when T is the min value (eg i8: -128), negating it
    // will overflow
    if(C4_LIKELY(v != std::numeric_limits<T>::min()))
    {
        size_t pos = 0;
        if(v < 0)
        {
            v = -v;
            _c4append('-');
        }
        switch(radix)
        {
        case 10:                                 return pos + write_dec(pos < buf.len ? buf.sub(pos) : substr(), v, num_digits);
        case 16: _c4append('0'); _c4append('x'); return pos + write_hex(pos < buf.len ? buf.sub(pos) : substr(), v, num_digits);
        case 2 : _c4append('0'); _c4append('b'); return pos + write_bin(pos < buf.len ? buf.sub(pos) : substr(), v, num_digits);
        case 8 : _c4append('0'); _c4append('o'); return pos + write_oct(pos < buf.len ? buf.sub(pos) : substr(), v, num_digits);
        }
    }
    return detail::itoa_neg(buf, v, radix, num_digits);
}





Add Discussion as Guest

Log in