[4e2cf8b] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2006 Josef Cejka
|
---|
[f2b8cdc] | 4 | * Copyright (c) 2009 Martin Decky
|
---|
[97f6b71] | 5 | * Copyright (c) 2025 Jiří Zárevúcky
|
---|
[4e2cf8b] | 6 | * All rights reserved.
|
---|
| 7 | *
|
---|
| 8 | * Redistribution and use in source and binary forms, with or without
|
---|
| 9 | * modification, are permitted provided that the following conditions
|
---|
| 10 | * are met:
|
---|
| 11 | *
|
---|
| 12 | * - Redistributions of source code must retain the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer.
|
---|
| 14 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 15 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 16 | * documentation and/or other materials provided with the distribution.
|
---|
| 17 | * - The name of the author may not be used to endorse or promote products
|
---|
| 18 | * derived from this software without specific prior written permission.
|
---|
| 19 | *
|
---|
| 20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 22 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 23 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 30 | */
|
---|
| 31 |
|
---|
[174156fd] | 32 | /** @addtogroup libc
|
---|
[b2951e2] | 33 | * @{
|
---|
| 34 | */
|
---|
[0bc36ba] | 35 | /**
|
---|
[b2951e2] | 36 | * @file
|
---|
[f2b8cdc] | 37 | * @brief Printing functions.
|
---|
[0bc36ba] | 38 | */
|
---|
| 39 |
|
---|
[97f6b71] | 40 | #include <_bits/uchar.h>
|
---|
| 41 | #include <_bits/wint_t.h>
|
---|
| 42 | #include <assert.h>
|
---|
| 43 | #include <ctype.h>
|
---|
| 44 | #include <errno.h>
|
---|
| 45 | #include <limits.h>
|
---|
| 46 | #include <macros.h>
|
---|
| 47 | #include <printf_core.h>
|
---|
[582a0b8] | 48 | #include <stddef.h>
|
---|
[97f6b71] | 49 | #include <stdint.h>
|
---|
[7d7bc09] | 50 | #include <stdlib.h>
|
---|
[19f857a] | 51 | #include <str.h>
|
---|
[34b9299] | 52 |
|
---|
[694ca3d6] | 53 | /* Disable float support in kernel, because we usually disable floating operations there. */
|
---|
| 54 | #if __STDC_HOSTED__
|
---|
| 55 | #define HAS_FLOAT
|
---|
| 56 | #endif
|
---|
| 57 |
|
---|
| 58 | #ifdef HAS_FLOAT
|
---|
| 59 | #include <double_to_str.h>
|
---|
| 60 | #include <ieee_double.h>
|
---|
| 61 | #endif
|
---|
| 62 |
|
---|
[f2b8cdc] | 63 | /** show prefixes 0x or 0 */
|
---|
| 64 | #define __PRINTF_FLAG_PREFIX 0x00000001
|
---|
[a721f6a] | 65 |
|
---|
[34b9299] | 66 | /** show the decimal point even if no fractional digits present */
|
---|
| 67 | #define __PRINTF_FLAG_DECIMALPT 0x00000001
|
---|
| 68 |
|
---|
[f2b8cdc] | 69 | /** signed / unsigned number */
|
---|
| 70 | #define __PRINTF_FLAG_SIGNED 0x00000002
|
---|
[a721f6a] | 71 |
|
---|
[f2b8cdc] | 72 | /** print leading zeroes */
|
---|
| 73 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004
|
---|
[a721f6a] | 74 |
|
---|
[f2b8cdc] | 75 | /** align to left */
|
---|
| 76 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010
|
---|
[a721f6a] | 77 |
|
---|
[f2b8cdc] | 78 | /** always show + sign */
|
---|
| 79 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020
|
---|
[a721f6a] | 80 |
|
---|
[f2b8cdc] | 81 | /** print space instead of plus */
|
---|
| 82 | #define __PRINTF_FLAG_SPACESIGN 0x00000040
|
---|
[a721f6a] | 83 |
|
---|
[f2b8cdc] | 84 | /** show big characters */
|
---|
| 85 | #define __PRINTF_FLAG_BIGCHARS 0x00000080
|
---|
[a721f6a] | 86 |
|
---|
[f2b8cdc] | 87 | /** number has - sign */
|
---|
| 88 | #define __PRINTF_FLAG_NEGATIVE 0x00000100
|
---|
| 89 |
|
---|
[34b9299] | 90 | /** don't print trailing zeros in the fractional part */
|
---|
| 91 | #define __PRINTF_FLAG_NOFRACZEROS 0x00000200
|
---|
| 92 |
|
---|
[f2b8cdc] | 93 | /**
|
---|
[97f6b71] | 94 | * Buffer big enough for 64-bit number printed in base 2.
|
---|
[f2b8cdc] | 95 | */
|
---|
[97f6b71] | 96 | #define PRINT_NUMBER_BUFFER_SIZE 64
|
---|
[56972c81] | 97 |
|
---|
[7fadb65] | 98 | /** Get signed or unsigned integer argument */
|
---|
| 99 | #define PRINTF_GET_INT_ARGUMENT(type, ap, flags) \
|
---|
| 100 | ({ \
|
---|
| 101 | unsigned type res; \
|
---|
| 102 | \
|
---|
| 103 | if ((flags) & __PRINTF_FLAG_SIGNED) { \
|
---|
| 104 | signed type arg = va_arg((ap), signed type); \
|
---|
| 105 | \
|
---|
| 106 | if (arg < 0) { \
|
---|
| 107 | res = -arg; \
|
---|
| 108 | (flags) |= __PRINTF_FLAG_NEGATIVE; \
|
---|
| 109 | } else \
|
---|
| 110 | res = arg; \
|
---|
| 111 | } else \
|
---|
| 112 | res = va_arg((ap), unsigned type); \
|
---|
| 113 | \
|
---|
| 114 | res; \
|
---|
| 115 | })
|
---|
| 116 |
|
---|
[0bc36ba] | 117 | /** Enumeration of possible arguments types.
|
---|
| 118 | */
|
---|
[11a4fbf] | 119 | typedef enum {
|
---|
| 120 | PrintfQualifierByte = 0,
|
---|
| 121 | PrintfQualifierShort,
|
---|
| 122 | PrintfQualifierInt,
|
---|
| 123 | PrintfQualifierLong,
|
---|
| 124 | PrintfQualifierLongLong,
|
---|
[1e27d85] | 125 | PrintfQualifierPointer,
|
---|
[11a4fbf] | 126 | } qualifier_t;
|
---|
[4e2cf8b] | 127 |
|
---|
[97f6b71] | 128 | static const char _digits_small[] = "0123456789abcdef";
|
---|
| 129 | static const char _digits_big[] = "0123456789ABCDEF";
|
---|
[4e2cf8b] | 130 |
|
---|
[97f6b71] | 131 | static const char _nullstr[] = "(NULL)";
|
---|
| 132 | static const char _replacement[] = u8"�";
|
---|
| 133 | static const char _spaces[] = " ";
|
---|
| 134 | static const char _zeros[] = "000000000000000000000000000000000000000000000000";
|
---|
| 135 |
|
---|
| 136 | static void _set_errno(errno_t rc)
|
---|
[3214a20] | 137 | {
|
---|
[97f6b71] | 138 | #ifdef errno
|
---|
| 139 | errno = rc;
|
---|
| 140 | #endif
|
---|
[3214a20] | 141 | }
|
---|
| 142 |
|
---|
[97f6b71] | 143 | static size_t _utf8_bytes(char32_t c)
|
---|
[3214a20] | 144 | {
|
---|
[97f6b71] | 145 | if (c < 0x80)
|
---|
| 146 | return 1;
|
---|
| 147 |
|
---|
| 148 | if (c < 0x800)
|
---|
| 149 | return 2;
|
---|
| 150 |
|
---|
| 151 | if (c < 0xD800)
|
---|
| 152 | return 3;
|
---|
| 153 |
|
---|
| 154 | /* Surrogate code points, invalid in UTF-32. */
|
---|
| 155 | if (c < 0xE000)
|
---|
| 156 | return sizeof(_replacement) - 1;
|
---|
| 157 |
|
---|
| 158 | if (c < 0x10000)
|
---|
| 159 | return 3;
|
---|
| 160 |
|
---|
| 161 | if (c < 0x110000)
|
---|
| 162 | return 4;
|
---|
| 163 |
|
---|
| 164 | /* Invalid character. */
|
---|
| 165 | return sizeof(_replacement) - 1;
|
---|
[f2b8cdc] | 166 | }
|
---|
[3214a20] | 167 |
|
---|
[97f6b71] | 168 | /** Counts characters and utf8 bytes in a wide string up to a byte limit.
|
---|
| 169 | * @param max_bytes Byte length limit for string's utf8 conversion.
|
---|
| 170 | * @param[out] len The number of wide characters
|
---|
| 171 | * @return Number of utf8 bytes that the first *len characters in the string
|
---|
| 172 | * will convert to. Will always be less than max_bytes.
|
---|
[f2b8cdc] | 173 | */
|
---|
[97f6b71] | 174 | static size_t _utf8_wstr_bytes_len(char32_t *s, size_t max_bytes, size_t *len)
|
---|
[f2b8cdc] | 175 | {
|
---|
[97f6b71] | 176 | size_t bytes = 0;
|
---|
| 177 | size_t i;
|
---|
| 178 |
|
---|
| 179 | for (i = 0; bytes < max_bytes && s[i]; i++) {
|
---|
| 180 | size_t next = _utf8_bytes(s[i]);
|
---|
| 181 | if (max_bytes - bytes < next)
|
---|
| 182 | break;
|
---|
[a35b458] | 183 |
|
---|
[97f6b71] | 184 | bytes += next;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | *len = i;
|
---|
| 188 | return bytes;
|
---|
[3214a20] | 189 | }
|
---|
| 190 |
|
---|
[97f6b71] | 191 | #define TRY(expr) ({ errno_t rc = (expr); if (rc != EOK) return rc; })
|
---|
| 192 |
|
---|
| 193 | static inline void _saturating_add(size_t *a, size_t b)
|
---|
[3214a20] | 194 | {
|
---|
[97f6b71] | 195 | size_t s = *a + b;
|
---|
| 196 | /* Only works because size_t is unsigned. */
|
---|
| 197 | *a = (s < b) ? SIZE_MAX : s;
|
---|
| 198 | }
|
---|
[a35b458] | 199 |
|
---|
[97f6b71] | 200 | static errno_t _write_bytes(const char *buf, size_t n, printf_spec_t *ps,
|
---|
| 201 | size_t *written_bytes)
|
---|
| 202 | {
|
---|
[163e34c] | 203 | errno_t rc = ps->write(buf, n, ps->data);
|
---|
| 204 | if (rc != EOK)
|
---|
| 205 | return rc;
|
---|
[97f6b71] | 206 |
|
---|
| 207 | _saturating_add(written_bytes, n);
|
---|
[163e34c] | 208 | return EOK;
|
---|
[3214a20] | 209 | }
|
---|
[c9857c6] | 210 |
|
---|
[97f6b71] | 211 | /** Write one UTF-32 character. */
|
---|
| 212 | static errno_t _write_uchar(char32_t ch, printf_spec_t *ps,
|
---|
| 213 | size_t *written_bytes)
|
---|
| 214 | {
|
---|
| 215 | char utf8[4];
|
---|
| 216 | size_t offset = 0;
|
---|
| 217 |
|
---|
| 218 | if (chr_encode(ch, utf8, &offset, sizeof(utf8)) == EOK)
|
---|
| 219 | return _write_bytes(utf8, offset, ps, written_bytes);
|
---|
| 220 |
|
---|
| 221 | /* Invalid character. */
|
---|
| 222 | return _write_bytes(_replacement, sizeof(_replacement) - 1, ps, written_bytes);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | /** Write n UTF-32 characters. */
|
---|
| 226 | static errno_t _write_chars(const char32_t *buf, size_t n, printf_spec_t *ps,
|
---|
| 227 | size_t *written_bytes)
|
---|
[c9857c6] | 228 | {
|
---|
[97f6b71] | 229 | for (size_t i = 0; i < n; i++)
|
---|
| 230 | TRY(_write_uchar(buf[i], ps, written_bytes));
|
---|
[a35b458] | 231 |
|
---|
[97f6b71] | 232 | return EOK;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | static errno_t _write_char(char c, printf_spec_t *ps, size_t *written_bytes)
|
---|
| 236 | {
|
---|
| 237 | return _write_bytes(&c, 1, ps, written_bytes);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | static errno_t _write_spaces(size_t n, printf_spec_t *ps, size_t *written_bytes)
|
---|
| 241 | {
|
---|
| 242 | size_t max_spaces = sizeof(_spaces) - 1;
|
---|
| 243 |
|
---|
| 244 | while (n > max_spaces) {
|
---|
| 245 | TRY(_write_bytes(_spaces, max_spaces, ps, written_bytes));
|
---|
| 246 | n -= max_spaces;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | return _write_bytes(_spaces, n, ps, written_bytes);
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | static errno_t _write_zeros(size_t n, printf_spec_t *ps, size_t *written_bytes)
|
---|
| 253 | {
|
---|
| 254 | size_t max_zeros = sizeof(_zeros) - 1;
|
---|
| 255 |
|
---|
| 256 | while (n > max_zeros) {
|
---|
| 257 | TRY(_write_bytes(_zeros, max_zeros, ps, written_bytes));
|
---|
| 258 | n -= max_zeros;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | return _write_bytes(_zeros, n, ps, written_bytes);
|
---|
[f2b8cdc] | 262 | }
|
---|
| 263 |
|
---|
| 264 | /** Print one formatted ASCII character.
|
---|
| 265 | *
|
---|
| 266 | * @param ch Character to print.
|
---|
| 267 | * @param width Width modifier.
|
---|
| 268 | * @param flags Flags that change the way the character is printed.
|
---|
| 269 | */
|
---|
[97f6b71] | 270 | static errno_t _format_char(const char c, size_t width, uint32_t flags,
|
---|
| 271 | printf_spec_t *ps, size_t *written_bytes)
|
---|
[f2b8cdc] | 272 | {
|
---|
[97f6b71] | 273 | size_t bytes = 1;
|
---|
[a35b458] | 274 |
|
---|
[97f6b71] | 275 | if (width <= bytes)
|
---|
| 276 | return _write_char(c, ps, written_bytes);
|
---|
[a35b458] | 277 |
|
---|
[97f6b71] | 278 | if (flags & __PRINTF_FLAG_LEFTALIGNED) {
|
---|
| 279 | TRY(_write_char(c, ps, written_bytes));
|
---|
| 280 | TRY(_write_spaces(width - bytes, ps, written_bytes));
|
---|
| 281 | } else {
|
---|
| 282 | TRY(_write_spaces(width - bytes, ps, written_bytes));
|
---|
| 283 | TRY(_write_char(c, ps, written_bytes));
|
---|
[f2b8cdc] | 284 | }
|
---|
[a35b458] | 285 |
|
---|
[97f6b71] | 286 | return EOK;
|
---|
[f2b8cdc] | 287 | }
|
---|
| 288 |
|
---|
| 289 | /** Print one formatted wide character.
|
---|
| 290 | *
|
---|
| 291 | * @param ch Character to print.
|
---|
| 292 | * @param width Width modifier.
|
---|
| 293 | * @param flags Flags that change the way the character is printed.
|
---|
| 294 | */
|
---|
[97f6b71] | 295 | static errno_t _format_uchar(const char32_t ch, size_t width, uint32_t flags,
|
---|
| 296 | printf_spec_t *ps, size_t *written_bytes)
|
---|
[f2b8cdc] | 297 | {
|
---|
[97f6b71] | 298 | /*
|
---|
| 299 | * All widths in printf() are specified in bytes. It might seem nonsensical
|
---|
| 300 | * with unicode text, but that's the way the function is defined. The width
|
---|
| 301 | * is barely useful if you want column alignment in terminal, but keep in
|
---|
| 302 | * mind that counting code points is only marginally better for that.
|
---|
| 303 | * Characters can span more than one unicode code point, even in languages
|
---|
| 304 | * based on latin alphabet, and a single unicode code point can occupy two
|
---|
| 305 | * spaces in east asian scripts.
|
---|
| 306 | *
|
---|
| 307 | * What the width can actually be useful for is padding, when you need the
|
---|
| 308 | * output to fill an exact number of bytes in a file. That use would break
|
---|
| 309 | * if we did our own thing here.
|
---|
| 310 | */
|
---|
| 311 |
|
---|
| 312 | size_t bytes = _utf8_bytes(ch);
|
---|
| 313 |
|
---|
| 314 | if (width <= bytes)
|
---|
| 315 | return _write_uchar(ch, ps, written_bytes);
|
---|
[a35b458] | 316 |
|
---|
[97f6b71] | 317 | if (flags & __PRINTF_FLAG_LEFTALIGNED) {
|
---|
| 318 | TRY(_write_uchar(ch, ps, written_bytes));
|
---|
| 319 | TRY(_write_spaces(width - bytes, ps, written_bytes));
|
---|
| 320 | } else {
|
---|
| 321 | TRY(_write_spaces(width - bytes, ps, written_bytes));
|
---|
| 322 | TRY(_write_uchar(ch, ps, written_bytes));
|
---|
[c9857c6] | 323 | }
|
---|
[a35b458] | 324 |
|
---|
[97f6b71] | 325 | return EOK;
|
---|
[c9857c6] | 326 | }
|
---|
| 327 |
|
---|
[f2b8cdc] | 328 | /** Print string.
|
---|
| 329 | *
|
---|
| 330 | * @param str String to be printed.
|
---|
| 331 | * @param width Width modifier.
|
---|
| 332 | * @param precision Precision modifier.
|
---|
| 333 | * @param flags Flags that modify the way the string is printed.
|
---|
[c9857c6] | 334 | */
|
---|
[97f6b71] | 335 | static errno_t _format_cstr(const char *str, size_t width, int precision,
|
---|
| 336 | uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
|
---|
[c9857c6] | 337 | {
|
---|
[f2b8cdc] | 338 | if (str == NULL)
|
---|
[97f6b71] | 339 | str = _nullstr;
|
---|
[a35b458] | 340 |
|
---|
[97f6b71] | 341 | /* Negative precision == unspecified. */
|
---|
| 342 | size_t max_bytes = (precision < 0) ? SIZE_MAX : (size_t) precision;
|
---|
| 343 | size_t bytes = str_nsize(str, max_bytes);
|
---|
[34b9299] | 344 |
|
---|
[97f6b71] | 345 | if (width <= bytes)
|
---|
| 346 | return _write_bytes(str, bytes, ps, written_bytes);
|
---|
[a35b458] | 347 |
|
---|
[97f6b71] | 348 | if (flags & __PRINTF_FLAG_LEFTALIGNED) {
|
---|
| 349 | TRY(_write_bytes(str, bytes, ps, written_bytes));
|
---|
| 350 | TRY(_write_spaces(width - bytes, ps, written_bytes));
|
---|
| 351 | } else {
|
---|
| 352 | TRY(_write_spaces(width - bytes, ps, written_bytes));
|
---|
| 353 | TRY(_write_bytes(str, bytes, ps, written_bytes));
|
---|
[c9857c6] | 354 | }
|
---|
| 355 |
|
---|
[97f6b71] | 356 | return EOK;
|
---|
[f2b8cdc] | 357 | }
|
---|
[c9857c6] | 358 |
|
---|
[f2b8cdc] | 359 | /** Print wide string.
|
---|
| 360 | *
|
---|
| 361 | * @param str Wide string to be printed.
|
---|
| 362 | * @param width Width modifier.
|
---|
| 363 | * @param precision Precision modifier.
|
---|
| 364 | * @param flags Flags that modify the way the string is printed.
|
---|
| 365 | */
|
---|
[97f6b71] | 366 | static errno_t _format_wstr(char32_t *str, size_t width, int precision,
|
---|
| 367 | uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
|
---|
[f2b8cdc] | 368 | {
|
---|
[97f6b71] | 369 | if (!str)
|
---|
| 370 | return _format_cstr(_nullstr, width, precision, flags, ps, written_bytes);
|
---|
[a35b458] | 371 |
|
---|
[97f6b71] | 372 | /* Width and precision are always byte-based. See _format_uchar() */
|
---|
| 373 | /* Negative precision == unspecified. */
|
---|
| 374 | size_t max_bytes = (precision < 0) ? SIZE_MAX : (size_t) precision;
|
---|
[34b9299] | 375 |
|
---|
[97f6b71] | 376 | size_t len;
|
---|
| 377 | size_t bytes = _utf8_wstr_bytes_len(str, max_bytes, &len);
|
---|
[a35b458] | 378 |
|
---|
[97f6b71] | 379 | if (width <= bytes)
|
---|
| 380 | return _write_chars(str, len, ps, written_bytes);
|
---|
| 381 |
|
---|
| 382 | if (flags & __PRINTF_FLAG_LEFTALIGNED) {
|
---|
| 383 | TRY(_write_chars(str, len, ps, written_bytes));
|
---|
| 384 | TRY(_write_spaces(width - bytes, ps, written_bytes));
|
---|
| 385 | } else {
|
---|
| 386 | TRY(_write_spaces(width - bytes, ps, written_bytes));
|
---|
| 387 | TRY(_write_chars(str, len, ps, written_bytes));
|
---|
[c9857c6] | 388 | }
|
---|
[a35b458] | 389 |
|
---|
[97f6b71] | 390 | return EOK;
|
---|
| 391 | }
|
---|
[a35b458] | 392 |
|
---|
[97f6b71] | 393 | static char _sign(uint32_t flags)
|
---|
| 394 | {
|
---|
| 395 | if (!(flags & __PRINTF_FLAG_SIGNED))
|
---|
| 396 | return 0;
|
---|
[a35b458] | 397 |
|
---|
[97f6b71] | 398 | if (flags & __PRINTF_FLAG_NEGATIVE)
|
---|
| 399 | return '-';
|
---|
[c9857c6] | 400 |
|
---|
[97f6b71] | 401 | if (flags & __PRINTF_FLAG_SHOWPLUS)
|
---|
| 402 | return '+';
|
---|
| 403 |
|
---|
| 404 | if (flags & __PRINTF_FLAG_SPACESIGN)
|
---|
| 405 | return ' ';
|
---|
| 406 |
|
---|
| 407 | return 0;
|
---|
[f2b8cdc] | 408 | }
|
---|
[c9857c6] | 409 |
|
---|
[f2b8cdc] | 410 | /** Print a number in a given base.
|
---|
[4e2cf8b] | 411 | *
|
---|
[f2b8cdc] | 412 | * Print significant digits of a number in given base.
|
---|
[4e2cf8b] | 413 | *
|
---|
[f2b8cdc] | 414 | * @param num Number to print.
|
---|
| 415 | * @param width Width modifier.
|
---|
| 416 | * @param precision Precision modifier.
|
---|
| 417 | * @param base Base to print the number in (must be between 2 and 16).
|
---|
| 418 | * @param flags Flags that modify the way the number is printed.
|
---|
[4e2cf8b] | 419 | */
|
---|
[97f6b71] | 420 | static errno_t _format_number(uint64_t num, size_t width, int precision, int base,
|
---|
| 421 | uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
|
---|
[4e2cf8b] | 422 | {
|
---|
[97f6b71] | 423 | assert(base >= 2 && base <= 16);
|
---|
[a35b458] | 424 |
|
---|
[97f6b71] | 425 | /* Default precision for numeric output is 1. */
|
---|
| 426 | size_t min_digits = (precision < 0) ? 1 : precision;
|
---|
[a35b458] | 427 |
|
---|
[97f6b71] | 428 | bool bigchars = flags & __PRINTF_FLAG_BIGCHARS;
|
---|
| 429 | bool prefix = flags & __PRINTF_FLAG_PREFIX;
|
---|
| 430 | bool left_aligned = flags & __PRINTF_FLAG_LEFTALIGNED;
|
---|
| 431 | bool zero_padded = flags & __PRINTF_FLAG_ZEROPADDED;
|
---|
[a35b458] | 432 |
|
---|
[97f6b71] | 433 | const char *digits = bigchars ? _digits_big : _digits_small;
|
---|
[a35b458] | 434 |
|
---|
[97f6b71] | 435 | char buffer[PRINT_NUMBER_BUFFER_SIZE];
|
---|
| 436 | char *end = &buffer[PRINT_NUMBER_BUFFER_SIZE];
|
---|
[a35b458] | 437 |
|
---|
[97f6b71] | 438 | /* Write number to the buffer. */
|
---|
| 439 | int offset = 0;
|
---|
| 440 | while (num > 0) {
|
---|
| 441 | end[--offset] = digits[num % base];
|
---|
| 442 | num /= base;
|
---|
[11a4fbf] | 443 | }
|
---|
[a35b458] | 444 |
|
---|
[97f6b71] | 445 | char *number = &end[offset];
|
---|
| 446 | size_t number_len = end - number;
|
---|
| 447 | char sign = _sign(flags);
|
---|
[a35b458] | 448 |
|
---|
[97f6b71] | 449 | if (left_aligned) {
|
---|
| 450 | /* Space padded right-aligned. */
|
---|
| 451 | size_t real_size = max(number_len, min_digits);
|
---|
| 452 |
|
---|
| 453 | if (sign) {
|
---|
| 454 | TRY(_write_char(sign, ps, written_bytes));
|
---|
| 455 | real_size++;
|
---|
[11a4fbf] | 456 | }
|
---|
[a35b458] | 457 |
|
---|
[97f6b71] | 458 | if (prefix && base == 2 && number_len > 0) {
|
---|
| 459 | TRY(_write_bytes(bigchars ? "0B" : "0b", 2, ps, written_bytes));
|
---|
| 460 | real_size += 2;
|
---|
[1c38445] | 461 | }
|
---|
[a35b458] | 462 |
|
---|
[97f6b71] | 463 | if (prefix && base == 16 && number_len > 0) {
|
---|
| 464 | TRY(_write_bytes(bigchars ? "0X" : "0x", 2, ps, written_bytes));
|
---|
| 465 | real_size += 2;
|
---|
| 466 | }
|
---|
[a35b458] | 467 |
|
---|
[97f6b71] | 468 | if (min_digits > number_len) {
|
---|
| 469 | TRY(_write_zeros(min_digits - number_len, ps, written_bytes));
|
---|
| 470 | } else if (prefix && base == 8) {
|
---|
| 471 | TRY(_write_zeros(1, ps, written_bytes));
|
---|
| 472 | real_size++;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | TRY(_write_bytes(number, number_len, ps, written_bytes));
|
---|
| 476 |
|
---|
| 477 | if (width > real_size)
|
---|
| 478 | TRY(_write_spaces(width - real_size, ps, written_bytes));
|
---|
[a35b458] | 479 |
|
---|
[97f6b71] | 480 | return EOK;
|
---|
[f2b8cdc] | 481 | }
|
---|
[a35b458] | 482 |
|
---|
[97f6b71] | 483 | /* Zero padded number (ignored when left aligned or if precision is specified). */
|
---|
| 484 | if (precision < 0 && zero_padded) {
|
---|
| 485 | size_t real_size = number_len;
|
---|
[a35b458] | 486 |
|
---|
[97f6b71] | 487 | if (sign) {
|
---|
| 488 | TRY(_write_char(sign, ps, written_bytes));
|
---|
| 489 | real_size++;
|
---|
[c9857c6] | 490 | }
|
---|
[a35b458] | 491 |
|
---|
[97f6b71] | 492 | if (prefix && base == 2 && number_len > 0) {
|
---|
| 493 | TRY(_write_bytes(bigchars ? "0B" : "0b", 2, ps, written_bytes));
|
---|
| 494 | real_size += 2;
|
---|
| 495 | }
|
---|
[a35b458] | 496 |
|
---|
[97f6b71] | 497 | if (prefix && base == 16 && number_len > 0) {
|
---|
| 498 | TRY(_write_bytes(bigchars ? "0X" : "0x", 2, ps, written_bytes));
|
---|
| 499 | real_size += 2;
|
---|
[c9857c6] | 500 | }
|
---|
[a35b458] | 501 |
|
---|
[97f6b71] | 502 | if (width > real_size)
|
---|
| 503 | TRY(_write_zeros(width - real_size, ps, written_bytes));
|
---|
| 504 | else if (number_len == 0 || (prefix && base == 8))
|
---|
| 505 | TRY(_write_char('0', ps, written_bytes));
|
---|
| 506 |
|
---|
| 507 | return _write_bytes(number, number_len, ps, written_bytes);
|
---|
[c9857c6] | 508 | }
|
---|
[a35b458] | 509 |
|
---|
[97f6b71] | 510 | /* Space padded right-aligned. */
|
---|
| 511 | size_t real_size = max(number_len, min_digits);
|
---|
| 512 | if (sign)
|
---|
| 513 | real_size++;
|
---|
[a35b458] | 514 |
|
---|
[97f6b71] | 515 | if (prefix && (base == 2 || base == 16) && number_len > 0)
|
---|
| 516 | real_size += 2;
|
---|
[a35b458] | 517 |
|
---|
[97f6b71] | 518 | if (prefix && base == 8 && number_len >= min_digits)
|
---|
| 519 | real_size += 1;
|
---|
| 520 |
|
---|
| 521 | if (width > real_size)
|
---|
| 522 | TRY(_write_spaces(width - real_size, ps, written_bytes));
|
---|
| 523 |
|
---|
| 524 | if (sign)
|
---|
| 525 | TRY(_write_char(sign, ps, written_bytes));
|
---|
[a35b458] | 526 |
|
---|
[97f6b71] | 527 | if (prefix && base == 2 && number_len > 0)
|
---|
| 528 | TRY(_write_bytes(bigchars ? "0B" : "0b", 2, ps, written_bytes));
|
---|
| 529 |
|
---|
| 530 | if (prefix && base == 16 && number_len > 0)
|
---|
| 531 | TRY(_write_bytes(bigchars ? "0X" : "0x", 2, ps, written_bytes));
|
---|
| 532 |
|
---|
| 533 | if (min_digits > number_len)
|
---|
| 534 | TRY(_write_zeros(min_digits - number_len, ps, written_bytes));
|
---|
| 535 | else if (prefix && base == 8)
|
---|
| 536 | TRY(_write_char('0', ps, written_bytes));
|
---|
| 537 |
|
---|
| 538 | return _write_bytes(number, number_len, ps, written_bytes);
|
---|
[4e2cf8b] | 539 | }
|
---|
| 540 |
|
---|
[694ca3d6] | 541 | #ifdef HAS_FLOAT
|
---|
| 542 |
|
---|
| 543 | /** Unformatted double number string representation. */
|
---|
| 544 | typedef struct {
|
---|
| 545 | /** Buffer with len digits, no sign or leading zeros. */
|
---|
| 546 | char *str;
|
---|
| 547 | /** Number of digits in str. */
|
---|
| 548 | int len;
|
---|
| 549 | /** Decimal exponent, ie number = str * 10^dec_exp */
|
---|
| 550 | int dec_exp;
|
---|
| 551 | /** True if negative. */
|
---|
| 552 | bool neg;
|
---|
| 553 | } double_str_t;
|
---|
| 554 |
|
---|
| 555 | /** Returns the sign character or 0 if no sign should be printed. */
|
---|
[97f6b71] | 556 | static char _get_sign_char(bool negative, uint32_t flags)
|
---|
[694ca3d6] | 557 | {
|
---|
| 558 | if (negative) {
|
---|
| 559 | return '-';
|
---|
| 560 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
|
---|
| 561 | return '+';
|
---|
| 562 | } else if (flags & __PRINTF_FLAG_SPACESIGN) {
|
---|
| 563 | return ' ';
|
---|
| 564 | } else {
|
---|
| 565 | return 0;
|
---|
| 566 | }
|
---|
| 567 | }
|
---|
| 568 |
|
---|
[34b9299] | 569 | /** Prints a special double (ie NaN, infinity) padded to width characters. */
|
---|
[97f6b71] | 570 | static errno_t _format_special(ieee_double_t val, int width, uint32_t flags,
|
---|
| 571 | printf_spec_t *ps, size_t *written_bytes)
|
---|
[34b9299] | 572 | {
|
---|
| 573 | assert(val.is_special);
|
---|
| 574 |
|
---|
[97f6b71] | 575 | char sign = _get_sign_char(val.is_negative, flags);
|
---|
[34b9299] | 576 |
|
---|
| 577 | const int str_len = 3;
|
---|
| 578 | const char *str;
|
---|
[a35b458] | 579 |
|
---|
[34b9299] | 580 | if (flags & __PRINTF_FLAG_BIGCHARS) {
|
---|
| 581 | str = val.is_infinity ? "INF" : "NAN";
|
---|
| 582 | } else {
|
---|
| 583 | str = val.is_infinity ? "inf" : "nan";
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | int padding_len = max(0, width - ((sign ? 1 : 0) + str_len));
|
---|
| 587 |
|
---|
| 588 | /* Leading padding. */
|
---|
[97f6b71] | 589 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED))
|
---|
| 590 | TRY(_write_spaces(padding_len, ps, written_bytes));
|
---|
[34b9299] | 591 |
|
---|
[97f6b71] | 592 | if (sign)
|
---|
| 593 | TRY(_write_char(sign, ps, written_bytes));
|
---|
[a35b458] | 594 |
|
---|
[97f6b71] | 595 | TRY(_write_bytes(str, str_len, ps, written_bytes));
|
---|
[34b9299] | 596 |
|
---|
| 597 | /* Trailing padding. */
|
---|
[97f6b71] | 598 | if (flags & __PRINTF_FLAG_LEFTALIGNED)
|
---|
| 599 | TRY(_write_spaces(padding_len, ps, written_bytes));
|
---|
[34b9299] | 600 |
|
---|
[97f6b71] | 601 | return EOK;
|
---|
[34b9299] | 602 | }
|
---|
| 603 |
|
---|
| 604 | /** Trims trailing zeros but leaves a single "0" intact. */
|
---|
[97f6b71] | 605 | static void _fp_trim_trailing_zeros(char *buf, int *len, int *dec_exp)
|
---|
[34b9299] | 606 | {
|
---|
| 607 | /* Cut the zero off by adjusting the exponent. */
|
---|
| 608 | while (2 <= *len && '0' == buf[*len - 1]) {
|
---|
| 609 | --*len;
|
---|
| 610 | ++*dec_exp;
|
---|
| 611 | }
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | /** Textually round up the last digit thereby eliminating it. */
|
---|
[97f6b71] | 615 | static void _fp_round_up(char *buf, int *len, int *dec_exp)
|
---|
[34b9299] | 616 | {
|
---|
| 617 | assert(1 <= *len);
|
---|
| 618 |
|
---|
| 619 | char *last_digit = &buf[*len - 1];
|
---|
| 620 |
|
---|
| 621 | int carry = ('5' <= *last_digit);
|
---|
| 622 |
|
---|
| 623 | /* Cut the digit off by adjusting the exponent. */
|
---|
| 624 | --*len;
|
---|
| 625 | ++*dec_exp;
|
---|
| 626 | --last_digit;
|
---|
| 627 |
|
---|
| 628 | if (carry) {
|
---|
| 629 | /* Skip all the digits to cut off/round to zero. */
|
---|
| 630 | while (buf <= last_digit && '9' == *last_digit) {
|
---|
| 631 | --last_digit;
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | /* last_digit points to the last digit to round but not '9' */
|
---|
| 635 | if (buf <= last_digit) {
|
---|
| 636 | *last_digit += 1;
|
---|
| 637 | int new_len = last_digit - buf + 1;
|
---|
| 638 | *dec_exp += *len - new_len;
|
---|
| 639 | *len = new_len;
|
---|
| 640 | } else {
|
---|
| 641 | /* All len digits rounded to 0. */
|
---|
| 642 | buf[0] = '1';
|
---|
| 643 | *dec_exp += *len;
|
---|
| 644 | *len = 1;
|
---|
| 645 | }
|
---|
| 646 | } else {
|
---|
| 647 | /* The only digit was rounded to 0. */
|
---|
| 648 | if (last_digit < buf) {
|
---|
| 649 | buf[0] = '0';
|
---|
| 650 | *dec_exp = 0;
|
---|
| 651 | *len = 1;
|
---|
| 652 | }
|
---|
| 653 | }
|
---|
| 654 | }
|
---|
| 655 |
|
---|
[1b20da0] | 656 | /** Format and print the double string repressentation according
|
---|
| 657 | * to the %f specifier.
|
---|
[34b9299] | 658 | */
|
---|
[97f6b71] | 659 | static errno_t _format_double_str_fixed(double_str_t *val_str, int precision, int width,
|
---|
| 660 | uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
|
---|
[34b9299] | 661 | {
|
---|
| 662 | int len = val_str->len;
|
---|
| 663 | char *buf = val_str->str;
|
---|
| 664 | int dec_exp = val_str->dec_exp;
|
---|
| 665 |
|
---|
| 666 | assert(0 < len);
|
---|
| 667 | assert(0 <= precision);
|
---|
| 668 | assert(0 <= dec_exp || -dec_exp <= precision);
|
---|
| 669 |
|
---|
| 670 | /* Number of integral digits to print (at least leading zero). */
|
---|
| 671 | int int_len = max(1, len + dec_exp);
|
---|
| 672 |
|
---|
[97f6b71] | 673 | char sign = _get_sign_char(val_str->neg, flags);
|
---|
[34b9299] | 674 |
|
---|
| 675 | /* Fractional portion lengths. */
|
---|
| 676 | int last_frac_signif_pos = max(0, -dec_exp);
|
---|
| 677 | int leading_frac_zeros = max(0, last_frac_signif_pos - len);
|
---|
| 678 | int signif_frac_figs = min(last_frac_signif_pos, len);
|
---|
| 679 | int trailing_frac_zeros = precision - last_frac_signif_pos;
|
---|
| 680 | char *buf_frac = buf + len - signif_frac_figs;
|
---|
| 681 |
|
---|
[97f6b71] | 682 | if (flags & __PRINTF_FLAG_NOFRACZEROS)
|
---|
[34b9299] | 683 | trailing_frac_zeros = 0;
|
---|
| 684 |
|
---|
| 685 | int frac_len = leading_frac_zeros + signif_frac_figs + trailing_frac_zeros;
|
---|
| 686 |
|
---|
| 687 | bool has_decimal_pt = (0 < frac_len) || (flags & __PRINTF_FLAG_DECIMALPT);
|
---|
| 688 |
|
---|
| 689 | /* Number of non-padding chars to print. */
|
---|
| 690 | int num_len = (sign ? 1 : 0) + int_len + (has_decimal_pt ? 1 : 0) + frac_len;
|
---|
| 691 |
|
---|
| 692 | int padding_len = max(0, width - num_len);
|
---|
| 693 |
|
---|
| 694 | /* Leading padding and sign. */
|
---|
| 695 |
|
---|
[97f6b71] | 696 | if (!(flags & (__PRINTF_FLAG_LEFTALIGNED | __PRINTF_FLAG_ZEROPADDED)))
|
---|
| 697 | TRY(_write_spaces(padding_len, ps, written_bytes));
|
---|
[34b9299] | 698 |
|
---|
[97f6b71] | 699 | if (sign)
|
---|
| 700 | TRY(_write_char(sign, ps, written_bytes));
|
---|
[a35b458] | 701 |
|
---|
[97f6b71] | 702 | if (flags & __PRINTF_FLAG_ZEROPADDED)
|
---|
| 703 | TRY(_write_zeros(padding_len, ps, written_bytes));
|
---|
[34b9299] | 704 |
|
---|
| 705 | /* Print the intergral part of the buffer. */
|
---|
| 706 |
|
---|
| 707 | int buf_int_len = min(len, len + dec_exp);
|
---|
| 708 |
|
---|
| 709 | if (0 < buf_int_len) {
|
---|
[97f6b71] | 710 | TRY(_write_bytes(buf, buf_int_len, ps, written_bytes));
|
---|
[34b9299] | 711 |
|
---|
| 712 | /* Print trailing zeros of the integral part of the number. */
|
---|
[97f6b71] | 713 | TRY(_write_zeros(int_len - buf_int_len, ps, written_bytes));
|
---|
[34b9299] | 714 | } else {
|
---|
| 715 | /* Single leading integer 0. */
|
---|
[97f6b71] | 716 | TRY(_write_char('0', ps, written_bytes));
|
---|
[34b9299] | 717 | }
|
---|
| 718 |
|
---|
| 719 | /* Print the decimal point and the fractional part. */
|
---|
| 720 | if (has_decimal_pt) {
|
---|
[97f6b71] | 721 | TRY(_write_char('.', ps, written_bytes));
|
---|
[34b9299] | 722 |
|
---|
| 723 | /* Print leading zeros of the fractional part of the number. */
|
---|
[97f6b71] | 724 | TRY(_write_zeros(leading_frac_zeros, ps, written_bytes));
|
---|
[34b9299] | 725 |
|
---|
| 726 | /* Print significant digits of the fractional part of the number. */
|
---|
[97f6b71] | 727 | if (0 < signif_frac_figs)
|
---|
| 728 | TRY(_write_bytes(buf_frac, signif_frac_figs, ps, written_bytes));
|
---|
[34b9299] | 729 |
|
---|
| 730 | /* Print trailing zeros of the fractional part of the number. */
|
---|
[97f6b71] | 731 | TRY(_write_zeros(trailing_frac_zeros, ps, written_bytes));
|
---|
[34b9299] | 732 | }
|
---|
| 733 |
|
---|
| 734 | /* Trailing padding. */
|
---|
[97f6b71] | 735 | if (flags & __PRINTF_FLAG_LEFTALIGNED)
|
---|
| 736 | TRY(_write_spaces(padding_len, ps, written_bytes));
|
---|
[34b9299] | 737 |
|
---|
[97f6b71] | 738 | return EOK;
|
---|
[34b9299] | 739 | }
|
---|
| 740 |
|
---|
[1b20da0] | 741 | /** Convert, format and print a double according to the %f specifier.
|
---|
[34b9299] | 742 | *
|
---|
| 743 | * @param g Double to print.
|
---|
| 744 | * @param precision Number of fractional digits to print. If 0 no
|
---|
| 745 | * decimal point will be printed unless the flag
|
---|
| 746 | * __PRINTF_FLAG_DECIMALPT is specified.
|
---|
[1b20da0] | 747 | * @param width Minimum number of characters to display. Pads
|
---|
[34b9299] | 748 | * with '0' or ' ' depending on the set flags;
|
---|
| 749 | * @param flags Printf flags.
|
---|
| 750 | * @param ps Printing functions.
|
---|
| 751 | */
|
---|
[97f6b71] | 752 | static errno_t _format_double_fixed(double g, int precision, int width,
|
---|
| 753 | uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
|
---|
[34b9299] | 754 | {
|
---|
| 755 | if (flags & __PRINTF_FLAG_LEFTALIGNED) {
|
---|
| 756 | flags &= ~__PRINTF_FLAG_ZEROPADDED;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
| 759 | if (flags & __PRINTF_FLAG_DECIMALPT) {
|
---|
| 760 | flags &= ~__PRINTF_FLAG_NOFRACZEROS;
|
---|
| 761 | }
|
---|
| 762 |
|
---|
| 763 | ieee_double_t val = extract_ieee_double(g);
|
---|
| 764 |
|
---|
| 765 | if (val.is_special) {
|
---|
[97f6b71] | 766 | return _format_special(val, width, flags, ps, written_bytes);
|
---|
[1b20da0] | 767 | }
|
---|
[34b9299] | 768 |
|
---|
| 769 | char buf[MAX_DOUBLE_STR_BUF_SIZE];
|
---|
| 770 | const size_t buf_size = MAX_DOUBLE_STR_BUF_SIZE;
|
---|
| 771 | double_str_t val_str;
|
---|
| 772 |
|
---|
| 773 | val_str.str = buf;
|
---|
| 774 | val_str.neg = val.is_negative;
|
---|
| 775 |
|
---|
| 776 | if (0 <= precision) {
|
---|
[1b20da0] | 777 | /*
|
---|
[34b9299] | 778 | * Request one more digit so we can round the result. The last
|
---|
[1b20da0] | 779 | * digit it returns may have an error of at most +/- 1.
|
---|
[34b9299] | 780 | */
|
---|
[1b20da0] | 781 | val_str.len = double_to_fixed_str(val, -1, precision + 1, buf, buf_size,
|
---|
[1433ecda] | 782 | &val_str.dec_exp);
|
---|
[34b9299] | 783 |
|
---|
[1b20da0] | 784 | /*
|
---|
| 785 | * Round using the last digit to produce precision fractional digits.
|
---|
| 786 | * If less than precision+1 fractional digits were output the last
|
---|
| 787 | * digit is definitely inaccurate so also round to get rid of it.
|
---|
[34b9299] | 788 | */
|
---|
[97f6b71] | 789 | _fp_round_up(buf, &val_str.len, &val_str.dec_exp);
|
---|
[34b9299] | 790 |
|
---|
| 791 | /* Rounding could have introduced trailing zeros. */
|
---|
| 792 | if (flags & __PRINTF_FLAG_NOFRACZEROS) {
|
---|
[97f6b71] | 793 | _fp_trim_trailing_zeros(buf, &val_str.len, &val_str.dec_exp);
|
---|
[34b9299] | 794 | }
|
---|
| 795 | } else {
|
---|
| 796 | /* Let the implementation figure out the proper precision. */
|
---|
| 797 | val_str.len = double_to_short_str(val, buf, buf_size, &val_str.dec_exp);
|
---|
[a35b458] | 798 |
|
---|
[34b9299] | 799 | /* Precision needed for the last significant digit. */
|
---|
| 800 | precision = max(0, -val_str.dec_exp);
|
---|
| 801 | }
|
---|
| 802 |
|
---|
[97f6b71] | 803 | return _format_double_str_fixed(&val_str, precision, width, flags, ps, written_bytes);
|
---|
[34b9299] | 804 | }
|
---|
| 805 |
|
---|
| 806 | /** Prints the decimal exponent part of a %e specifier formatted number. */
|
---|
[97f6b71] | 807 | static errno_t _format_exponent(int exp_val, uint32_t flags, printf_spec_t *ps,
|
---|
| 808 | size_t *written_bytes)
|
---|
[34b9299] | 809 | {
|
---|
| 810 | char exp_ch = (flags & __PRINTF_FLAG_BIGCHARS) ? 'E' : 'e';
|
---|
[97f6b71] | 811 | TRY(_write_char(exp_ch, ps, written_bytes));
|
---|
[34b9299] | 812 |
|
---|
| 813 | char exp_sign = (exp_val < 0) ? '-' : '+';
|
---|
[97f6b71] | 814 | TRY(_write_char(exp_sign, ps, written_bytes));
|
---|
[34b9299] | 815 |
|
---|
| 816 | /* Print the exponent. */
|
---|
| 817 | exp_val = abs(exp_val);
|
---|
[a35b458] | 818 |
|
---|
[34b9299] | 819 | char exp_str[4] = { 0 };
|
---|
| 820 |
|
---|
| 821 | exp_str[0] = '0' + exp_val / 100;
|
---|
[1433ecda] | 822 | exp_str[1] = '0' + (exp_val % 100) / 10;
|
---|
[34b9299] | 823 | exp_str[2] = '0' + (exp_val % 10);
|
---|
[a35b458] | 824 |
|
---|
[34b9299] | 825 | int exp_len = (exp_str[0] == '0') ? 2 : 3;
|
---|
| 826 | const char *exp_str_start = &exp_str[3] - exp_len;
|
---|
| 827 |
|
---|
[97f6b71] | 828 | return _write_bytes(exp_str_start, exp_len, ps, written_bytes);
|
---|
[34b9299] | 829 | }
|
---|
| 830 |
|
---|
[1b20da0] | 831 | /** Format and print the double string repressentation according
|
---|
| 832 | * to the %e specifier.
|
---|
[34b9299] | 833 | */
|
---|
[97f6b71] | 834 | static errno_t _format_double_str_scient(double_str_t *val_str, int precision,
|
---|
| 835 | int width, uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
|
---|
[34b9299] | 836 | {
|
---|
| 837 | int len = val_str->len;
|
---|
| 838 | int dec_exp = val_str->dec_exp;
|
---|
| 839 | char *buf = val_str->str;
|
---|
| 840 |
|
---|
| 841 | assert(0 < len);
|
---|
| 842 |
|
---|
[97f6b71] | 843 | char sign = _get_sign_char(val_str->neg, flags);
|
---|
[34b9299] | 844 | bool has_decimal_pt = (0 < precision) || (flags & __PRINTF_FLAG_DECIMALPT);
|
---|
| 845 | int dec_pt_len = has_decimal_pt ? 1 : 0;
|
---|
| 846 |
|
---|
| 847 | /* Fractional part lengths. */
|
---|
| 848 | int signif_frac_figs = len - 1;
|
---|
| 849 | int trailing_frac_zeros = precision - signif_frac_figs;
|
---|
| 850 |
|
---|
| 851 | if (flags & __PRINTF_FLAG_NOFRACZEROS) {
|
---|
| 852 | trailing_frac_zeros = 0;
|
---|
| 853 | }
|
---|
| 854 |
|
---|
| 855 | int frac_len = signif_frac_figs + trailing_frac_zeros;
|
---|
| 856 |
|
---|
| 857 | int exp_val = dec_exp + len - 1;
|
---|
| 858 | /* Account for exponent sign and 'e'; minimum 2 digits. */
|
---|
| 859 | int exp_len = 2 + (abs(exp_val) >= 100 ? 3 : 2);
|
---|
| 860 |
|
---|
| 861 | /* Number of non-padding chars to print. */
|
---|
| 862 | int num_len = (sign ? 1 : 0) + 1 + dec_pt_len + frac_len + exp_len;
|
---|
| 863 |
|
---|
| 864 | int padding_len = max(0, width - num_len);
|
---|
| 865 |
|
---|
[97f6b71] | 866 | if (!(flags & (__PRINTF_FLAG_LEFTALIGNED | __PRINTF_FLAG_ZEROPADDED)))
|
---|
| 867 | TRY(_write_spaces(padding_len, ps, written_bytes));
|
---|
[34b9299] | 868 |
|
---|
[97f6b71] | 869 | if (sign)
|
---|
| 870 | TRY(_write_char(sign, ps, written_bytes));
|
---|
[a35b458] | 871 |
|
---|
[97f6b71] | 872 | if (flags & __PRINTF_FLAG_ZEROPADDED)
|
---|
| 873 | TRY(_write_zeros(padding_len, ps, written_bytes));
|
---|
[34b9299] | 874 |
|
---|
| 875 | /* Single leading integer. */
|
---|
[97f6b71] | 876 | TRY(_write_char(buf[0], ps, written_bytes));
|
---|
[34b9299] | 877 |
|
---|
| 878 | /* Print the decimal point and the fractional part. */
|
---|
| 879 | if (has_decimal_pt) {
|
---|
[97f6b71] | 880 | TRY(_write_char('.', ps, written_bytes));
|
---|
[34b9299] | 881 |
|
---|
| 882 | /* Print significant digits of the fractional part of the number. */
|
---|
[97f6b71] | 883 | if (0 < signif_frac_figs)
|
---|
| 884 | TRY(_write_bytes(buf + 1, signif_frac_figs, ps, written_bytes));
|
---|
[34b9299] | 885 |
|
---|
| 886 | /* Print trailing zeros of the fractional part of the number. */
|
---|
[97f6b71] | 887 | TRY(_write_zeros(trailing_frac_zeros, ps, written_bytes));
|
---|
[34b9299] | 888 | }
|
---|
| 889 |
|
---|
| 890 | /* Print the exponent. */
|
---|
[97f6b71] | 891 | TRY(_format_exponent(exp_val, flags, ps, written_bytes));
|
---|
[34b9299] | 892 |
|
---|
[97f6b71] | 893 | if (flags & __PRINTF_FLAG_LEFTALIGNED)
|
---|
| 894 | TRY(_write_spaces(padding_len, ps, written_bytes));
|
---|
[34b9299] | 895 |
|
---|
[97f6b71] | 896 | return EOK;
|
---|
[34b9299] | 897 | }
|
---|
| 898 |
|
---|
[1b20da0] | 899 | /** Convert, format and print a double according to the %e specifier.
|
---|
[34b9299] | 900 | *
|
---|
[1b20da0] | 901 | * Note that if g is large, the output may be huge (3e100 prints
|
---|
[34b9299] | 902 | * with at least 100 digits).
|
---|
| 903 | *
|
---|
| 904 | * %e style: [-]d.dddde+dd
|
---|
| 905 | * left-justified: [-]d.dddde+dd[space_pad]
|
---|
| 906 | * right-justified: [space_pad][-][zero_pad]d.dddde+dd
|
---|
| 907 | *
|
---|
| 908 | * @param g Double to print.
|
---|
| 909 | * @param precision Number of fractional digits to print, ie
|
---|
| 910 | * precision + 1 significant digits to display. If 0 no
|
---|
| 911 | * decimal point will be printed unless the flag
|
---|
| 912 | * __PRINTF_FLAG_DECIMALPT is specified. If negative
|
---|
| 913 | * the shortest accurate number will be printed.
|
---|
[1b20da0] | 914 | * @param width Minimum number of characters to display. Pads
|
---|
[34b9299] | 915 | * with '0' or ' ' depending on the set flags;
|
---|
| 916 | * @param flags Printf flags.
|
---|
| 917 | * @param ps Printing functions.
|
---|
| 918 | */
|
---|
[97f6b71] | 919 | static errno_t _format_double_scientific(double g, int precision, int width,
|
---|
| 920 | uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
|
---|
[34b9299] | 921 | {
|
---|
[97f6b71] | 922 | if (flags & __PRINTF_FLAG_LEFTALIGNED)
|
---|
[34b9299] | 923 | flags &= ~__PRINTF_FLAG_ZEROPADDED;
|
---|
| 924 |
|
---|
| 925 | ieee_double_t val = extract_ieee_double(g);
|
---|
| 926 |
|
---|
[97f6b71] | 927 | if (val.is_special)
|
---|
| 928 | return _format_special(val, width, flags, ps, written_bytes);
|
---|
[34b9299] | 929 |
|
---|
| 930 | char buf[MAX_DOUBLE_STR_BUF_SIZE];
|
---|
| 931 | const size_t buf_size = MAX_DOUBLE_STR_BUF_SIZE;
|
---|
| 932 | double_str_t val_str;
|
---|
| 933 |
|
---|
| 934 | val_str.str = buf;
|
---|
| 935 | val_str.neg = val.is_negative;
|
---|
| 936 |
|
---|
| 937 | if (0 <= precision) {
|
---|
[1b20da0] | 938 | /*
|
---|
| 939 | * Request one more digit (in addition to the leading integer)
|
---|
| 940 | * so we can round the result. The last digit it returns may
|
---|
| 941 | * have an error of at most +/- 1.
|
---|
[34b9299] | 942 | */
|
---|
[1b20da0] | 943 | val_str.len = double_to_fixed_str(val, precision + 2, -1, buf, buf_size,
|
---|
[1433ecda] | 944 | &val_str.dec_exp);
|
---|
[34b9299] | 945 |
|
---|
[1b20da0] | 946 | /*
|
---|
| 947 | * Round the extra digit to produce precision+1 significant digits.
|
---|
| 948 | * If less than precision+2 significant digits were returned the last
|
---|
| 949 | * digit is definitely inaccurate so also round to get rid of it.
|
---|
[34b9299] | 950 | */
|
---|
[97f6b71] | 951 | _fp_round_up(buf, &val_str.len, &val_str.dec_exp);
|
---|
[34b9299] | 952 |
|
---|
| 953 | /* Rounding could have introduced trailing zeros. */
|
---|
| 954 | if (flags & __PRINTF_FLAG_NOFRACZEROS) {
|
---|
[97f6b71] | 955 | _fp_trim_trailing_zeros(buf, &val_str.len, &val_str.dec_exp);
|
---|
[34b9299] | 956 | }
|
---|
| 957 | } else {
|
---|
| 958 | /* Let the implementation figure out the proper precision. */
|
---|
| 959 | val_str.len = double_to_short_str(val, buf, buf_size, &val_str.dec_exp);
|
---|
[a35b458] | 960 |
|
---|
[34b9299] | 961 | /* Use all produced digits. */
|
---|
| 962 | precision = val_str.len - 1;
|
---|
| 963 | }
|
---|
| 964 |
|
---|
[97f6b71] | 965 | return _format_double_str_scient(&val_str, precision, width, flags, ps, written_bytes);
|
---|
[34b9299] | 966 | }
|
---|
| 967 |
|
---|
[1b20da0] | 968 | /** Convert, format and print a double according to the %g specifier.
|
---|
[34b9299] | 969 | *
|
---|
[1b20da0] | 970 | * %g style chooses between %f and %e.
|
---|
[34b9299] | 971 | *
|
---|
| 972 | * @param g Double to print.
|
---|
| 973 | * @param precision Number of significant digits to display; excluding
|
---|
| 974 | * any leading zeros from this count. If negative
|
---|
| 975 | * the shortest accurate number will be printed.
|
---|
[1b20da0] | 976 | * @param width Minimum number of characters to display. Pads
|
---|
[34b9299] | 977 | * with '0' or ' ' depending on the set flags;
|
---|
| 978 | * @param flags Printf flags.
|
---|
| 979 | * @param ps Printing functions.
|
---|
| 980 | *
|
---|
| 981 | * @return The number of characters printed; negative on failure.
|
---|
| 982 | */
|
---|
[97f6b71] | 983 | static errno_t _format_double_generic(double g, int precision, int width,
|
---|
| 984 | uint32_t flags, printf_spec_t *ps, size_t *written_bytes)
|
---|
[34b9299] | 985 | {
|
---|
| 986 | ieee_double_t val = extract_ieee_double(g);
|
---|
| 987 |
|
---|
[97f6b71] | 988 | if (val.is_special)
|
---|
| 989 | return _format_special(val, width, flags, ps, written_bytes);
|
---|
[34b9299] | 990 |
|
---|
| 991 | char buf[MAX_DOUBLE_STR_BUF_SIZE];
|
---|
| 992 | const size_t buf_size = MAX_DOUBLE_STR_BUF_SIZE;
|
---|
| 993 | int dec_exp;
|
---|
| 994 | int len;
|
---|
| 995 |
|
---|
| 996 | /* Honor the user requested number of significant digits. */
|
---|
| 997 | if (0 <= precision) {
|
---|
[1b20da0] | 998 | /*
|
---|
| 999 | * Do a quick and dirty conversion of a single digit to determine
|
---|
[34b9299] | 1000 | * the decimal exponent.
|
---|
| 1001 | */
|
---|
| 1002 | len = double_to_fixed_str(val, 1, -1, buf, buf_size, &dec_exp);
|
---|
| 1003 | assert(0 < len);
|
---|
| 1004 |
|
---|
| 1005 | precision = max(1, precision);
|
---|
| 1006 |
|
---|
| 1007 | if (-4 <= dec_exp && dec_exp < precision) {
|
---|
| 1008 | precision = precision - (dec_exp + 1);
|
---|
[97f6b71] | 1009 | return _format_double_fixed(g, precision, width,
|
---|
| 1010 | flags | __PRINTF_FLAG_NOFRACZEROS, ps, written_bytes);
|
---|
[34b9299] | 1011 | } else {
|
---|
| 1012 | --precision;
|
---|
[97f6b71] | 1013 | return _format_double_scientific(g, precision, width,
|
---|
| 1014 | flags | __PRINTF_FLAG_NOFRACZEROS, ps, written_bytes);
|
---|
[34b9299] | 1015 | }
|
---|
| 1016 | } else {
|
---|
[d1582b50] | 1017 | /* Convert to get the decimal exponent and digit count. */
|
---|
[34b9299] | 1018 | len = double_to_short_str(val, buf, buf_size, &dec_exp);
|
---|
| 1019 | assert(0 < len);
|
---|
| 1020 |
|
---|
| 1021 | if (flags & __PRINTF_FLAG_LEFTALIGNED) {
|
---|
| 1022 | flags &= ~__PRINTF_FLAG_ZEROPADDED;
|
---|
| 1023 | }
|
---|
| 1024 |
|
---|
| 1025 | double_str_t val_str;
|
---|
| 1026 | val_str.str = buf;
|
---|
| 1027 | val_str.len = len;
|
---|
| 1028 | val_str.neg = val.is_negative;
|
---|
| 1029 | val_str.dec_exp = dec_exp;
|
---|
| 1030 |
|
---|
| 1031 | int first_digit_pos = len + dec_exp;
|
---|
| 1032 | int last_digit_pos = dec_exp;
|
---|
| 1033 |
|
---|
| 1034 | /* The whole number (15 digits max) fits between dec places 15 .. -6 */
|
---|
| 1035 | if (len <= 15 && -6 <= last_digit_pos && first_digit_pos <= 15) {
|
---|
| 1036 | /* Precision needed for the last significant digit. */
|
---|
| 1037 | precision = max(0, -val_str.dec_exp);
|
---|
[97f6b71] | 1038 | return _format_double_str_fixed(&val_str, precision, width, flags, ps, written_bytes);
|
---|
[34b9299] | 1039 | } else {
|
---|
| 1040 | /* Use all produced digits. */
|
---|
| 1041 | precision = val_str.len - 1;
|
---|
[97f6b71] | 1042 | return _format_double_str_scient(&val_str, precision, width, flags, ps, written_bytes);
|
---|
[34b9299] | 1043 | }
|
---|
| 1044 | }
|
---|
| 1045 | }
|
---|
| 1046 |
|
---|
[1b20da0] | 1047 | /** Convert, format and print a double according to the specifier.
|
---|
[34b9299] | 1048 | *
|
---|
| 1049 | * Depending on the specifier it prints the double using the styles
|
---|
| 1050 | * %g, %f or %e by means of print_double_generic(), print_double_fixed(),
|
---|
| 1051 | * print_double_scientific().
|
---|
| 1052 | *
|
---|
| 1053 | * @param g Double to print.
|
---|
| 1054 | * @param spec Specifier of the style to print in; one of: 'g','G','f','F',
|
---|
| 1055 | * 'e','E'.
|
---|
| 1056 | * @param precision Number of fractional digits to display. If negative
|
---|
| 1057 | * the shortest accurate number will be printed for style %g;
|
---|
| 1058 | * negative precision defaults to 6 for styles %f, %e.
|
---|
[1b20da0] | 1059 | * @param width Minimum number of characters to display. Pads
|
---|
[34b9299] | 1060 | * with '0' or ' ' depending on the set flags;
|
---|
| 1061 | * @param flags Printf flags.
|
---|
| 1062 | * @param ps Printing functions.
|
---|
| 1063 | */
|
---|
[97f6b71] | 1064 | static errno_t _format_double(double g, char spec, int precision, int width,
|
---|
| 1065 | uint32_t flags, printf_spec_t *ps, size_t *written_chars)
|
---|
[34b9299] | 1066 | {
|
---|
| 1067 | switch (spec) {
|
---|
| 1068 | case 'F':
|
---|
| 1069 | flags |= __PRINTF_FLAG_BIGCHARS;
|
---|
[dc12262] | 1070 | /* Fallthrough */
|
---|
[34b9299] | 1071 | case 'f':
|
---|
| 1072 | precision = (precision < 0) ? 6 : precision;
|
---|
[97f6b71] | 1073 | return _format_double_fixed(g, precision, width, flags, ps, written_chars);
|
---|
[a35b458] | 1074 |
|
---|
[34b9299] | 1075 | case 'E':
|
---|
| 1076 | flags |= __PRINTF_FLAG_BIGCHARS;
|
---|
[dc12262] | 1077 | /* Fallthrough */
|
---|
[34b9299] | 1078 | case 'e':
|
---|
| 1079 | precision = (precision < 0) ? 6 : precision;
|
---|
[97f6b71] | 1080 | return _format_double_scientific(g, precision, width, flags, ps, written_chars);
|
---|
[a35b458] | 1081 |
|
---|
[34b9299] | 1082 | case 'G':
|
---|
| 1083 | flags |= __PRINTF_FLAG_BIGCHARS;
|
---|
[dc12262] | 1084 | /* Fallthrough */
|
---|
[34b9299] | 1085 | case 'g':
|
---|
[97f6b71] | 1086 | return _format_double_generic(g, precision, width, flags, ps, written_chars);
|
---|
[a35b458] | 1087 |
|
---|
[34b9299] | 1088 | default:
|
---|
| 1089 | assert(false);
|
---|
| 1090 | return -1;
|
---|
| 1091 | }
|
---|
| 1092 | }
|
---|
| 1093 |
|
---|
[694ca3d6] | 1094 | #endif
|
---|
| 1095 |
|
---|
[97f6b71] | 1096 | static const char *_strchrnul(const char *s, int c)
|
---|
| 1097 | {
|
---|
| 1098 | while (*s != c && *s != 0)
|
---|
| 1099 | s++;
|
---|
| 1100 | return s;
|
---|
| 1101 | }
|
---|
| 1102 |
|
---|
| 1103 | /** Read a sequence of digits from the format string as a number.
|
---|
| 1104 | * If the number has too many digits to fit in int, returns INT_MAX.
|
---|
| 1105 | */
|
---|
| 1106 | static int _read_num(const char *fmt, size_t *i)
|
---|
| 1107 | {
|
---|
| 1108 | const char *s;
|
---|
| 1109 | unsigned n = 0;
|
---|
| 1110 |
|
---|
| 1111 | for (s = &fmt[*i]; isdigit(*s); s++) {
|
---|
| 1112 | unsigned digit = (*s - '0');
|
---|
| 1113 |
|
---|
| 1114 | /* Check for overflow */
|
---|
| 1115 | if (n > INT_MAX / 10 || n * 10 > INT_MAX - digit) {
|
---|
| 1116 | n = INT_MAX;
|
---|
| 1117 | while (isdigit(*s))
|
---|
| 1118 | s++;
|
---|
| 1119 | break;
|
---|
| 1120 | }
|
---|
| 1121 |
|
---|
| 1122 | n = n * 10 + digit;
|
---|
| 1123 | }
|
---|
| 1124 |
|
---|
| 1125 | *i = s - fmt;
|
---|
| 1126 | return n;
|
---|
| 1127 | }
|
---|
| 1128 |
|
---|
| 1129 | static uint32_t _parse_flags(const char *fmt, size_t *i)
|
---|
| 1130 | {
|
---|
| 1131 | uint32_t flags = 0;
|
---|
| 1132 |
|
---|
| 1133 | while (true) {
|
---|
| 1134 | switch (fmt[(*i)++]) {
|
---|
| 1135 | case '#':
|
---|
| 1136 | flags |= __PRINTF_FLAG_PREFIX;
|
---|
| 1137 | flags |= __PRINTF_FLAG_DECIMALPT;
|
---|
| 1138 | continue;
|
---|
| 1139 | case '-':
|
---|
| 1140 | flags |= __PRINTF_FLAG_LEFTALIGNED;
|
---|
| 1141 | continue;
|
---|
| 1142 | case '+':
|
---|
| 1143 | flags |= __PRINTF_FLAG_SHOWPLUS;
|
---|
| 1144 | continue;
|
---|
| 1145 | case ' ':
|
---|
| 1146 | flags |= __PRINTF_FLAG_SPACESIGN;
|
---|
| 1147 | continue;
|
---|
| 1148 | case '0':
|
---|
| 1149 | flags |= __PRINTF_FLAG_ZEROPADDED;
|
---|
| 1150 | continue;
|
---|
| 1151 | }
|
---|
| 1152 |
|
---|
| 1153 | --*i;
|
---|
| 1154 | break;
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
| 1157 | return flags;
|
---|
| 1158 | }
|
---|
| 1159 |
|
---|
| 1160 | static bool _eat_char(const char *s, size_t *idx, int c)
|
---|
| 1161 | {
|
---|
| 1162 | if (s[*idx] != c)
|
---|
| 1163 | return false;
|
---|
| 1164 |
|
---|
| 1165 | (*idx)++;
|
---|
| 1166 | return true;
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
| 1169 | static qualifier_t _read_qualifier(const char *s, size_t *idx)
|
---|
| 1170 | {
|
---|
| 1171 | switch (s[(*idx)++]) {
|
---|
| 1172 | case 't': /* ptrdiff_t */
|
---|
| 1173 | case 'z': /* size_t */
|
---|
| 1174 | if (sizeof(ptrdiff_t) == sizeof(int))
|
---|
| 1175 | return PrintfQualifierInt;
|
---|
| 1176 | else
|
---|
| 1177 | return PrintfQualifierLong;
|
---|
| 1178 |
|
---|
| 1179 | case 'h':
|
---|
| 1180 | if (_eat_char(s, idx, 'h'))
|
---|
| 1181 | return PrintfQualifierByte;
|
---|
| 1182 | else
|
---|
| 1183 | return PrintfQualifierShort;
|
---|
| 1184 |
|
---|
| 1185 | case 'l':
|
---|
| 1186 | if (_eat_char(s, idx, 'l'))
|
---|
| 1187 | return PrintfQualifierLongLong;
|
---|
| 1188 | else
|
---|
| 1189 | return PrintfQualifierLong;
|
---|
| 1190 |
|
---|
| 1191 | case 'j':
|
---|
| 1192 | return PrintfQualifierLongLong;
|
---|
| 1193 |
|
---|
| 1194 | default:
|
---|
| 1195 | --*idx;
|
---|
| 1196 |
|
---|
| 1197 | /* Unspecified */
|
---|
| 1198 | return PrintfQualifierInt;
|
---|
| 1199 | }
|
---|
| 1200 | }
|
---|
| 1201 |
|
---|
[0bc36ba] | 1202 | /** Print formatted string.
|
---|
[4e2cf8b] | 1203 | *
|
---|
[1c38445] | 1204 | * Print string formatted according to the fmt parameter and variadic arguments.
|
---|
| 1205 | * Each formatting directive must have the following form:
|
---|
[f2b8cdc] | 1206 | *
|
---|
| 1207 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION
|
---|
[0bc36ba] | 1208 | *
|
---|
| 1209 | * FLAGS:@n
|
---|
[f2b8cdc] | 1210 | * - "#" Force to print prefix. For \%o conversion, the prefix is 0, for
|
---|
| 1211 | * \%x and \%X prefixes are 0x and 0X and for conversion \%b the
|
---|
| 1212 | * prefix is 0b.
|
---|
[4e2cf8b] | 1213 | *
|
---|
[f2b8cdc] | 1214 | * - "-" Align to left.
|
---|
[0bc36ba] | 1215 | *
|
---|
[f2b8cdc] | 1216 | * - "+" Print positive sign just as negative.
|
---|
[0bc36ba] | 1217 | *
|
---|
[f2b8cdc] | 1218 | * - " " If the printed number is positive and "+" flag is not set,
|
---|
| 1219 | * print space in place of sign.
|
---|
| 1220 | *
|
---|
| 1221 | * - "0" Print 0 as padding instead of spaces. Zeroes are placed between
|
---|
| 1222 | * sign and the rest of the number. This flag is ignored if "-"
|
---|
| 1223 | * flag is specified.
|
---|
[0bc36ba] | 1224 | *
|
---|
| 1225 | * WIDTH:@n
|
---|
[f2b8cdc] | 1226 | * - Specify the minimal width of a printed argument. If it is bigger,
|
---|
| 1227 | * width is ignored. If width is specified with a "*" character instead of
|
---|
| 1228 | * number, width is taken from parameter list. And integer parameter is
|
---|
| 1229 | * expected before parameter for processed conversion specification. If
|
---|
| 1230 | * this value is negative its absolute value is taken and the "-" flag is
|
---|
| 1231 | * set.
|
---|
[4e2cf8b] | 1232 | *
|
---|
[0bc36ba] | 1233 | * PRECISION:@n
|
---|
[f2b8cdc] | 1234 | * - Value precision. For numbers it specifies minimum valid numbers.
|
---|
| 1235 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not
|
---|
| 1236 | * affected. Strings with more than precision characters are cut off. Just
|
---|
| 1237 | * as with width, an "*" can be used used instead of a number. An integer
|
---|
| 1238 | * value is then expected in parameters. When both width and precision are
|
---|
| 1239 | * specified using "*", the first parameter is used for width and the
|
---|
| 1240 | * second one for precision.
|
---|
| 1241 | *
|
---|
[0bc36ba] | 1242 | * TYPE:@n
|
---|
[f2b8cdc] | 1243 | * - "hh" Signed or unsigned char.@n
|
---|
| 1244 | * - "h" Signed or unsigned short.@n
|
---|
| 1245 | * - "" Signed or unsigned int (default value).@n
|
---|
| 1246 | * - "l" Signed or unsigned long int.@n
|
---|
[1e27d85] | 1247 | * If conversion is "c", the character is wint_t (wide character).@n
|
---|
[28a5ebd] | 1248 | * If conversion is "s", the string is char32_t * (wide string).@n
|
---|
[f2b8cdc] | 1249 | * - "ll" Signed or unsigned long long int.@n
|
---|
[1e27d85] | 1250 | * - "z" Signed or unsigned ssize_t or site_t.@n
|
---|
[f2b8cdc] | 1251 | *
|
---|
[0bc36ba] | 1252 | * CONVERSION:@n
|
---|
[f2b8cdc] | 1253 | * - % Print percentile character itself.
|
---|
| 1254 | *
|
---|
| 1255 | * - c Print single character. The character is expected to be plain
|
---|
| 1256 | * ASCII (e.g. only values 0 .. 127 are valid).@n
|
---|
| 1257 | * If type is "l", then the character is expected to be wide character
|
---|
| 1258 | * (e.g. values 0 .. 0x10ffff are valid).
|
---|
| 1259 | *
|
---|
| 1260 | * - s Print zero terminated string. If a NULL value is passed as
|
---|
| 1261 | * value, "(NULL)" is printed instead.@n
|
---|
| 1262 | * If type is "l", then the string is expected to be wide string.
|
---|
| 1263 | *
|
---|
| 1264 | * - P, p Print value of a pointer. Void * value is expected and it is
|
---|
[2afb650] | 1265 | * printed in hexadecimal notation with prefix (as with
|
---|
| 1266 | * \%#0.8X / \%#0.8x for 32-bit or \%#0.16lX / \%#0.16lx for 64-bit
|
---|
| 1267 | * long pointers).
|
---|
[4e2cf8b] | 1268 | *
|
---|
[f2b8cdc] | 1269 | * - b Print value as unsigned binary number. Prefix is not printed by
|
---|
| 1270 | * default. (Nonstandard extension.)
|
---|
[4e2cf8b] | 1271 | *
|
---|
[f2b8cdc] | 1272 | * - o Print value as unsigned octal number. Prefix is not printed by
|
---|
| 1273 | * default.
|
---|
[4e2cf8b] | 1274 | *
|
---|
[f2b8cdc] | 1275 | * - d, i Print signed decimal number. There is no difference between d
|
---|
| 1276 | * and i conversion.
|
---|
[4e2cf8b] | 1277 | *
|
---|
[f2b8cdc] | 1278 | * - u Print unsigned decimal number.
|
---|
[4e2cf8b] | 1279 | *
|
---|
[f2b8cdc] | 1280 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is
|
---|
| 1281 | * not printed by default.
|
---|
[4e2cf8b] | 1282 | *
|
---|
[f2b8cdc] | 1283 | * All other characters from fmt except the formatting directives are printed
|
---|
[1c38445] | 1284 | * verbatim.
|
---|
[4e2cf8b] | 1285 | *
|
---|
[f2b8cdc] | 1286 | * @param fmt Format NULL-terminated string.
|
---|
| 1287 | *
|
---|
| 1288 | * @return Number of characters printed, negative value on failure.
|
---|
| 1289 | *
|
---|
[4e2cf8b] | 1290 | */
|
---|
[f2b8cdc] | 1291 | int printf_core(const char *fmt, printf_spec_t *ps, va_list ap)
|
---|
[4e2cf8b] | 1292 | {
|
---|
[97f6b71] | 1293 | errno_t rc = EOK;
|
---|
[f2b8cdc] | 1294 | size_t nxt = 0; /* Index of the next character from fmt */
|
---|
[a35b458] | 1295 |
|
---|
[2595dab] | 1296 | size_t counter = 0; /* Number of characters printed */
|
---|
[a35b458] | 1297 |
|
---|
[97f6b71] | 1298 | while (rc == EOK) {
|
---|
| 1299 | /* Find the next specifier and write all the bytes before it. */
|
---|
| 1300 | const char *s = _strchrnul(&fmt[nxt], '%');
|
---|
| 1301 | size_t bytes = s - &fmt[nxt];
|
---|
| 1302 | rc = _write_bytes(&fmt[nxt], bytes, ps, &counter);
|
---|
| 1303 | if (rc != EOK)
|
---|
| 1304 | break;
|
---|
[a35b458] | 1305 |
|
---|
[97f6b71] | 1306 | nxt += bytes;
|
---|
| 1307 |
|
---|
| 1308 | /* Check for end of string. */
|
---|
| 1309 | if (_eat_char(fmt, &nxt, 0))
|
---|
[f2b8cdc] | 1310 | break;
|
---|
[a35b458] | 1311 |
|
---|
[97f6b71] | 1312 | /* We must be at the start of a specifier. */
|
---|
| 1313 | bool spec = _eat_char(fmt, &nxt, '%');
|
---|
| 1314 | assert(spec);
|
---|
[a35b458] | 1315 |
|
---|
[97f6b71] | 1316 | /* Parse modifiers */
|
---|
| 1317 | uint32_t flags = _parse_flags(fmt, &nxt);
|
---|
| 1318 |
|
---|
| 1319 | /* Width & '*' operator */
|
---|
| 1320 | int width = -1;
|
---|
| 1321 | if (_eat_char(fmt, &nxt, '*')) {
|
---|
| 1322 | /* Get width value from argument list */
|
---|
| 1323 | width = va_arg(ap, int);
|
---|
[a35b458] | 1324 |
|
---|
[97f6b71] | 1325 | if (width < 0) {
|
---|
| 1326 | /* Negative width sets '-' flag */
|
---|
| 1327 | width = (width == INT_MIN) ? INT_MAX : -width;
|
---|
| 1328 | flags |= __PRINTF_FLAG_LEFTALIGNED;
|
---|
[c9857c6] | 1329 | }
|
---|
[97f6b71] | 1330 | } else {
|
---|
| 1331 | width = _read_num(fmt, &nxt);
|
---|
| 1332 | }
|
---|
| 1333 |
|
---|
| 1334 | /* Precision and '*' operator */
|
---|
| 1335 | int precision = -1;
|
---|
| 1336 | if (_eat_char(fmt, &nxt, '.')) {
|
---|
| 1337 | if (_eat_char(fmt, &nxt, '*')) {
|
---|
| 1338 | /* Get precision value from the argument list */
|
---|
| 1339 | precision = va_arg(ap, int);
|
---|
[a35b458] | 1340 |
|
---|
[97f6b71] | 1341 | /* Negative is treated as omitted. */
|
---|
| 1342 | if (precision < 0)
|
---|
| 1343 | precision = -1;
|
---|
| 1344 | } else {
|
---|
| 1345 | precision = _read_num(fmt, &nxt);
|
---|
[f2b8cdc] | 1346 | }
|
---|
[97f6b71] | 1347 | }
|
---|
[a35b458] | 1348 |
|
---|
[97f6b71] | 1349 | qualifier_t qualifier = _read_qualifier(fmt, &nxt);
|
---|
| 1350 | unsigned int base = 10;
|
---|
| 1351 | char specifier = fmt[nxt++];
|
---|
[a35b458] | 1352 |
|
---|
[97f6b71] | 1353 | switch (specifier) {
|
---|
| 1354 | /*
|
---|
| 1355 | * String and character conversions.
|
---|
| 1356 | */
|
---|
| 1357 | case 's':
|
---|
| 1358 | if (qualifier == PrintfQualifierLong)
|
---|
| 1359 | rc = _format_wstr(va_arg(ap, char32_t *), width, precision, flags, ps, &counter);
|
---|
| 1360 | else
|
---|
| 1361 | rc = _format_cstr(va_arg(ap, char *), width, precision, flags, ps, &counter);
|
---|
| 1362 | continue;
|
---|
| 1363 |
|
---|
| 1364 | case 'c':
|
---|
| 1365 | if (qualifier == PrintfQualifierLong)
|
---|
| 1366 | rc = _format_uchar(va_arg(ap, wint_t), width, flags, ps, &counter);
|
---|
| 1367 | else
|
---|
| 1368 | rc = _format_char(va_arg(ap, int), width, flags, ps, &counter);
|
---|
| 1369 | continue;
|
---|
| 1370 |
|
---|
| 1371 | /*
|
---|
| 1372 | * Floating point values
|
---|
| 1373 | */
|
---|
| 1374 | case 'G':
|
---|
| 1375 | case 'g':
|
---|
| 1376 | case 'F':
|
---|
| 1377 | case 'f':
|
---|
| 1378 | case 'E':
|
---|
| 1379 | case 'e':;
|
---|
[694ca3d6] | 1380 | #ifdef HAS_FLOAT
|
---|
[97f6b71] | 1381 | rc = _format_double(va_arg(ap, double), specifier, precision,
|
---|
| 1382 | width, flags, ps, &counter);
|
---|
| 1383 | #else
|
---|
| 1384 | rc = _format_cstr("<float unsupported>", width, -1, 0, ps, &counter);
|
---|
[694ca3d6] | 1385 | #endif
|
---|
[97f6b71] | 1386 | continue;
|
---|
[a35b458] | 1387 |
|
---|
[97f6b71] | 1388 | /*
|
---|
| 1389 | * Integer values
|
---|
| 1390 | */
|
---|
| 1391 | case 'P':
|
---|
| 1392 | /* Pointer */
|
---|
| 1393 | flags |= __PRINTF_FLAG_BIGCHARS;
|
---|
| 1394 | /* Fallthrough */
|
---|
| 1395 | case 'p':
|
---|
| 1396 | flags |= __PRINTF_FLAG_PREFIX;
|
---|
| 1397 | flags |= __PRINTF_FLAG_ZEROPADDED;
|
---|
| 1398 | base = 16;
|
---|
| 1399 | qualifier = PrintfQualifierPointer;
|
---|
| 1400 | break;
|
---|
| 1401 | case 'b':
|
---|
| 1402 | base = 2;
|
---|
| 1403 | break;
|
---|
| 1404 | case 'o':
|
---|
| 1405 | base = 8;
|
---|
| 1406 | break;
|
---|
| 1407 | case 'd':
|
---|
| 1408 | case 'i':
|
---|
| 1409 | flags |= __PRINTF_FLAG_SIGNED;
|
---|
| 1410 | break;
|
---|
| 1411 | case 'u':
|
---|
| 1412 | break;
|
---|
| 1413 | case 'X':
|
---|
| 1414 | flags |= __PRINTF_FLAG_BIGCHARS;
|
---|
| 1415 | /* Fallthrough */
|
---|
| 1416 | case 'x':
|
---|
| 1417 | base = 16;
|
---|
| 1418 | break;
|
---|
[a35b458] | 1419 |
|
---|
[97f6b71] | 1420 | case '%':
|
---|
| 1421 | /* Percentile itself */
|
---|
| 1422 | rc = _write_char('%', ps, &counter);
|
---|
| 1423 | continue;
|
---|
[a35b458] | 1424 |
|
---|
[97f6b71] | 1425 | /*
|
---|
| 1426 | * Bad formatting.
|
---|
| 1427 | */
|
---|
| 1428 | default:
|
---|
| 1429 | rc = EINVAL;
|
---|
| 1430 | continue;
|
---|
| 1431 | }
|
---|
[a35b458] | 1432 |
|
---|
[97f6b71] | 1433 | /* Print integers */
|
---|
| 1434 | uint64_t number;
|
---|
| 1435 |
|
---|
| 1436 | switch (qualifier) {
|
---|
| 1437 | case PrintfQualifierByte:
|
---|
| 1438 | number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);
|
---|
| 1439 | break;
|
---|
| 1440 | case PrintfQualifierShort:
|
---|
| 1441 | number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);
|
---|
| 1442 | break;
|
---|
| 1443 | case PrintfQualifierInt:
|
---|
| 1444 | number = PRINTF_GET_INT_ARGUMENT(int, ap, flags);
|
---|
| 1445 | break;
|
---|
| 1446 | case PrintfQualifierLong:
|
---|
| 1447 | number = PRINTF_GET_INT_ARGUMENT(long, ap, flags);
|
---|
| 1448 | break;
|
---|
| 1449 | case PrintfQualifierLongLong:
|
---|
| 1450 | number = PRINTF_GET_INT_ARGUMENT(long long, ap, flags);
|
---|
| 1451 | break;
|
---|
| 1452 | case PrintfQualifierPointer:
|
---|
| 1453 | precision = sizeof(void *) << 1;
|
---|
| 1454 | number = (uint64_t) (uintptr_t) va_arg(ap, void *);
|
---|
| 1455 | break;
|
---|
| 1456 | default:
|
---|
| 1457 | /* Unknown qualifier */
|
---|
| 1458 | rc = EINVAL;
|
---|
| 1459 | continue;
|
---|
[f2b8cdc] | 1460 | }
|
---|
[97f6b71] | 1461 |
|
---|
| 1462 | rc = _format_number(number, width, precision, base, flags, ps, &counter);
|
---|
[4e2cf8b] | 1463 | }
|
---|
[a35b458] | 1464 |
|
---|
[97f6b71] | 1465 | if (rc != EOK) {
|
---|
| 1466 | _set_errno(rc);
|
---|
| 1467 | return -1;
|
---|
| 1468 | }
|
---|
| 1469 |
|
---|
| 1470 | if (counter > INT_MAX) {
|
---|
| 1471 | _set_errno(EOVERFLOW);
|
---|
| 1472 | return -1;
|
---|
[4ba1db5] | 1473 | }
|
---|
[a35b458] | 1474 |
|
---|
[97f6b71] | 1475 | return (int) counter;
|
---|
[4e2cf8b] | 1476 | }
|
---|
| 1477 |
|
---|
[fadd381] | 1478 | /** @}
|
---|
[b2951e2] | 1479 | */
|
---|