71 mEnd {buffer + size - 1_usize} {}
76 auto isFull() const noexcept ->
bool {
82 void put(
const char symbol)
noexcept {
83 if (!
isFull()) [[likely]] {
90 void put(
const std::string_view text)
noexcept {
91 mPos = std::ranges::copy_n(text.data(),
static_cast<std::ptrdiff_t
>(std::min(text.size(),
space())),
mPos).out;
98 mPos = std::ranges::fill_n(
mPos,
static_cast<std::ptrdiff_t
>(std::min(count,
space())), symbol);
105 void padded(
const std::string_view prefix,
const std::string_view text,
const spec_t &spec)
noexcept {
106 const auto length {prefix.size() + text.size()};
107 const auto padding {(spec.width > length) ? (spec.width - length) : 0_usize};
112 }
else if (spec.zero) {
149 template<std::
integral T>
151 using U = std::make_unsigned_t<T>;
153 constexpr auto MINUS {std::string_view {
"-"}};
154 constexpr auto NONE {std::string_view {}};
155 const auto isNegative {std::cmp_less(value,
static_cast<T
>(0))};
156 const auto magnitude {isNegative ?
static_cast<U
>(
static_cast<U
>(0) -
static_cast<U
>(value)) :
static_cast<U
>(value)};
157 auto digits {std::array<char, std::numeric_limits<U>::digits> {}};
158 out.
padded(isNegative ? MINUS : NONE,
kdigits(digits, magnitude, radix, upper), spec);
212 while ((
'0' == fmt[1]) || (
'-' == fmt[1])) {
222 while ((
'0' <= fmt[1]) && (
'9' >= fmt[1])) {
223 spec.width = (spec.width * 10_usize) +
static_cast<igros_usize_t>(fmt[1] -
'0');
236 fmt += (
'l' == fmt[2]) ? 2 : 1;
258 if ((
nullptr == buffer) || (0_usize == size) || (
nullptr == format)) [[unlikely]] {
263 auto out {writer_t {buffer, size}};
265 std::va_list args {};
269 const auto* fmt {format};
270 while ((
'\0' != *fmt) && !out.isFull()) {
273 if (
'%' != *fmt) [[likely]] {
279 const auto spec {parseSpec(fmt)};
282 if (
'\0' == fmt[1]) [[unlikely]] {
294 const auto symbol {
static_cast<char>(va_arg(args,
igros_sdword_t))};
295 out.padded({}, std::string_view {&symbol, 1_usize}, spec);
299 const auto*
const str {va_arg(args,
const char*)};
300 out.padded({},
kstrview(str), spec);
305 putArgument(out, args,
radix_t::DEC, spec,
true,
false);
309 putArgument(out, args,
radix_t::DEC, spec,
false,
false);
312 putArgument(out, args,
radix_t::BIN, spec,
false,
false);
315 putArgument(out, args,
radix_t::OCT, spec,
false,
false);
318 putArgument(out, args,
radix_t::HEX, spec,
false,
false);
321 putArgument(out, args,
radix_t::HEX, spec,
false,
true);
339 return out.finish(buffer);
346 std::va_list list {};
348 va_start(list, format);
350 const auto length {
kvsnprintf(buffer, size, format, list)};
358 void kprintf(
const char*
const format, ...) noexcept {
360 constexpr auto NEWLINE {std::string_view {
"\n"}};
364 constinit static auto buffer {std::array<char, 1024_usize> {}};
366 std::va_list list {};
368 va_start(list, format);
370 const auto text {std::string_view {buffer.data(),
kvsnprintf(buffer.data(), buffer.size(), format, list)}};
Disable interrupts in scope (restores previous state on exit).
Output buffer writer: never writes past last position (reserved for terminator).
void padded(const std::string_view prefix, const std::string_view text, const spec_t &spec) noexcept
Prefix (sign) and text padded to field width.
auto space() const noexcept -> igros_usize_t
Space left.
auto isFull() const noexcept -> bool
No space left.
auto finish(const char *const buffer) noexcept -> igros_usize_t
Terminate, return written length.
writer_t(char *const buffer, const igros_usize_t size) noexcept
Writer for buffer of given size (at least 1).
void fill(const char symbol, const igros_usize_t count) noexcept
Repeated character (clipped).
void put(const std::string_view text) noexcept
Text (clipped; no substr: it may throw).
char *const mEnd
Terminator position.
void put(const char symbol) noexcept
Character.
char * mPos
Current position.
std::uint16_t igros_word_t
16-bit unsigned integer
std::int64_t igros_squad_t
64-bit signed integer
std::size_t igros_usize_t
Unsigned size.
std::uint8_t igros_byte_t
8-bit unsigned integer
std::uint32_t igros_dword_t
32-bit unsigned integer
std::add_pointer_t< void > igros_pointer_t
Common pointer type.
std::int8_t igros_sbyte_t
8-bit signed integer
std::ptrdiff_t igros_ssize_t
Signed size (difference).
std::int16_t igros_sword_t
16-bit signed integer
std::int32_t igros_sdword_t
32-bit signed integer
std::uint64_t igros_quad_t
64-bit unsigned integer
Architecture interrupts type deduction.
Kernel text print functions.
auto serialWrite(const SERIAL_PORT port, const std::string_view text) noexcept -> igros_usize_t
Serial write.
void vmemWrite(const char symbol) noexcept
Write symbol to VGA memory.
argSize_t
Argument size (length modifier).
void putArgument(writer_t &out, std::va_list &list, const radix_t radix, const spec_t &spec, const bool isSigned, const bool upper) noexcept
Integer argument of given size: signed or unsigned.
auto parseSpec(const char *&fmt) noexcept -> spec_t
Parse flags, field width and length modifier of a placeholder.
void putInteger(writer_t &out, const T value, const radix_t radix, const spec_t &spec, const bool upper=false) noexcept
Integer with sign (signed types, decimal only) and padding.
Freestanding kernel library: printing, math, alignment, flags, strings.
auto ksnprintf(char *const buffer, const igros_usize_t size, const char *const format,...) noexcept -> igros_usize_t
Kernel snprintf function (returns written length without terminator).
constexpr auto kstrview(const char *const src) noexcept -> std::string_view
View of null-terminated string (null pointer is empty string).
void kprintf(const char *const format,...) noexcept
Kernel printf function (arguments are checked against format at compile time).
auto kvsnprintf(char *const buffer, const igros_usize_t size, const char *const format, std::va_list list) noexcept -> igros_usize_t
Kernel vsnprintf function (returns written length without terminator).
radix_t
Integer representation types.
@ BIN
(Base 2) Binary integer format
@ DEC
(Base 10) Decimal integer format
@ OCT
(Base 8) Oct integer format
@ HEX
(Base 16) Hexidemical integer format
constexpr auto kdigits(const std::span< char > buffer, U value, const radix_t radix, const bool upper=false) noexcept -> std::string_view
Digits of unsigned value written to the end of buffer (no sign, no terminator).
Conversion specification.
igros_usize_t width
Minimal field width.
bool zero
Pad numbers with '0' (after sign).
argSize_t size
Argument size.
bool left
Left-justify (pad with spaces after text).
VGA memory low-level operations.