Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013-2015 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include <assert.h> |
| 28 | #include <stdarg.h> |
| 29 | #include <stdint.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | |
Damien George | 731f359 | 2015-10-30 23:03:58 +0000 | [diff] [blame^] | 33 | #include "py/mphal.h" |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 34 | #include "py/mpprint.h" |
| 35 | #include "py/obj.h" |
| 36 | #include "py/objint.h" |
| 37 | #include "py/runtime.h" |
| 38 | |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 39 | #if MICROPY_PY_BUILTINS_FLOAT |
| 40 | #include "py/formatfloat.h" |
| 41 | #endif |
| 42 | |
| 43 | static const char pad_spaces[] = " "; |
| 44 | static const char pad_zeroes[] = "0000000000000000"; |
| 45 | |
| 46 | STATIC void plat_print_strn(void *env, const char *str, mp_uint_t len) { |
| 47 | (void)env; |
| 48 | MP_PLAT_PRINT_STRN(str, len); |
| 49 | } |
| 50 | |
| 51 | const mp_print_t mp_plat_print = {NULL, plat_print_strn}; |
| 52 | |
| 53 | int mp_print_str(const mp_print_t *print, const char *str) { |
| 54 | mp_uint_t len = strlen(str); |
| 55 | if (len) { |
| 56 | print->print_strn(print->data, str, len); |
| 57 | } |
| 58 | return len; |
| 59 | } |
| 60 | |
| 61 | int mp_print_strn(const mp_print_t *print, const char *str, mp_uint_t len, int flags, char fill, int width) { |
| 62 | int left_pad = 0; |
| 63 | int right_pad = 0; |
| 64 | int pad = width - len; |
| 65 | int pad_size; |
| 66 | int total_chars_printed = 0; |
| 67 | const char *pad_chars; |
| 68 | |
| 69 | if (!fill || fill == ' ') { |
| 70 | pad_chars = pad_spaces; |
| 71 | pad_size = sizeof(pad_spaces) - 1; |
| 72 | } else if (fill == '0') { |
| 73 | pad_chars = pad_zeroes; |
| 74 | pad_size = sizeof(pad_zeroes) - 1; |
| 75 | } else { |
| 76 | // Other pad characters are fairly unusual, so we'll take the hit |
| 77 | // and output them 1 at a time. |
| 78 | pad_chars = &fill; |
| 79 | pad_size = 1; |
| 80 | } |
| 81 | |
| 82 | if (flags & PF_FLAG_CENTER_ADJUST) { |
| 83 | left_pad = pad / 2; |
| 84 | right_pad = pad - left_pad; |
| 85 | } else if (flags & PF_FLAG_LEFT_ADJUST) { |
| 86 | right_pad = pad; |
| 87 | } else { |
| 88 | left_pad = pad; |
| 89 | } |
| 90 | |
| 91 | if (left_pad > 0) { |
| 92 | total_chars_printed += left_pad; |
| 93 | while (left_pad > 0) { |
| 94 | int p = left_pad; |
| 95 | if (p > pad_size) { |
| 96 | p = pad_size; |
| 97 | } |
| 98 | print->print_strn(print->data, pad_chars, p); |
| 99 | left_pad -= p; |
| 100 | } |
| 101 | } |
| 102 | if (len) { |
| 103 | print->print_strn(print->data, str, len); |
| 104 | total_chars_printed += len; |
| 105 | } |
| 106 | if (right_pad > 0) { |
| 107 | total_chars_printed += right_pad; |
| 108 | while (right_pad > 0) { |
| 109 | int p = right_pad; |
| 110 | if (p > pad_size) { |
| 111 | p = pad_size; |
| 112 | } |
| 113 | print->print_strn(print->data, pad_chars, p); |
| 114 | right_pad -= p; |
| 115 | } |
| 116 | } |
| 117 | return total_chars_printed; |
| 118 | } |
| 119 | |
| 120 | // 32-bits is 10 digits, add 3 for commas, 1 for sign, 1 for terminating null |
| 121 | // We can use 16 characters for 32-bit and 32 characters for 64-bit |
| 122 | #define INT_BUF_SIZE (sizeof(mp_int_t) * 4) |
| 123 | |
Damien George | 2cae0f6 | 2015-05-28 13:54:56 +0000 | [diff] [blame] | 124 | // Our mp_vprintf function below does not support the '#' format modifier to |
| 125 | // print the prefix of a non-base-10 number, so we don't need code for this. |
| 126 | #define SUPPORT_INT_BASE_PREFIX (0) |
| 127 | |
| 128 | // This function is used exclusively by mp_vprintf to format ints. |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 129 | // It needs to be a separate function to mp_print_mp_int, since converting to a mp_int looses the MSB. |
Damien George | 2cae0f6 | 2015-05-28 13:54:56 +0000 | [diff] [blame] | 130 | STATIC int mp_print_int(const mp_print_t *print, mp_uint_t x, int sgn, int base, int base_char, int flags, char fill, int width) { |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 131 | char sign = 0; |
| 132 | if (sgn) { |
| 133 | if ((mp_int_t)x < 0) { |
| 134 | sign = '-'; |
| 135 | x = -x; |
| 136 | } else if (flags & PF_FLAG_SHOW_SIGN) { |
| 137 | sign = '+'; |
| 138 | } else if (flags & PF_FLAG_SPACE_SIGN) { |
| 139 | sign = ' '; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | char buf[INT_BUF_SIZE]; |
| 144 | char *b = buf + INT_BUF_SIZE; |
| 145 | |
| 146 | if (x == 0) { |
| 147 | *(--b) = '0'; |
| 148 | } else { |
| 149 | do { |
| 150 | int c = x % base; |
| 151 | x /= base; |
| 152 | if (c >= 10) { |
| 153 | c += base_char - 10; |
| 154 | } else { |
| 155 | c += '0'; |
| 156 | } |
| 157 | *(--b) = c; |
| 158 | } while (b > buf && x != 0); |
| 159 | } |
| 160 | |
Damien George | 2cae0f6 | 2015-05-28 13:54:56 +0000 | [diff] [blame] | 161 | #if SUPPORT_INT_BASE_PREFIX |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 162 | char prefix_char = '\0'; |
| 163 | |
| 164 | if (flags & PF_FLAG_SHOW_PREFIX) { |
| 165 | if (base == 2) { |
| 166 | prefix_char = base_char + 'b' - 'a'; |
| 167 | } else if (base == 8) { |
| 168 | prefix_char = base_char + 'o' - 'a'; |
| 169 | } else if (base == 16) { |
| 170 | prefix_char = base_char + 'x' - 'a'; |
| 171 | } |
| 172 | } |
Damien George | 2cae0f6 | 2015-05-28 13:54:56 +0000 | [diff] [blame] | 173 | #endif |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 174 | |
| 175 | int len = 0; |
| 176 | if (flags & PF_FLAG_PAD_AFTER_SIGN) { |
| 177 | if (sign) { |
| 178 | len += mp_print_strn(print, &sign, 1, flags, fill, 1); |
| 179 | width--; |
| 180 | } |
Damien George | 2cae0f6 | 2015-05-28 13:54:56 +0000 | [diff] [blame] | 181 | #if SUPPORT_INT_BASE_PREFIX |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 182 | if (prefix_char) { |
| 183 | len += mp_print_strn(print, "0", 1, flags, fill, 1); |
| 184 | len += mp_print_strn(print, &prefix_char, 1, flags, fill, 1); |
| 185 | width -= 2; |
| 186 | } |
Damien George | 2cae0f6 | 2015-05-28 13:54:56 +0000 | [diff] [blame] | 187 | #endif |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 188 | } else { |
Damien George | 2cae0f6 | 2015-05-28 13:54:56 +0000 | [diff] [blame] | 189 | #if SUPPORT_INT_BASE_PREFIX |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 190 | if (prefix_char && b > &buf[1]) { |
| 191 | *(--b) = prefix_char; |
| 192 | *(--b) = '0'; |
| 193 | } |
Damien George | 2cae0f6 | 2015-05-28 13:54:56 +0000 | [diff] [blame] | 194 | #endif |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 195 | if (sign && b > buf) { |
| 196 | *(--b) = sign; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | len += mp_print_strn(print, b, buf + INT_BUF_SIZE - b, flags, fill, width); |
| 201 | return len; |
| 202 | } |
| 203 | |
| 204 | int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char, int flags, char fill, int width, int prec) { |
| 205 | if (!MP_OBJ_IS_INT(x)) { |
| 206 | // This will convert booleans to int, or raise an error for |
| 207 | // non-integer types. |
| 208 | x = MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(x)); |
| 209 | } |
| 210 | |
| 211 | if ((flags & (PF_FLAG_LEFT_ADJUST | PF_FLAG_CENTER_ADJUST)) == 0 && fill == '0') { |
| 212 | if (prec > width) { |
| 213 | width = prec; |
| 214 | } |
| 215 | prec = 0; |
| 216 | } |
| 217 | char prefix_buf[4]; |
| 218 | char *prefix = prefix_buf; |
| 219 | |
| 220 | if (mp_obj_int_is_positive(x)) { |
| 221 | if (flags & PF_FLAG_SHOW_SIGN) { |
| 222 | *prefix++ = '+'; |
| 223 | } else if (flags & PF_FLAG_SPACE_SIGN) { |
| 224 | *prefix++ = ' '; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (flags & PF_FLAG_SHOW_PREFIX) { |
| 229 | if (base == 2) { |
| 230 | *prefix++ = '0'; |
| 231 | *prefix++ = base_char + 'b' - 'a'; |
| 232 | } else if (base == 8) { |
| 233 | *prefix++ = '0'; |
| 234 | if (flags & PF_FLAG_SHOW_OCTAL_LETTER) { |
| 235 | *prefix++ = base_char + 'o' - 'a'; |
| 236 | } |
| 237 | } else if (base == 16) { |
| 238 | *prefix++ = '0'; |
| 239 | *prefix++ = base_char + 'x' - 'a'; |
| 240 | } |
| 241 | } |
| 242 | *prefix = '\0'; |
| 243 | int prefix_len = prefix - prefix_buf; |
| 244 | prefix = prefix_buf; |
| 245 | |
| 246 | char comma = '\0'; |
| 247 | if (flags & PF_FLAG_SHOW_COMMA) { |
| 248 | comma = ','; |
| 249 | } |
| 250 | |
| 251 | // The size of this buffer is rather arbitrary. If it's not large |
| 252 | // enough, a dynamic one will be allocated. |
| 253 | char stack_buf[sizeof(mp_int_t) * 4]; |
| 254 | char *buf = stack_buf; |
| 255 | mp_uint_t buf_size = sizeof(stack_buf); |
| 256 | mp_uint_t fmt_size = 0; |
| 257 | char *str; |
| 258 | |
| 259 | if (prec > 1) { |
| 260 | flags |= PF_FLAG_PAD_AFTER_SIGN; |
| 261 | } |
| 262 | char sign = '\0'; |
| 263 | if (flags & PF_FLAG_PAD_AFTER_SIGN) { |
| 264 | // We add the pad in this function, so since the pad goes after |
| 265 | // the sign & prefix, we format without a prefix |
| 266 | str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size, |
| 267 | x, base, NULL, base_char, comma); |
| 268 | if (*str == '-') { |
| 269 | sign = *str++; |
| 270 | fmt_size--; |
| 271 | } |
| 272 | } else { |
| 273 | str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size, |
| 274 | x, base, prefix, base_char, comma); |
| 275 | } |
| 276 | |
| 277 | int spaces_before = 0; |
| 278 | int spaces_after = 0; |
| 279 | |
| 280 | if (prec > 1) { |
| 281 | // If prec was specified, then prec specifies the width to zero-pad the |
| 282 | // the number to. This zero-padded number then gets left or right |
| 283 | // aligned in width characters. |
| 284 | |
| 285 | int prec_width = fmt_size; // The digits |
| 286 | if (prec_width < prec) { |
| 287 | prec_width = prec; |
| 288 | } |
| 289 | if (flags & PF_FLAG_PAD_AFTER_SIGN) { |
| 290 | if (sign) { |
| 291 | prec_width++; |
| 292 | } |
| 293 | prec_width += prefix_len; |
| 294 | } |
| 295 | if (prec_width < width) { |
| 296 | if (flags & PF_FLAG_LEFT_ADJUST) { |
| 297 | spaces_after = width - prec_width; |
| 298 | } else { |
| 299 | spaces_before = width - prec_width; |
| 300 | } |
| 301 | } |
| 302 | fill = '0'; |
| 303 | flags &= ~PF_FLAG_LEFT_ADJUST; |
| 304 | } |
| 305 | |
| 306 | int len = 0; |
| 307 | if (spaces_before) { |
| 308 | len += mp_print_strn(print, "", 0, 0, ' ', spaces_before); |
| 309 | } |
| 310 | if (flags & PF_FLAG_PAD_AFTER_SIGN) { |
| 311 | // pad after sign implies pad after prefix as well. |
| 312 | if (sign) { |
| 313 | len += mp_print_strn(print, &sign, 1, 0, 0, 1); |
| 314 | width--; |
| 315 | } |
| 316 | if (prefix_len) { |
| 317 | len += mp_print_strn(print, prefix, prefix_len, 0, 0, 1); |
| 318 | width -= prefix_len; |
| 319 | } |
| 320 | } |
| 321 | if (prec > 1) { |
| 322 | width = prec; |
| 323 | } |
| 324 | |
| 325 | len += mp_print_strn(print, str, fmt_size, flags, fill, width); |
| 326 | |
| 327 | if (spaces_after) { |
| 328 | len += mp_print_strn(print, "", 0, 0, ' ', spaces_after); |
| 329 | } |
| 330 | |
| 331 | if (buf != stack_buf) { |
| 332 | m_del(char, buf, buf_size); |
| 333 | } |
| 334 | return len; |
| 335 | } |
| 336 | |
| 337 | #if MICROPY_PY_BUILTINS_FLOAT |
| 338 | int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, char fill, int width, int prec) { |
| 339 | char buf[32]; |
| 340 | char sign = '\0'; |
| 341 | int chrs = 0; |
| 342 | |
| 343 | if (flags & PF_FLAG_SHOW_SIGN) { |
| 344 | sign = '+'; |
| 345 | } |
| 346 | else |
| 347 | if (flags & PF_FLAG_SPACE_SIGN) { |
| 348 | sign = ' '; |
| 349 | } |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 350 | |
stijn | 861670b | 2015-05-16 10:54:19 +0200 | [diff] [blame] | 351 | int len = mp_format_float(f, buf, sizeof(buf), fmt, prec, sign); |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 352 | if (len < 0) { |
| 353 | len = 0; |
| 354 | } |
stijn | 861670b | 2015-05-16 10:54:19 +0200 | [diff] [blame] | 355 | |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 356 | char *s = buf; |
| 357 | |
| 358 | if ((flags & PF_FLAG_ADD_PERCENT) && (size_t)(len + 1) < sizeof(buf)) { |
| 359 | buf[len++] = '%'; |
| 360 | buf[len] = '\0'; |
| 361 | } |
| 362 | |
| 363 | // buf[0] < '0' returns true if the first character is space, + or - |
| 364 | if ((flags & PF_FLAG_PAD_AFTER_SIGN) && buf[0] < '0') { |
| 365 | // We have a sign character |
| 366 | s++; |
Damien George | 79474c6 | 2015-05-28 14:22:12 +0000 | [diff] [blame] | 367 | chrs += mp_print_strn(print, &buf[0], 1, 0, 0, 1); |
| 368 | width--; |
| 369 | len--; |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 372 | chrs += mp_print_strn(print, s, len, flags, fill, width); |
| 373 | |
| 374 | return chrs; |
| 375 | } |
| 376 | #endif |
| 377 | |
| 378 | int mp_printf(const mp_print_t *print, const char *fmt, ...) { |
| 379 | va_list ap; |
| 380 | va_start(ap, fmt); |
| 381 | int ret = mp_vprintf(print, fmt, ap); |
| 382 | va_end(ap); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) { |
| 387 | int chrs = 0; |
| 388 | for (;;) { |
| 389 | { |
| 390 | const char *f = fmt; |
| 391 | while (*f != '\0' && *f != '%') { |
| 392 | ++f; // XXX UTF8 advance char |
| 393 | } |
| 394 | if (f > fmt) { |
| 395 | print->print_strn(print->data, fmt, f - fmt); |
| 396 | chrs += f - fmt; |
| 397 | fmt = f; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (*fmt == '\0') { |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | // move past % character |
| 406 | ++fmt; |
| 407 | |
| 408 | // parse flags, if they exist |
| 409 | int flags = 0; |
| 410 | char fill = ' '; |
| 411 | while (*fmt != '\0') { |
| 412 | if (*fmt == '-') flags |= PF_FLAG_LEFT_ADJUST; |
| 413 | else if (*fmt == '+') flags |= PF_FLAG_SHOW_SIGN; |
| 414 | else if (*fmt == ' ') flags |= PF_FLAG_SPACE_SIGN; |
| 415 | else if (*fmt == '!') flags |= PF_FLAG_NO_TRAILZ; |
| 416 | else if (*fmt == '0') { |
| 417 | flags |= PF_FLAG_PAD_AFTER_SIGN; |
| 418 | fill = '0'; |
| 419 | } else break; |
| 420 | ++fmt; |
| 421 | } |
| 422 | |
| 423 | // parse width, if it exists |
| 424 | int width = 0; |
| 425 | for (; '0' <= *fmt && *fmt <= '9'; ++fmt) { |
| 426 | width = width * 10 + *fmt - '0'; |
| 427 | } |
| 428 | |
| 429 | // parse precision, if it exists |
| 430 | int prec = -1; |
| 431 | if (*fmt == '.') { |
| 432 | ++fmt; |
| 433 | if (*fmt == '*') { |
| 434 | ++fmt; |
| 435 | prec = va_arg(args, int); |
| 436 | } else { |
| 437 | prec = 0; |
| 438 | for (; '0' <= *fmt && *fmt <= '9'; ++fmt) { |
| 439 | prec = prec * 10 + *fmt - '0'; |
| 440 | } |
| 441 | } |
| 442 | if (prec < 0) { |
| 443 | prec = 0; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // parse long specifiers (current not used) |
| 448 | //bool long_arg = false; |
| 449 | if (*fmt == 'l') { |
| 450 | ++fmt; |
| 451 | //long_arg = true; |
| 452 | } |
| 453 | |
| 454 | if (*fmt == '\0') { |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | switch (*fmt) { |
| 459 | case 'b': |
| 460 | if (va_arg(args, int)) { |
| 461 | chrs += mp_print_strn(print, "true", 4, flags, fill, width); |
| 462 | } else { |
| 463 | chrs += mp_print_strn(print, "false", 5, flags, fill, width); |
| 464 | } |
| 465 | break; |
| 466 | case 'c': |
| 467 | { |
| 468 | char str = va_arg(args, int); |
| 469 | chrs += mp_print_strn(print, &str, 1, flags, fill, width); |
| 470 | break; |
| 471 | } |
Damien George | 044c473 | 2015-04-11 13:03:37 +0100 | [diff] [blame] | 472 | case 'q': |
| 473 | { |
| 474 | qstr qst = va_arg(args, qstr); |
| 475 | mp_uint_t len; |
| 476 | const char *str = (const char*)qstr_data(qst, &len); |
| 477 | if (prec < 0) { |
| 478 | prec = len; |
| 479 | } |
| 480 | chrs += mp_print_strn(print, str, prec, flags, fill, width); |
| 481 | break; |
| 482 | } |
Damien George | 7f9d1d6 | 2015-04-09 23:56:15 +0100 | [diff] [blame] | 483 | case 's': |
| 484 | { |
| 485 | const char *str = va_arg(args, const char*); |
| 486 | if (str) { |
| 487 | if (prec < 0) { |
| 488 | prec = strlen(str); |
| 489 | } |
| 490 | chrs += mp_print_strn(print, str, prec, flags, fill, width); |
| 491 | } else { |
| 492 | chrs += mp_print_strn(print, "(null)", 6, flags, fill, width); |
| 493 | } |
| 494 | break; |
| 495 | } |
| 496 | case 'u': |
| 497 | chrs += mp_print_int(print, va_arg(args, int), 0, 10, 'a', flags, fill, width); |
| 498 | break; |
| 499 | case 'd': |
| 500 | chrs += mp_print_int(print, va_arg(args, int), 1, 10, 'a', flags, fill, width); |
| 501 | break; |
| 502 | case 'x': |
| 503 | chrs += mp_print_int(print, va_arg(args, int), 0, 16, 'a', flags, fill, width); |
| 504 | break; |
| 505 | case 'X': |
| 506 | chrs += mp_print_int(print, va_arg(args, int), 0, 16, 'A', flags, fill, width); |
| 507 | break; |
| 508 | case 'p': |
| 509 | case 'P': // don't bother to handle upcase for 'P' |
| 510 | chrs += mp_print_int(print, va_arg(args, unsigned int), 0, 16, 'a', flags, fill, width); |
| 511 | break; |
| 512 | #if MICROPY_PY_BUILTINS_FLOAT |
| 513 | case 'e': |
| 514 | case 'E': |
| 515 | case 'f': |
| 516 | case 'F': |
| 517 | case 'g': |
| 518 | case 'G': |
| 519 | { |
| 520 | #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT |
| 521 | mp_float_t f = va_arg(args, double); |
| 522 | chrs += mp_print_float(print, f, *fmt, flags, fill, width, prec); |
| 523 | #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE |
| 524 | // Currently mp_print_float uses snprintf, but snprintf |
| 525 | // itself may be implemented in terms of mp_vprintf() for |
| 526 | // some ports. So, for extra caution, this case is handled |
| 527 | // with assert below. Note that currently ports which |
| 528 | // use MICROPY_FLOAT_IMPL_DOUBLE, don't call mp_vprintf() |
| 529 | // with float format specifier at all. |
| 530 | // TODO: resolve this completely |
| 531 | assert(0); |
| 532 | //#error Calling mp_print_float with double not supported from within printf |
| 533 | #else |
| 534 | #error Unknown MICROPY FLOAT IMPL |
| 535 | #endif |
| 536 | break; |
| 537 | } |
| 538 | #endif |
| 539 | default: |
| 540 | print->print_strn(print->data, fmt, 1); |
| 541 | chrs += 1; |
| 542 | break; |
| 543 | } |
| 544 | ++fmt; |
| 545 | } |
| 546 | return chrs; |
| 547 | } |