MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::io::Formatter< char > Struct Reference

Formatter for characters Format specifier options for output: More...

#include <formats.hpp>

Inheritance diagram for mtcore::io::Formatter< char >:
Collaboration diagram for mtcore::io::Formatter< char >:

Static Public Member Functions

template<WriterImpl WI>
static Result< size_t, typename Writer< WI >::ErrType > fmt (Writer< WI > &writer, const FormatOptions &opts, const char &ch)
 

Detailed Description

Formatter for characters Format specifier options for output:

  • Format padding options (e.g. 0<2;) - Must come before other options if present (i.e. 0<2;x not x0<2;)
  • x for lowercase hex
  • X for uppercase hex
  • o for octal
  • b for binary
  • d for decimal (numeric, base 10)
  • s (or none) for string
See also
extract_padding_options

Definition at line 174 of file formats.hpp.

Member Function Documentation

◆ fmt()

template<WriterImpl WI>
Result< size_t, typename Writer< WI >::ErrType > mtcore::io::Formatter< char >::fmt ( Writer< WI > & writer,
const FormatOptions & opts,
const char & ch )
static

Definition at line 700 of file formats.hpp.

701 {
702 auto padOpts = extract_padding_options(opts.formatStr);
703 if (padOpts.has_value()) {
704 return Padded<char>::write_padded(writer, padOpts.value(), {opts.formatStr.sub(padOpts->endPos + 1)}, ch);
705 }
706
707 if (str_equal(opts.formatStr, "b")) {
709 (ch >> 7) & 0x1, (ch >> 6) & 0x1, (ch >> 5) & 0x1, (ch >> 4) & 0x1,
710 (ch >> 3) & 0x1, (ch >> 2) & 0x1, (ch >> 1) & 0x1, (ch >> 0) & 0x1,
711 };
712 auto bit = slice_from(bits);
713 auto start = slices::first_index(1, bit);
714
715 // write the minimal bits needed
716 if (!start.has_value()) {
717 return Formatter<int>::fmt(writer, {slice_from("d")}, 0);
718 }
719 else {
721 slice_from(bits).sub(start.value()));
722 }
723 }
724 else if (str_equal(opts.formatStr, "X")) {
725 constexpr auto chars = "0123456789ABCDEF";
726
727 auto c = chars[(ch >> 4) & 0xf];
728 // by default, no padding
729 if (c != '0') {
730 auto ch1 = writer.write(c);
731 if (ch1.is_error()) {
732 return ch1.error();
733 }
734 }
735 auto ch2 = writer.write(chars[ch & 0xf]);
736 if (ch2.is_error()) {
737 return ch2.error();
738 }
739 return static_cast<size_t>(c != '0' ? 2 : 1);
740 }
741 else if (str_equal(opts.formatStr, "x")) {
742 constexpr auto chars = "0123456789abcdef";
743 auto c = chars[(ch >> 4) & 0xf];
744 // by default, no padding
745 if (c != '0') {
746 auto ch1 = writer.write(c);
747 if (ch1.is_error()) {
748 return ch1.error();
749 }
750 }
751 auto ch2 = writer.write(chars[ch & 0xf]);
752 if (ch2.is_error()) {
753 return ch2.error();
754 }
755 return static_cast<size_t>(c != '0' ? 2 : 1);
756 }
757 else if (str_equal(opts.formatStr, "o")) {
758 constexpr auto chars = "01234567";
760 static_cast<char>(chars[(ch >> 6) & 0x5ull]),
761 static_cast<char>(chars[(ch >> 3) & 0x7ull]),
762 static_cast<char>(chars[(ch >> 0) & 0x7ull]),
763 };
766
767 size_t written = 0;
768 if (start.has_value()) {
769 auto out = octal.sub(start.value());
770 written += out.size();
771 auto res = writer.write_all(out);
772 return res.with_success_val(written);
773 }
774 else {
775 ++written;
776 return writer.write('0').with_success_val(written);
777 }
778 }
779 else if (str_equal(opts.formatStr, "d")) {
780 auto v = static_cast<u8>(ch);
781 return Formatter<u8>::fmt(writer, {slice_from("d")}, v);
782 }
783 else {
784 return writer.write(ch).with_success_val(static_cast<size_t>(1));
785 }
786 }
constexpr Slice< const char32_t > slice_from(char32_t *cstr)
Creates a slice from a utf32 string in the form of a c string.
mtcore::Optional< size_t > first_index(const Slice< T > &needle, const Slice< T > &haystack)
Gets the first index that a needle appears in the haystack, or nullopt if the needle does not appear ...
mtcore::Optional< size_t > first_index_not(const std::remove_const_t< T > &needle, const Slice< T > &haystack)
Gets the first index that a needle appears in the haystack, or nullopt if the needle does not appear ...
Optional< PaddingOptions > extract_padding_options(Slice< const char > formatStr)
Extracts padding options from a format string if present Padding options are in the following form: (...
Definition formats.hpp:71
bool str_equal(const L &left, const R &right)
Compares two string strings for ordering.
constexpr Slice sub(size_t start) const noexcept
Gets a sub Slice from start.
static Result< size_t, typename Writer< WI >::ErrType > fmt(Writer< WI > &writer, const FormatOptions &opts, const char &ch)
Definition formats.hpp:700
Struct to override to specify how a type should be formatted.
Definition format.hpp:45
static Result< size_t, typename Writer< WI >::ErrType > write_padded(Writer< WI > &writer, const PaddingOptions &padOpts, const FormatOptions &opts, const T &elem)
Definition formats.hpp:791
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this struct was generated from the following file: