HelenOS sources
This source file includes following definitions.
- _set_errno
- _utf8_bytes
- _utf8_wstr_bytes_len
- _saturating_add
- _write_bytes
- _write_uchar
- _write_chars
- _write_char
- _write_spaces
- _write_zeros
- _format_char
- _format_uchar
- _format_cstr
- _format_wstr
- _sign
- _format_number
- _get_sign_char
- _format_special
- _fp_trim_trailing_zeros
- _fp_round_up
- _format_double_str_fixed
- _format_double_fixed
- _format_exponent
- _format_double_str_scient
- _format_double_scientific
- _format_double_generic
- _format_double
- _strchrnul
- _read_num
- _parse_flags
- _eat_char
- _read_qualifier
- printf_core
#include <_bits/uchar.h>
#include <_bits/wint_t.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <macros.h>
#include <printf_core.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <str.h>
#if __STDC_HOSTED__
#define HAS_FLOAT
#endif
#ifdef HAS_FLOAT
#include <double_to_str.h>
#include <ieee_double.h>
#endif
#define __PRINTF_FLAG_PREFIX 0x00000001
#define __PRINTF_FLAG_DECIMALPT 0x00000001
#define __PRINTF_FLAG_SIGNED 0x00000002
#define __PRINTF_FLAG_ZEROPADDED 0x00000004
#define __PRINTF_FLAG_LEFTALIGNED 0x00000010
#define __PRINTF_FLAG_SHOWPLUS 0x00000020
#define __PRINTF_FLAG_SPACESIGN 0x00000040
#define __PRINTF_FLAG_BIGCHARS 0x00000080
#define __PRINTF_FLAG_NEGATIVE 0x00000100
#define __PRINTF_FLAG_NOFRACZEROS 0x00000200
#define PRINT_NUMBER_BUFFER_SIZE 64
#define PRINTF_GET_INT_ARGUMENT(type, ap, flags) \
({ \
unsigned type res; \
\
if ((flags) & __PRINTF_FLAG_SIGNED) { \
signed type arg = va_arg((ap), signed type); \
\
if (arg < 0) { \
res = -arg; \
(flags) |= __PRINTF_FLAG_NEGATIVE; \
} else \
res = arg; \
} else \
res = va_arg((ap), unsigned type); \
\
res; \
})
typedef enum {
PrintfQualifierByte = 0,
PrintfQualifierShort,
PrintfQualifierInt,
PrintfQualifierLong,
PrintfQualifierLongLong,
PrintfQualifierPointer,
} qualifier_t;
static const char _digits_small[] = "0123456789abcdef";
static const char _digits_big[] = "0123456789ABCDEF";
static const char _nullstr[] = "(NULL)";
static const char _replacement[] = u8"�";
static const char _spaces[] = " ";
static const char _zeros[] = "000000000000000000000000000000000000000000000000";
static void _set_errno(errno_t rc)
{
#ifdef errno
errno = rc;
#endif
}
static size_t _utf8_bytes(char32_t c)
{
if (c < 0x80)
return 1;
if (c < 0x800)
return 2;
if (c < 0xD800)
return 3;
if (c < 0xE000)
return sizeof(_replacement) - 1;
if (c < 0x10000)
return 3;
if (c < 0x110000)
return 4;
return sizeof(_replacement) - 1;
}
static size_t _utf8_wstr_bytes_len(char32_t *s, size_t max_bytes, size_t *len)
{
size_t bytes = 0;
size_t i;
for (i = 0; bytes < max_bytes && s[i]; i++) {
size_t next = _utf8_bytes(s[i]);
if (max_bytes - bytes < next)
break;
bytes += next;
}
*len = i;
return bytes;
}
#define TRY(expr) ({ errno_t rc = (expr); if (rc != EOK) return rc; })
static inline void _saturating_add(size_t *a, size_t b)
{
size_t s = *a + b;
*a = (s < b) ? SIZE_MAX : s;
}
static errno_t _write_bytes(const char *buf, size_t n, printf_spec_t *ps,
size_t *written_bytes)
{
errno_t rc = ps->write(buf, n, ps->data);
if (rc != EOK)
return rc;
_saturating_add(written_bytes, n);
return EOK;
}
static errno_t _write_uchar(char32_t ch, printf_spec_t *ps,
size_t *written_bytes)
{
char utf8[4];
size_t offset = 0;
if (chr_encode(ch, utf8, &offset, sizeof(utf8)) == EOK)
return _write_bytes(utf8, offset, ps, written_bytes);
return _write_bytes(_replacement, sizeof(_replacement) - 1, ps, written_bytes);
}
static errno_t _write_chars(const char32_t *buf, size_t n, printf_spec_t *ps,
size_t *written_bytes)
{
for (size_t i = 0; i < n; i++)
TRY(_write_uchar(buf[i], ps, written_bytes));
return EOK;
}
static errno_t _write_char(char c, printf_spec_t *ps, size_t *written_bytes)
{
return _write_bytes(&c, 1, ps, written_bytes);
}
static errno_t _write_spaces(size_t n, printf_spec_t *ps, size_t *written_bytes)
{
size_t max_spaces = sizeof(_spaces) - 1;
while (n > max_spaces) {
TRY(_write_bytes(_spaces, max_spaces, ps, written_bytes));
n -= max_spaces;
}
return _write_bytes(_spaces, n, ps, written_bytes);
}
static errno_t _write_zeros(size_t n, printf_spec_t *ps, size_t *written_bytes)
{
size_t max_zeros = sizeof(_zeros) - 1;
while (n > max_zeros) {
TRY(_write_bytes(_zeros, max_zeros, ps, written_bytes));
n -= max_zeros;
}
return _write_bytes(_zeros, n, ps, written_bytes);
}
static errno_t _format_char(const char c, size_t width, uint32_t flags,
printf_spec_t *ps, size_t *written_bytes)
{
size_t bytes = 1;
if (width <= bytes)
return _write_char(c, ps, written_bytes);
if (flags & __PRINTF_FLAG_LEFTALIGNED) {
TRY(_write_char(c, ps, written_bytes));
TRY(_write_spaces(width - bytes, ps, written_bytes));
} else {
TRY(_write_spaces(width - bytes, ps, written_bytes));
TRY(_write_char(c, ps, written_bytes));
}
return EOK;
}
static errno_t _format_uchar(const char32_t ch, size_t width, uint32_t flags,
printf_spec_t *ps, size_t *written_bytes)
{
size_t bytes = _utf8_bytes(ch);
if (width <= bytes)
return _write_uchar(ch, ps, written_bytes);
if (flags & __PRINTF_FLAG_LEFTALIGNED) {
TRY(_write_uchar(ch, ps, written_bytes));
TRY(_write_spaces(width - bytes, ps, written_bytes));
} else {
TRY(_write_spaces(width - bytes, ps, written_bytes));
TRY(_write_uchar(ch, ps, written_bytes));
}
return EOK;
}
static errno_t _format_cstr(const char *str, size_t width, int precision,
uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
{
if (str == NULL)
str = _nullstr;
size_t max_bytes = (precision < 0) ? SIZE_MAX : (size_t) precision;
size_t bytes = str_nsize(str, max_bytes);
if (width <= bytes)
return _write_bytes(str, bytes, ps, written_bytes);
if (flags & __PRINTF_FLAG_LEFTALIGNED) {
TRY(_write_bytes(str, bytes, ps, written_bytes));
TRY(_write_spaces(width - bytes, ps, written_bytes));
} else {
TRY(_write_spaces(width - bytes, ps, written_bytes));
TRY(_write_bytes(str, bytes, ps, written_bytes));
}
return EOK;
}
static errno_t _format_wstr(char32_t *str, size_t width, int precision,
uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
{
if (!str)
return _format_cstr(_nullstr, width, precision, flags, ps, written_bytes);
size_t max_bytes = (precision < 0) ? SIZE_MAX : (size_t) precision;
size_t len;
size_t bytes = _utf8_wstr_bytes_len(str, max_bytes, &len);
if (width <= bytes)
return _write_chars(str, len, ps, written_bytes);
if (flags & __PRINTF_FLAG_LEFTALIGNED) {
TRY(_write_chars(str, len, ps, written_bytes));
TRY(_write_spaces(width - bytes, ps, written_bytes));
} else {
TRY(_write_spaces(width - bytes, ps, written_bytes));
TRY(_write_chars(str, len, ps, written_bytes));
}
return EOK;
}
static char _sign(uint32_t flags)
{
if (!(flags & __PRINTF_FLAG_SIGNED))
return 0;
if (flags & __PRINTF_FLAG_NEGATIVE)
return '-';
if (flags & __PRINTF_FLAG_SHOWPLUS)
return '+';
if (flags & __PRINTF_FLAG_SPACESIGN)
return ' ';
return 0;
}
static errno_t _format_number(uint64_t num, size_t width, int precision, int base,
uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
{
assert(base >= 2 && base <= 16);
size_t min_digits = (precision < 0) ? 1 : precision;
bool bigchars = flags & __PRINTF_FLAG_BIGCHARS;
bool prefix = flags & __PRINTF_FLAG_PREFIX;
bool left_aligned = flags & __PRINTF_FLAG_LEFTALIGNED;
bool zero_padded = flags & __PRINTF_FLAG_ZEROPADDED;
const char *digits = bigchars ? _digits_big : _digits_small;
char buffer[PRINT_NUMBER_BUFFER_SIZE];
char *end = &buffer[PRINT_NUMBER_BUFFER_SIZE];
int offset = 0;
while (num > 0) {
end[--offset] = digits[num % base];
num /= base;
}
char *number = &end[offset];
size_t number_len = end - number;
char sign = _sign(flags);
if (left_aligned) {
size_t real_size = max(number_len, min_digits);
if (sign) {
TRY(_write_char(sign, ps, written_bytes));
real_size++;
}
if (prefix && base == 2 && number_len > 0) {
TRY(_write_bytes(bigchars ? "0B" : "0b", 2, ps, written_bytes));
real_size += 2;
}
if (prefix && base == 16 && number_len > 0) {
TRY(_write_bytes(bigchars ? "0X" : "0x", 2, ps, written_bytes));
real_size += 2;
}
if (min_digits > number_len) {
TRY(_write_zeros(min_digits - number_len, ps, written_bytes));
} else if (prefix && base == 8) {
TRY(_write_zeros(1, ps, written_bytes));
real_size++;
}
TRY(_write_bytes(number, number_len, ps, written_bytes));
if (width > real_size)
TRY(_write_spaces(width - real_size, ps, written_bytes));
return EOK;
}
if (precision < 0 && zero_padded) {
size_t real_size = number_len;
if (sign) {
TRY(_write_char(sign, ps, written_bytes));
real_size++;
}
if (prefix && base == 2 && number_len > 0) {
TRY(_write_bytes(bigchars ? "0B" : "0b", 2, ps, written_bytes));
real_size += 2;
}
if (prefix && base == 16 && number_len > 0) {
TRY(_write_bytes(bigchars ? "0X" : "0x", 2, ps, written_bytes));
real_size += 2;
}
if (width > real_size)
TRY(_write_zeros(width - real_size, ps, written_bytes));
else if (number_len == 0 || (prefix && base == 8))
TRY(_write_char('0', ps, written_bytes));
return _write_bytes(number, number_len, ps, written_bytes);
}
size_t real_size = max(number_len, min_digits);
if (sign)
real_size++;
if (prefix && (base == 2 || base == 16) && number_len > 0)
real_size += 2;
if (prefix && base == 8 && number_len >= min_digits)
real_size += 1;
if (width > real_size)
TRY(_write_spaces(width - real_size, ps, written_bytes));
if (sign)
TRY(_write_char(sign, ps, written_bytes));
if (prefix && base == 2 && number_len > 0)
TRY(_write_bytes(bigchars ? "0B" : "0b", 2, ps, written_bytes));
if (prefix && base == 16 && number_len > 0)
TRY(_write_bytes(bigchars ? "0X" : "0x", 2, ps, written_bytes));
if (min_digits > number_len)
TRY(_write_zeros(min_digits - number_len, ps, written_bytes));
else if (prefix && base == 8)
TRY(_write_char('0', ps, written_bytes));
return _write_bytes(number, number_len, ps, written_bytes);
}
#ifdef HAS_FLOAT
typedef struct {
char *str;
int len;
int dec_exp;
bool neg;
} double_str_t;
static char _get_sign_char(bool negative, uint32_t flags)
{
if (negative) {
return '-';
} else if (flags & __PRINTF_FLAG_SHOWPLUS) {
return '+';
} else if (flags & __PRINTF_FLAG_SPACESIGN) {
return ' ';
} else {
return 0;
}
}
static errno_t _format_special(ieee_double_t val, int width, uint32_t flags,
printf_spec_t *ps, size_t *written_bytes)
{
assert(val.is_special);
char sign = _get_sign_char(val.is_negative, flags);
const int str_len = 3;
const char *str;
if (flags & __PRINTF_FLAG_BIGCHARS) {
str = val.is_infinity ? "INF" : "NAN";
} else {
str = val.is_infinity ? "inf" : "nan";
}
int padding_len = max(0, width - ((sign ? 1 : 0) + str_len));
if (!(flags & __PRINTF_FLAG_LEFTALIGNED))
TRY(_write_spaces(padding_len, ps, written_bytes));
if (sign)
TRY(_write_char(sign, ps, written_bytes));
TRY(_write_bytes(str, str_len, ps, written_bytes));
if (flags & __PRINTF_FLAG_LEFTALIGNED)
TRY(_write_spaces(padding_len, ps, written_bytes));
return EOK;
}
static void _fp_trim_trailing_zeros(char *buf, int *len, int *dec_exp)
{
while (2 <= *len && '0' == buf[*len - 1]) {
--*len;
++*dec_exp;
}
}
static void _fp_round_up(char *buf, int *len, int *dec_exp)
{
assert(1 <= *len);
char *last_digit = &buf[*len - 1];
int carry = ('5' <= *last_digit);
--*len;
++*dec_exp;
--last_digit;
if (carry) {
while (buf <= last_digit && '9' == *last_digit) {
--last_digit;
}
if (buf <= last_digit) {
*last_digit += 1;
int new_len = last_digit - buf + 1;
*dec_exp += *len - new_len;
*len = new_len;
} else {
buf[0] = '1';
*dec_exp += *len;
*len = 1;
}
} else {
if (last_digit < buf) {
buf[0] = '0';
*dec_exp = 0;
*len = 1;
}
}
}
static errno_t _format_double_str_fixed(double_str_t *val_str, int precision, int width,
uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
{
int len = val_str->len;
char *buf = val_str->str;
int dec_exp = val_str->dec_exp;
assert(0 < len);
assert(0 <= precision);
assert(0 <= dec_exp || -dec_exp <= precision);
int int_len = max(1, len + dec_exp);
char sign = _get_sign_char(val_str->neg, flags);
int last_frac_signif_pos = max(0, -dec_exp);
int leading_frac_zeros = max(0, last_frac_signif_pos - len);
int signif_frac_figs = min(last_frac_signif_pos, len);
int trailing_frac_zeros = precision - last_frac_signif_pos;
char *buf_frac = buf + len - signif_frac_figs;
if (flags & __PRINTF_FLAG_NOFRACZEROS)
trailing_frac_zeros = 0;
int frac_len = leading_frac_zeros + signif_frac_figs + trailing_frac_zeros;
bool has_decimal_pt = (0 < frac_len) || (flags & __PRINTF_FLAG_DECIMALPT);
int num_len = (sign ? 1 : 0) + int_len + (has_decimal_pt ? 1 : 0) + frac_len;
int padding_len = max(0, width - num_len);
if (!(flags & (__PRINTF_FLAG_LEFTALIGNED | __PRINTF_FLAG_ZEROPADDED)))
TRY(_write_spaces(padding_len, ps, written_bytes));
if (sign)
TRY(_write_char(sign, ps, written_bytes));
if (flags & __PRINTF_FLAG_ZEROPADDED)
TRY(_write_zeros(padding_len, ps, written_bytes));
int buf_int_len = min(len, len + dec_exp);
if (0 < buf_int_len) {
TRY(_write_bytes(buf, buf_int_len, ps, written_bytes));
TRY(_write_zeros(int_len - buf_int_len, ps, written_bytes));
} else {
TRY(_write_char('0', ps, written_bytes));
}
if (has_decimal_pt) {
TRY(_write_char('.', ps, written_bytes));
TRY(_write_zeros(leading_frac_zeros, ps, written_bytes));
if (0 < signif_frac_figs)
TRY(_write_bytes(buf_frac, signif_frac_figs, ps, written_bytes));
TRY(_write_zeros(trailing_frac_zeros, ps, written_bytes));
}
if (flags & __PRINTF_FLAG_LEFTALIGNED)
TRY(_write_spaces(padding_len, ps, written_bytes));
return EOK;
}
static errno_t _format_double_fixed(double g, int precision, int width,
uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
{
if (flags & __PRINTF_FLAG_LEFTALIGNED) {
flags &= ~__PRINTF_FLAG_ZEROPADDED;
}
if (flags & __PRINTF_FLAG_DECIMALPT) {
flags &= ~__PRINTF_FLAG_NOFRACZEROS;
}
ieee_double_t val = extract_ieee_double(g);
if (val.is_special) {
return _format_special(val, width, flags, ps, written_bytes);
}
char buf[MAX_DOUBLE_STR_BUF_SIZE];
const size_t buf_size = MAX_DOUBLE_STR_BUF_SIZE;
double_str_t val_str;
val_str.str = buf;
val_str.neg = val.is_negative;
if (0 <= precision) {
val_str.len = double_to_fixed_str(val, -1, precision + 1, buf, buf_size,
&val_str.dec_exp);
_fp_round_up(buf, &val_str.len, &val_str.dec_exp);
if (flags & __PRINTF_FLAG_NOFRACZEROS) {
_fp_trim_trailing_zeros(buf, &val_str.len, &val_str.dec_exp);
}
} else {
val_str.len = double_to_short_str(val, buf, buf_size, &val_str.dec_exp);
precision = max(0, -val_str.dec_exp);
}
return _format_double_str_fixed(&val_str, precision, width, flags, ps, written_bytes);
}
static errno_t _format_exponent(int exp_val, uint32_t flags, printf_spec_t *ps,
size_t *written_bytes)
{
char exp_ch = (flags & __PRINTF_FLAG_BIGCHARS) ? 'E' : 'e';
TRY(_write_char(exp_ch, ps, written_bytes));
char exp_sign = (exp_val < 0) ? '-' : '+';
TRY(_write_char(exp_sign, ps, written_bytes));
exp_val = abs(exp_val);
char exp_str[4] = { 0 };
exp_str[0] = '0' + exp_val / 100;
exp_str[1] = '0' + (exp_val % 100) / 10;
exp_str[2] = '0' + (exp_val % 10);
int exp_len = (exp_str[0] == '0') ? 2 : 3;
const char *exp_str_start = &exp_str[3] - exp_len;
return _write_bytes(exp_str_start, exp_len, ps, written_bytes);
}
static errno_t _format_double_str_scient(double_str_t *val_str, int precision,
int width, uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
{
int len = val_str->len;
int dec_exp = val_str->dec_exp;
char *buf = val_str->str;
assert(0 < len);
char sign = _get_sign_char(val_str->neg, flags);
bool has_decimal_pt = (0 < precision) || (flags & __PRINTF_FLAG_DECIMALPT);
int dec_pt_len = has_decimal_pt ? 1 : 0;
int signif_frac_figs = len - 1;
int trailing_frac_zeros = precision - signif_frac_figs;
if (flags & __PRINTF_FLAG_NOFRACZEROS) {
trailing_frac_zeros = 0;
}
int frac_len = signif_frac_figs + trailing_frac_zeros;
int exp_val = dec_exp + len - 1;
int exp_len = 2 + (abs(exp_val) >= 100 ? 3 : 2);
int num_len = (sign ? 1 : 0) + 1 + dec_pt_len + frac_len + exp_len;
int padding_len = max(0, width - num_len);
if (!(flags & (__PRINTF_FLAG_LEFTALIGNED | __PRINTF_FLAG_ZEROPADDED)))
TRY(_write_spaces(padding_len, ps, written_bytes));
if (sign)
TRY(_write_char(sign, ps, written_bytes));
if (flags & __PRINTF_FLAG_ZEROPADDED)
TRY(_write_zeros(padding_len, ps, written_bytes));
TRY(_write_char(buf[0], ps, written_bytes));
if (has_decimal_pt) {
TRY(_write_char('.', ps, written_bytes));
if (0 < signif_frac_figs)
TRY(_write_bytes(buf + 1, signif_frac_figs, ps, written_bytes));
TRY(_write_zeros(trailing_frac_zeros, ps, written_bytes));
}
TRY(_format_exponent(exp_val, flags, ps, written_bytes));
if (flags & __PRINTF_FLAG_LEFTALIGNED)
TRY(_write_spaces(padding_len, ps, written_bytes));
return EOK;
}
static errno_t _format_double_scientific(double g, int precision, int width,
uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
{
if (flags & __PRINTF_FLAG_LEFTALIGNED)
flags &= ~__PRINTF_FLAG_ZEROPADDED;
ieee_double_t val = extract_ieee_double(g);
if (val.is_special)
return _format_special(val, width, flags, ps, written_bytes);
char buf[MAX_DOUBLE_STR_BUF_SIZE];
const size_t buf_size = MAX_DOUBLE_STR_BUF_SIZE;
double_str_t val_str;
val_str.str = buf;
val_str.neg = val.is_negative;
if (0 <= precision) {
val_str.len = double_to_fixed_str(val, precision + 2, -1, buf, buf_size,
&val_str.dec_exp);
_fp_round_up(buf, &val_str.len, &val_str.dec_exp);
if (flags & __PRINTF_FLAG_NOFRACZEROS) {
_fp_trim_trailing_zeros(buf, &val_str.len, &val_str.dec_exp);
}
} else {
val_str.len = double_to_short_str(val, buf, buf_size, &val_str.dec_exp);
precision = val_str.len - 1;
}
return _format_double_str_scient(&val_str, precision, width, flags, ps, written_bytes);
}
static errno_t _format_double_generic(double g, int precision, int width,
uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
{
ieee_double_t val = extract_ieee_double(g);
if (val.is_special)
return _format_special(val, width, flags, ps, written_bytes);
char buf[MAX_DOUBLE_STR_BUF_SIZE];
const size_t buf_size = MAX_DOUBLE_STR_BUF_SIZE;
int dec_exp;
int len;
if (0 <= precision) {
len = double_to_fixed_str(val, 1, -1, buf, buf_size, &dec_exp);
assert(0 < len);
precision = max(1, precision);
if (-4 <= dec_exp && dec_exp < precision) {
precision = precision - (dec_exp + 1);
return _format_double_fixed(g, precision, width,
flags | __PRINTF_FLAG_NOFRACZEROS, ps, written_bytes);
} else {
--precision;
return _format_double_scientific(g, precision, width,
flags | __PRINTF_FLAG_NOFRACZEROS, ps, written_bytes);
}
} else {
len = double_to_short_str(val, buf, buf_size, &dec_exp);
assert(0 < len);
if (flags & __PRINTF_FLAG_LEFTALIGNED) {
flags &= ~__PRINTF_FLAG_ZEROPADDED;
}
double_str_t val_str;
val_str.str = buf;
val_str.len = len;
val_str.neg = val.is_negative;
val_str.dec_exp = dec_exp;
int first_digit_pos = len + dec_exp;
int last_digit_pos = dec_exp;
if (len <= 15 && -6 <= last_digit_pos && first_digit_pos <= 15) {
precision = max(0, -val_str.dec_exp);
return _format_double_str_fixed(&val_str, precision, width, flags, ps, written_bytes);
} else {
precision = val_str.len - 1;
return _format_double_str_scient(&val_str, precision, width, flags, ps, written_bytes);
}
}
}
static errno_t _format_double(double g, char spec, int precision, int width,
uint32_t flags, printf_spec_t *ps, size_t *written_chars)
{
switch (spec) {
case 'F':
flags |= __PRINTF_FLAG_BIGCHARS;
case 'f':
precision = (precision < 0) ? 6 : precision;
return _format_double_fixed(g, precision, width, flags, ps, written_chars);
case 'E':
flags |= __PRINTF_FLAG_BIGCHARS;
case 'e':
precision = (precision < 0) ? 6 : precision;
return _format_double_scientific(g, precision, width, flags, ps, written_chars);
case 'G':
flags |= __PRINTF_FLAG_BIGCHARS;
case 'g':
return _format_double_generic(g, precision, width, flags, ps, written_chars);
default:
assert(false);
return -1;
}
}
#endif
static const char *_strchrnul(const char *s, int c)
{
while (*s != c && *s != 0)
s++;
return s;
}
static int _read_num(const char *fmt, size_t *i)
{
const char *s;
unsigned n = 0;
for (s = &fmt[*i]; isdigit(*s); s++) {
unsigned digit = (*s - '0');
if (n > INT_MAX / 10 || n * 10 > INT_MAX - digit) {
n = INT_MAX;
while (isdigit(*s))
s++;
break;
}
n = n * 10 + digit;
}
*i = s - fmt;
return n;
}
static uint32_t _parse_flags(const char *fmt, size_t *i)
{
uint32_t flags = 0;
while (true) {
switch (fmt[(*i)++]) {
case '#':
flags |= __PRINTF_FLAG_PREFIX;
flags |= __PRINTF_FLAG_DECIMALPT;
continue;
case '-':
flags |= __PRINTF_FLAG_LEFTALIGNED;
continue;
case '+':
flags |= __PRINTF_FLAG_SHOWPLUS;
continue;
case ' ':
flags |= __PRINTF_FLAG_SPACESIGN;
continue;
case '0':
flags |= __PRINTF_FLAG_ZEROPADDED;
continue;
}
--*i;
break;
}
return flags;
}
static bool _eat_char(const char *s, size_t *idx, int c)
{
if (s[*idx] != c)
return false;
(*idx)++;
return true;
}
static qualifier_t _read_qualifier(const char *s, size_t *idx)
{
switch (s[(*idx)++]) {
case 't':
case 'z':
if (sizeof(ptrdiff_t) == sizeof(int))
return PrintfQualifierInt;
else
return PrintfQualifierLong;
case 'h':
if (_eat_char(s, idx, 'h'))
return PrintfQualifierByte;
else
return PrintfQualifierShort;
case 'l':
if (_eat_char(s, idx, 'l'))
return PrintfQualifierLongLong;
else
return PrintfQualifierLong;
case 'j':
return PrintfQualifierLongLong;
default:
--*idx;
return PrintfQualifierInt;
}
}
int printf_core(const char *fmt, printf_spec_t *ps, va_list ap)
{
errno_t rc = EOK;
size_t nxt = 0;
size_t counter = 0;
while (rc == EOK) {
const char *s = _strchrnul(&fmt[nxt], '%');
size_t bytes = s - &fmt[nxt];
rc = _write_bytes(&fmt[nxt], bytes, ps, &counter);
if (rc != EOK)
break;
nxt += bytes;
if (_eat_char(fmt, &nxt, 0))
break;
bool spec = _eat_char(fmt, &nxt, '%');
assert(spec);
uint32_t flags = _parse_flags(fmt, &nxt);
int width = -1;
if (_eat_char(fmt, &nxt, '*')) {
width = va_arg(ap, int);
if (width < 0) {
width = (width == INT_MIN) ? INT_MAX : -width;
flags |= __PRINTF_FLAG_LEFTALIGNED;
}
} else {
width = _read_num(fmt, &nxt);
}
int precision = -1;
if (_eat_char(fmt, &nxt, '.')) {
if (_eat_char(fmt, &nxt, '*')) {
precision = va_arg(ap, int);
if (precision < 0)
precision = -1;
} else {
precision = _read_num(fmt, &nxt);
}
}
qualifier_t qualifier = _read_qualifier(fmt, &nxt);
unsigned int base = 10;
char specifier = fmt[nxt++];
switch (specifier) {
case 's':
if (qualifier == PrintfQualifierLong)
rc = _format_wstr(va_arg(ap, char32_t *), width, precision, flags, ps, &counter);
else
rc = _format_cstr(va_arg(ap, char *), width, precision, flags, ps, &counter);
continue;
case 'c':
if (qualifier == PrintfQualifierLong)
rc = _format_uchar(va_arg(ap, wint_t), width, flags, ps, &counter);
else
rc = _format_char(va_arg(ap, int), width, flags, ps, &counter);
continue;
case 'G':
case 'g':
case 'F':
case 'f':
case 'E':
case 'e':;
#ifdef HAS_FLOAT
rc = _format_double(va_arg(ap, double), specifier, precision,
width, flags, ps, &counter);
#else
rc = _format_cstr("<float unsupported>", width, -1, 0, ps, &counter);
#endif
continue;
case 'P':
flags |= __PRINTF_FLAG_BIGCHARS;
case 'p':
flags |= __PRINTF_FLAG_PREFIX;
flags |= __PRINTF_FLAG_ZEROPADDED;
base = 16;
qualifier = PrintfQualifierPointer;
break;
case 'b':
base = 2;
break;
case 'o':
base = 8;
break;
case 'd':
case 'i':
flags |= __PRINTF_FLAG_SIGNED;
break;
case 'u':
break;
case 'X':
flags |= __PRINTF_FLAG_BIGCHARS;
case 'x':
base = 16;
break;
case '%':
rc = _write_char('%', ps, &counter);
continue;
default:
rc = EINVAL;
continue;
}
uint64_t number;
switch (qualifier) {
case PrintfQualifierByte:
number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);
break;
case PrintfQualifierShort:
number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);
break;
case PrintfQualifierInt:
number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);
break;
case PrintfQualifierLong:
number = PRINTF_GET_INT_ARGUMENT(long, ap, flags);
break;
case PrintfQualifierLongLong:
number = PRINTF_GET_INT_ARGUMENT(long long, ap, flags);
break;
case PrintfQualifierPointer:
precision = sizeof(void *) << 1;
number = (uint64_t) (uintptr_t) va_arg(ap, void *);
break;
default:
rc = EINVAL;
continue;
}
rc = _format_number(number, width, precision, base, flags, ps, &counter);
}
if (rc != EOK) {
_set_errno(rc);
return -1;
}
if (counter > INT_MAX) {
_set_errno(EOVERFLOW);
return -1;
}
return (int) counter;
}
HelenOS homepage, sources at GitHub