aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd/include
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-04-13 16:18:00 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-04-13 16:18:00 +0200
commit3795115641bf5c1d1a3d60313408ba462057ba18 (patch)
tree8dd4baafe46561d64fd72ae086ebc2ddaa719816 /libs/kstd/include
parentcd1dd2037cbe1d5f1362202d3127640406b468b8 (diff)
downloadteachos-3795115641bf5c1d1a3d60313408ba462057ba18.tar.xz
teachos-3795115641bf5c1d1a3d60313408ba462057ba18.zip
kstd/format: add support for char formatting
Diffstat (limited to 'libs/kstd/include')
-rw-r--r--libs/kstd/include/kstd/bits/format/formatter/char.hpp94
-rw-r--r--libs/kstd/include/kstd/format1
2 files changed, 95 insertions, 0 deletions
diff --git a/libs/kstd/include/kstd/bits/format/formatter/char.hpp b/libs/kstd/include/kstd/bits/format/formatter/char.hpp
new file mode 100644
index 0000000..ddfefe5
--- /dev/null
+++ b/libs/kstd/include/kstd/bits/format/formatter/char.hpp
@@ -0,0 +1,94 @@
+#ifndef KSTD_BITS_FORMAT_FORMATTER_CHAR_HPP
+#define KSTD_BITS_FORMAT_FORMATTER_CHAR_HPP
+
+#include "../context.hpp"
+#include "../error.hpp"
+#include "../formatter.hpp"
+#include "../parse_context.hpp"
+#include "../specifiers.hpp"
+#include "integral.hpp"
+
+#include <iterator>
+
+namespace kstd
+{
+
+ template<>
+ struct formatter<char> : formatter<unsigned char>
+ {
+ bits::format::specifiers specifiers{};
+
+ constexpr auto parse(format_parse_context & context) -> format_parse_context::iterator
+ {
+ specifiers = bits::format::parse_format_specifiers(context);
+
+ auto it = context.begin();
+ auto const end = context.end();
+
+ if (specifiers.alternative_form || specifiers.zero_pad || specifiers.sign != bits::format::sign_mode::none)
+ {
+ bits::format::error("Invalid format specifiers for 'char'");
+ }
+
+ if (it != end && *it != '}')
+ {
+ if (*it == 'c' || *it == 'b' || *it == 'B' || *it == 'd' || *it == 'o' || *it == 'x' || *it == 'X')
+ {
+ specifiers.type = *it;
+ std::advance(it, 1);
+ }
+ else
+ {
+ bits::format::error("Invalid type specifier for char.");
+ }
+ }
+
+ if (it != end && *it != '}')
+ {
+ bits::format::error("Missing terminating '}' in format string.");
+ }
+
+ return it;
+ }
+
+ auto format(char value, format_context & context) const -> void
+ {
+ if (specifiers.type == '\0' || specifiers.type == 'c')
+ {
+ auto final_width = 0uz;
+
+ if (specifiers.mode == bits::format::width_mode::static_value)
+ {
+ final_width = specifiers.width_value;
+ }
+ else if (specifiers.mode == bits::format::width_mode::dynamic_argument_id)
+ {
+ auto const & arg = context.arg(specifiers.width_value);
+ final_width = bits::format::extrat_dynamic_width(arg);
+ }
+
+ auto padding =
+ bits::format::calculate_format_padding(final_width, 1, specifiers.align, bits::format::alignment::left);
+
+ for (auto i = 0uz; i < padding.left; ++i)
+ {
+ context.push(specifiers.fill);
+ }
+
+ context.push(value);
+
+ for (auto i = 0uz; i < padding.right; ++i)
+ {
+ context.push(specifiers.fill);
+ }
+ }
+ else
+ {
+ formatter<unsigned char>::format(static_cast<unsigned char>(value), context);
+ }
+ }
+ };
+
+} // namespace kstd
+
+#endif \ No newline at end of file
diff --git a/libs/kstd/include/kstd/format b/libs/kstd/include/kstd/format
index d11c221..047ea5c 100644
--- a/libs/kstd/include/kstd/format
+++ b/libs/kstd/include/kstd/format
@@ -7,6 +7,7 @@
#include "bits/format/formatter.hpp" // IWYU pragma: export
#include "bits/format/formatter/bool.hpp" // IWYU pragma: export
#include "bits/format/formatter/byte.hpp" // IWYU pragma: export
+#include "bits/format/formatter/char.hpp" // IWYU pragma: export
#include "bits/format/formatter/cstring.hpp" // IWYU pragma: export
#include "bits/format/formatter/integral.hpp" // IWYU pragma: export
#include "bits/format/formatter/ordering.hpp" // IWYU pragma: export