Function utoa

Summary

#include <src/c4/charconv.hpp>

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

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

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

Function overload

Synopsis

#include <src/c4/charconv.hpp>

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

Description

convert an integral unsigned 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 694-699 in src/c4/charconv.hpp.

template<class T>
size_t utoa(substr buf, T v)
{
    C4_STATIC_ASSERT(std::is_unsigned<T>::value);
    return write_dec(buf, v);
}

Synopsis

#include <src/c4/charconv.hpp>

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

Description

convert an integral unsigned 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 705-720 in src/c4/charconv.hpp.

template<class T>
size_t utoa(substr buf, T v, T radix)
{
    C4_STATIC_ASSERT(std::is_unsigned<T>::value);
    C4_ASSERT(radix == 10 || radix == 16 || radix == 2 || radix == 8);
    size_t pos = 0;
    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);
    }
    C4_UNREACHABLE();
    return substr::npos;
}

Synopsis

#include <src/c4/charconv.hpp>

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

Description

same as c4::utoa(), 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 729-744 in src/c4/charconv.hpp.

template<class T>
size_t utoa(substr buf, T v, T radix, size_t num_digits)
{
    C4_STATIC_ASSERT(std::is_unsigned<T>::value);
    C4_ASSERT(radix == 10 || radix == 16 || radix == 2 || radix == 8);
    size_t pos = 0;
    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);
    }
    C4_UNREACHABLE();
    return substr::npos;
}





Add Discussion as Guest

Log in