Skip to content

ST::uint_formatter

Michael Hansen edited this page Nov 22, 2019 · 4 revisions

ST::uint_formatter<uint_T>

Since string_theory 3.0.

Headers

#include <string_theory/format_numeric>

Template Parameters

Type Description
uint_T Numeric type for formatting. This must be an unsigned integral type.

Public Functions

Name Summary
(constructor) ST::uint_formatter constructor
format Format an unsigned integer value
text Retrieve a pointer to the formatted text
size Retrieve the size of the formatted text

Details

ST::uint_formatter is a helper class for fast conversion of unsigned integer values to 7-bit ASCII text. It is used internally by ST::string and ST::format to provide the textual representation of integers.

Note that ST::uint_formatter does not support signed integers. In order to format a signed integer, one should format the absolute value of the input and then apply a sign (if necessary) to the front of the output. This is the method used by other string_theory formatters.

// Example to format a signed integer of type `int_type` in base 10.
template <typename int_type>
ST::char_buffer format_int(int_type value)
{
    ST::uint_formatter<std::make_unsigned<int_type>>::type> formatter;
    formatter.format(std::abs(value), 10);

    ST::char_buffer result;
    if (value < 0) {
        result.allocate(formatter.size() + 1);
        result[0] = '-';
        memcpy(result.data() + 1, formatter.text(), formatter.size());
    } else {
        result.allocate(formatter.size());
        memcpy(result.data(), formatter.text(), formatter.size());
    }
    return result;
}

Member Documentation

ST::uint_formatter constructor

Signature
uint_formatter() noexcept

Constructs an empty uint_formatter.

Since string_theory 3.0.


ST::uint_formatter::format

Signature
void format(uint_T value, int radix, bool upper_case = false) noexcept

Format the specified unsigned integer value into this formatter object's internal buffer.

This only performs basic digit formatting, and does not insert any special prefixes (such as 0x for hex). The radix must not be zero. For radix values greater than 10, the upper_case parameter can be specified to use upper case character digits instead of lower-case ones.

NOTE: Values for radix less than 1 or greater than 36 are not supported and will produce undefined behavior.

Since string_theory 3.0.


ST::uint_formatter::size

Signature
size_t size() const noexcept

For uint_formatter objects which contain a formatted value, this returns the number of characters in the formatted text.

Calling size() on an empty uint_formatter (i.e. before format() has been called) is undefined behavior.

Since string_theory 3.0.

See also text()


ST::uint_formatter::text

Signature
const char *text() const noexcept

For uint_formatter objects which contain a formatted value, this returns a pointer to the beginning of the formatted text.

Calling text() on an empty uint_formatter will return NULL.

Since string_theory 3.0.

See also size()

Clone this wiki locally