2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2015-04-09 18:56:15 -04:00
|
|
|
* Copyright (c) 2013-2015 Damien P. George
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
2014-03-31 00:06:50 -04:00
|
|
|
#include <stdint.h>
|
2015-04-09 18:56:15 -04:00
|
|
|
#include <stdio.h>
|
2014-03-31 00:06:50 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-10-30 19:03:58 -04:00
|
|
|
#include "py/mphal.h"
|
2015-04-09 18:56:15 -04:00
|
|
|
#include "py/mpprint.h"
|
|
|
|
#include "py/obj.h"
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/objint.h"
|
2015-04-09 18:56:15 -04:00
|
|
|
#include "py/runtime.h"
|
2014-03-31 00:06:50 -04:00
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/formatfloat.h"
|
2014-03-31 00:06:50 -04:00
|
|
|
#endif
|
|
|
|
|
2014-04-01 14:59:31 -04:00
|
|
|
static const char pad_spaces[] = " ";
|
|
|
|
static const char pad_zeroes[] = "0000000000000000";
|
2014-03-31 00:06:50 -04:00
|
|
|
|
2015-11-27 07:19:25 -05:00
|
|
|
STATIC void plat_print_strn(void *env, const char *str, size_t len) {
|
2015-04-09 18:56:15 -04:00
|
|
|
(void)env;
|
|
|
|
MP_PLAT_PRINT_STRN(str, len);
|
2014-03-31 00:06:50 -04:00
|
|
|
}
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
const mp_print_t mp_plat_print = {NULL, plat_print_strn};
|
|
|
|
|
|
|
|
int mp_print_str(const mp_print_t *print, const char *str) {
|
2015-11-27 07:19:25 -05:00
|
|
|
size_t len = strlen(str);
|
2015-04-09 18:56:15 -04:00
|
|
|
if (len) {
|
|
|
|
print->print_strn(print->data, str, len);
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-11-27 07:19:25 -05:00
|
|
|
int mp_print_strn(const mp_print_t *print, const char *str, size_t len, int flags, char fill, int width) {
|
2014-03-31 00:06:50 -04:00
|
|
|
int left_pad = 0;
|
|
|
|
int right_pad = 0;
|
|
|
|
int pad = width - len;
|
2014-04-01 14:59:31 -04:00
|
|
|
int pad_size;
|
2014-04-17 13:58:09 -04:00
|
|
|
int total_chars_printed = 0;
|
2014-03-31 00:06:50 -04:00
|
|
|
const char *pad_chars;
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
if (!fill || fill == ' ') {
|
2014-03-31 00:06:50 -04:00
|
|
|
pad_chars = pad_spaces;
|
2014-04-01 14:59:31 -04:00
|
|
|
pad_size = sizeof(pad_spaces) - 1;
|
2014-03-31 00:06:50 -04:00
|
|
|
} else if (fill == '0') {
|
|
|
|
pad_chars = pad_zeroes;
|
2014-04-01 14:59:31 -04:00
|
|
|
pad_size = sizeof(pad_zeroes) - 1;
|
2014-03-31 00:06:50 -04:00
|
|
|
} else {
|
2014-04-01 14:59:31 -04:00
|
|
|
// Other pad characters are fairly unusual, so we'll take the hit
|
|
|
|
// and output them 1 at a time.
|
|
|
|
pad_chars = &fill;
|
|
|
|
pad_size = 1;
|
2014-03-31 00:06:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & PF_FLAG_CENTER_ADJUST) {
|
|
|
|
left_pad = pad / 2;
|
|
|
|
right_pad = pad - left_pad;
|
|
|
|
} else if (flags & PF_FLAG_LEFT_ADJUST) {
|
|
|
|
right_pad = pad;
|
|
|
|
} else {
|
|
|
|
left_pad = pad;
|
|
|
|
}
|
|
|
|
|
2014-04-17 13:58:09 -04:00
|
|
|
if (left_pad > 0) {
|
|
|
|
total_chars_printed += left_pad;
|
2014-03-31 00:06:50 -04:00
|
|
|
while (left_pad > 0) {
|
|
|
|
int p = left_pad;
|
2014-04-01 16:15:03 -04:00
|
|
|
if (p > pad_size) {
|
2014-04-01 14:59:31 -04:00
|
|
|
p = pad_size;
|
2014-04-01 16:15:03 -04:00
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
print->print_strn(print->data, pad_chars, p);
|
2014-03-31 00:06:50 -04:00
|
|
|
left_pad -= p;
|
|
|
|
}
|
|
|
|
}
|
2014-06-06 02:09:02 -04:00
|
|
|
if (len) {
|
2015-04-09 18:56:15 -04:00
|
|
|
print->print_strn(print->data, str, len);
|
2014-06-06 02:09:02 -04:00
|
|
|
total_chars_printed += len;
|
|
|
|
}
|
2014-04-17 13:58:09 -04:00
|
|
|
if (right_pad > 0) {
|
|
|
|
total_chars_printed += right_pad;
|
2014-03-31 00:06:50 -04:00
|
|
|
while (right_pad > 0) {
|
|
|
|
int p = right_pad;
|
2014-04-01 16:15:03 -04:00
|
|
|
if (p > pad_size) {
|
2014-04-01 14:59:31 -04:00
|
|
|
p = pad_size;
|
2014-04-01 16:15:03 -04:00
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
print->print_strn(print->data, pad_chars, p);
|
2014-03-31 00:06:50 -04:00
|
|
|
right_pad -= p;
|
|
|
|
}
|
|
|
|
}
|
2014-04-17 13:58:09 -04:00
|
|
|
return total_chars_printed;
|
2014-03-31 00:06:50 -04:00
|
|
|
}
|
|
|
|
|
2014-04-01 14:01:55 -04:00
|
|
|
// 32-bits is 10 digits, add 3 for commas, 1 for sign, 1 for terminating null
|
|
|
|
// We can use 16 characters for 32-bit and 32 characters for 64-bit
|
2014-07-03 08:25:24 -04:00
|
|
|
#define INT_BUF_SIZE (sizeof(mp_int_t) * 4)
|
2014-03-31 00:06:50 -04:00
|
|
|
|
2015-05-28 09:54:56 -04:00
|
|
|
// Our mp_vprintf function below does not support the '#' format modifier to
|
|
|
|
// print the prefix of a non-base-10 number, so we don't need code for this.
|
|
|
|
#define SUPPORT_INT_BASE_PREFIX (0)
|
|
|
|
|
|
|
|
// This function is used exclusively by mp_vprintf to format ints.
|
2015-04-09 18:56:15 -04:00
|
|
|
// It needs to be a separate function to mp_print_mp_int, since converting to a mp_int looses the MSB.
|
2015-05-28 09:54:56 -04:00
|
|
|
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) {
|
2014-04-08 17:10:37 -04:00
|
|
|
char sign = 0;
|
|
|
|
if (sgn) {
|
2014-07-03 08:25:24 -04:00
|
|
|
if ((mp_int_t)x < 0) {
|
2014-04-08 17:10:37 -04:00
|
|
|
sign = '-';
|
|
|
|
x = -x;
|
|
|
|
} else if (flags & PF_FLAG_SHOW_SIGN) {
|
|
|
|
sign = '+';
|
|
|
|
} else if (flags & PF_FLAG_SPACE_SIGN) {
|
|
|
|
sign = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[INT_BUF_SIZE];
|
|
|
|
char *b = buf + INT_BUF_SIZE;
|
|
|
|
|
|
|
|
if (x == 0) {
|
|
|
|
*(--b) = '0';
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
int c = x % base;
|
|
|
|
x /= base;
|
|
|
|
if (c >= 10) {
|
|
|
|
c += base_char - 10;
|
|
|
|
} else {
|
|
|
|
c += '0';
|
|
|
|
}
|
|
|
|
*(--b) = c;
|
|
|
|
} while (b > buf && x != 0);
|
|
|
|
}
|
|
|
|
|
2015-05-28 09:54:56 -04:00
|
|
|
#if SUPPORT_INT_BASE_PREFIX
|
2014-04-08 17:10:37 -04:00
|
|
|
char prefix_char = '\0';
|
|
|
|
|
|
|
|
if (flags & PF_FLAG_SHOW_PREFIX) {
|
|
|
|
if (base == 2) {
|
|
|
|
prefix_char = base_char + 'b' - 'a';
|
|
|
|
} else if (base == 8) {
|
|
|
|
prefix_char = base_char + 'o' - 'a';
|
|
|
|
} else if (base == 16) {
|
|
|
|
prefix_char = base_char + 'x' - 'a';
|
|
|
|
}
|
|
|
|
}
|
2015-05-28 09:54:56 -04:00
|
|
|
#endif
|
2014-04-08 17:10:37 -04:00
|
|
|
|
|
|
|
int len = 0;
|
|
|
|
if (flags & PF_FLAG_PAD_AFTER_SIGN) {
|
|
|
|
if (sign) {
|
2015-04-09 18:56:15 -04:00
|
|
|
len += mp_print_strn(print, &sign, 1, flags, fill, 1);
|
2014-04-08 17:10:37 -04:00
|
|
|
width--;
|
|
|
|
}
|
2015-05-28 09:54:56 -04:00
|
|
|
#if SUPPORT_INT_BASE_PREFIX
|
2014-04-08 17:10:37 -04:00
|
|
|
if (prefix_char) {
|
2015-04-09 18:56:15 -04:00
|
|
|
len += mp_print_strn(print, "0", 1, flags, fill, 1);
|
|
|
|
len += mp_print_strn(print, &prefix_char, 1, flags, fill, 1);
|
2014-04-08 17:10:37 -04:00
|
|
|
width -= 2;
|
|
|
|
}
|
2015-05-28 09:54:56 -04:00
|
|
|
#endif
|
2014-04-08 17:10:37 -04:00
|
|
|
} else {
|
2015-05-28 09:54:56 -04:00
|
|
|
#if SUPPORT_INT_BASE_PREFIX
|
2014-04-08 17:10:37 -04:00
|
|
|
if (prefix_char && b > &buf[1]) {
|
|
|
|
*(--b) = prefix_char;
|
|
|
|
*(--b) = '0';
|
|
|
|
}
|
2015-05-28 09:54:56 -04:00
|
|
|
#endif
|
2014-04-08 17:10:37 -04:00
|
|
|
if (sign && b > buf) {
|
|
|
|
*(--b) = sign;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
len += mp_print_strn(print, b, buf + INT_BUF_SIZE - b, flags, fill, width);
|
2014-04-08 17:10:37 -04:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
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) {
|
2014-04-07 14:19:51 -04:00
|
|
|
if (!MP_OBJ_IS_INT(x)) {
|
|
|
|
// This will convert booleans to int, or raise an error for
|
|
|
|
// non-integer types.
|
|
|
|
x = MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(x));
|
2014-03-31 00:06:50 -04:00
|
|
|
}
|
|
|
|
|
2014-06-06 02:09:02 -04:00
|
|
|
if ((flags & (PF_FLAG_LEFT_ADJUST | PF_FLAG_CENTER_ADJUST)) == 0 && fill == '0') {
|
|
|
|
if (prec > width) {
|
|
|
|
width = prec;
|
|
|
|
}
|
|
|
|
prec = 0;
|
|
|
|
}
|
2014-04-07 14:19:51 -04:00
|
|
|
char prefix_buf[4];
|
|
|
|
char *prefix = prefix_buf;
|
2014-03-31 00:06:50 -04:00
|
|
|
|
2016-01-07 09:29:12 -05:00
|
|
|
if (mp_obj_int_sign(x) > 0) {
|
2014-04-07 14:19:51 -04:00
|
|
|
if (flags & PF_FLAG_SHOW_SIGN) {
|
|
|
|
*prefix++ = '+';
|
|
|
|
} else if (flags & PF_FLAG_SPACE_SIGN) {
|
|
|
|
*prefix++ = ' ';
|
|
|
|
}
|
2014-03-31 00:06:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & PF_FLAG_SHOW_PREFIX) {
|
|
|
|
if (base == 2) {
|
2014-04-07 14:19:51 -04:00
|
|
|
*prefix++ = '0';
|
|
|
|
*prefix++ = base_char + 'b' - 'a';
|
2014-03-31 00:06:50 -04:00
|
|
|
} else if (base == 8) {
|
2014-04-07 14:19:51 -04:00
|
|
|
*prefix++ = '0';
|
|
|
|
if (flags & PF_FLAG_SHOW_OCTAL_LETTER) {
|
|
|
|
*prefix++ = base_char + 'o' - 'a';
|
|
|
|
}
|
2014-03-31 00:06:50 -04:00
|
|
|
} else if (base == 16) {
|
2014-04-07 14:19:51 -04:00
|
|
|
*prefix++ = '0';
|
|
|
|
*prefix++ = base_char + 'x' - 'a';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*prefix = '\0';
|
|
|
|
int prefix_len = prefix - prefix_buf;
|
|
|
|
prefix = prefix_buf;
|
|
|
|
|
|
|
|
char comma = '\0';
|
|
|
|
if (flags & PF_FLAG_SHOW_COMMA) {
|
|
|
|
comma = ',';
|
|
|
|
}
|
|
|
|
|
|
|
|
// The size of this buffer is rather arbitrary. If it's not large
|
|
|
|
// enough, a dynamic one will be allocated.
|
2014-07-03 08:25:24 -04:00
|
|
|
char stack_buf[sizeof(mp_int_t) * 4];
|
2014-04-07 14:19:51 -04:00
|
|
|
char *buf = stack_buf;
|
2014-10-03 13:44:14 -04:00
|
|
|
mp_uint_t buf_size = sizeof(stack_buf);
|
|
|
|
mp_uint_t fmt_size = 0;
|
2014-04-07 14:19:51 -04:00
|
|
|
char *str;
|
|
|
|
|
2014-06-06 02:09:02 -04:00
|
|
|
if (prec > 1) {
|
|
|
|
flags |= PF_FLAG_PAD_AFTER_SIGN;
|
|
|
|
}
|
2014-04-07 14:19:51 -04:00
|
|
|
char sign = '\0';
|
|
|
|
if (flags & PF_FLAG_PAD_AFTER_SIGN) {
|
|
|
|
// We add the pad in this function, so since the pad goes after
|
|
|
|
// the sign & prefix, we format without a prefix
|
|
|
|
str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size,
|
|
|
|
x, base, NULL, base_char, comma);
|
|
|
|
if (*str == '-') {
|
|
|
|
sign = *str++;
|
|
|
|
fmt_size--;
|
2014-03-31 00:06:50 -04:00
|
|
|
}
|
2014-04-07 14:19:51 -04:00
|
|
|
} else {
|
|
|
|
str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size,
|
|
|
|
x, base, prefix, base_char, comma);
|
2014-03-31 00:06:50 -04:00
|
|
|
}
|
|
|
|
|
2014-06-06 02:09:02 -04:00
|
|
|
int spaces_before = 0;
|
|
|
|
int spaces_after = 0;
|
|
|
|
|
|
|
|
if (prec > 1) {
|
|
|
|
// If prec was specified, then prec specifies the width to zero-pad the
|
|
|
|
// the number to. This zero-padded number then gets left or right
|
|
|
|
// aligned in width characters.
|
|
|
|
|
|
|
|
int prec_width = fmt_size; // The digits
|
|
|
|
if (prec_width < prec) {
|
|
|
|
prec_width = prec;
|
|
|
|
}
|
|
|
|
if (flags & PF_FLAG_PAD_AFTER_SIGN) {
|
|
|
|
if (sign) {
|
|
|
|
prec_width++;
|
|
|
|
}
|
|
|
|
prec_width += prefix_len;
|
|
|
|
}
|
|
|
|
if (prec_width < width) {
|
|
|
|
if (flags & PF_FLAG_LEFT_ADJUST) {
|
|
|
|
spaces_after = width - prec_width;
|
|
|
|
} else {
|
|
|
|
spaces_before = width - prec_width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fill = '0';
|
|
|
|
flags &= ~PF_FLAG_LEFT_ADJUST;
|
|
|
|
}
|
|
|
|
|
2014-03-31 00:06:50 -04:00
|
|
|
int len = 0;
|
2014-06-06 02:09:02 -04:00
|
|
|
if (spaces_before) {
|
2015-04-09 18:56:15 -04:00
|
|
|
len += mp_print_strn(print, "", 0, 0, ' ', spaces_before);
|
2014-06-06 02:09:02 -04:00
|
|
|
}
|
2014-03-31 00:06:50 -04:00
|
|
|
if (flags & PF_FLAG_PAD_AFTER_SIGN) {
|
2014-04-07 14:19:51 -04:00
|
|
|
// pad after sign implies pad after prefix as well.
|
2014-03-31 00:06:50 -04:00
|
|
|
if (sign) {
|
2015-04-09 18:56:15 -04:00
|
|
|
len += mp_print_strn(print, &sign, 1, 0, 0, 1);
|
2014-03-31 00:06:50 -04:00
|
|
|
width--;
|
|
|
|
}
|
2014-04-07 14:19:51 -04:00
|
|
|
if (prefix_len) {
|
2015-04-09 18:56:15 -04:00
|
|
|
len += mp_print_strn(print, prefix, prefix_len, 0, 0, 1);
|
2014-04-07 14:19:51 -04:00
|
|
|
width -= prefix_len;
|
2014-03-31 00:06:50 -04:00
|
|
|
}
|
|
|
|
}
|
2014-06-06 02:09:02 -04:00
|
|
|
if (prec > 1) {
|
|
|
|
width = prec;
|
|
|
|
}
|
2014-03-31 00:06:50 -04:00
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
len += mp_print_strn(print, str, fmt_size, flags, fill, width);
|
2014-04-07 14:19:51 -04:00
|
|
|
|
2014-06-06 02:09:02 -04:00
|
|
|
if (spaces_after) {
|
2015-04-09 18:56:15 -04:00
|
|
|
len += mp_print_strn(print, "", 0, 0, ' ', spaces_after);
|
2014-06-06 02:09:02 -04:00
|
|
|
}
|
|
|
|
|
2014-04-07 14:19:51 -04:00
|
|
|
if (buf != stack_buf) {
|
2015-02-27 04:34:51 -05:00
|
|
|
m_del(char, buf, buf_size);
|
2014-04-07 14:19:51 -04:00
|
|
|
}
|
2014-03-31 00:06:50 -04:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2015-04-09 18:56:15 -04:00
|
|
|
int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, char fill, int width, int prec) {
|
2014-03-31 00:06:50 -04:00
|
|
|
char buf[32];
|
|
|
|
char sign = '\0';
|
|
|
|
int chrs = 0;
|
|
|
|
|
|
|
|
if (flags & PF_FLAG_SHOW_SIGN) {
|
|
|
|
sign = '+';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (flags & PF_FLAG_SPACE_SIGN) {
|
|
|
|
sign = ' ';
|
|
|
|
}
|
|
|
|
|
2015-05-16 04:54:19 -04:00
|
|
|
int len = mp_format_float(f, buf, sizeof(buf), fmt, prec, sign);
|
2015-01-16 12:47:07 -05:00
|
|
|
if (len < 0) {
|
|
|
|
len = 0;
|
|
|
|
}
|
2015-05-16 04:54:19 -04:00
|
|
|
|
2014-03-31 00:06:50 -04:00
|
|
|
char *s = buf;
|
|
|
|
|
2015-01-16 12:47:07 -05:00
|
|
|
if ((flags & PF_FLAG_ADD_PERCENT) && (size_t)(len + 1) < sizeof(buf)) {
|
2014-03-31 00:06:50 -04:00
|
|
|
buf[len++] = '%';
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
// buf[0] < '0' returns true if the first character is space, + or -
|
|
|
|
if ((flags & PF_FLAG_PAD_AFTER_SIGN) && buf[0] < '0') {
|
|
|
|
// We have a sign character
|
|
|
|
s++;
|
2015-05-28 10:22:12 -04:00
|
|
|
chrs += mp_print_strn(print, &buf[0], 1, 0, 0, 1);
|
|
|
|
width--;
|
|
|
|
len--;
|
2014-03-31 00:06:50 -04:00
|
|
|
}
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
chrs += mp_print_strn(print, s, len, flags, fill, width);
|
2014-03-31 00:06:50 -04:00
|
|
|
|
|
|
|
return chrs;
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-09 18:56:15 -04:00
|
|
|
|
|
|
|
int mp_printf(const mp_print_t *print, const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int ret = mp_vprintf(print, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
|
|
|
|
int chrs = 0;
|
|
|
|
for (;;) {
|
|
|
|
{
|
|
|
|
const char *f = fmt;
|
|
|
|
while (*f != '\0' && *f != '%') {
|
|
|
|
++f; // XXX UTF8 advance char
|
|
|
|
}
|
|
|
|
if (f > fmt) {
|
|
|
|
print->print_strn(print->data, fmt, f - fmt);
|
|
|
|
chrs += f - fmt;
|
|
|
|
fmt = f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*fmt == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move past % character
|
|
|
|
++fmt;
|
|
|
|
|
|
|
|
// parse flags, if they exist
|
|
|
|
int flags = 0;
|
|
|
|
char fill = ' ';
|
|
|
|
while (*fmt != '\0') {
|
|
|
|
if (*fmt == '-') flags |= PF_FLAG_LEFT_ADJUST;
|
|
|
|
else if (*fmt == '+') flags |= PF_FLAG_SHOW_SIGN;
|
|
|
|
else if (*fmt == ' ') flags |= PF_FLAG_SPACE_SIGN;
|
|
|
|
else if (*fmt == '!') flags |= PF_FLAG_NO_TRAILZ;
|
|
|
|
else if (*fmt == '0') {
|
|
|
|
flags |= PF_FLAG_PAD_AFTER_SIGN;
|
|
|
|
fill = '0';
|
|
|
|
} else break;
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse width, if it exists
|
|
|
|
int width = 0;
|
|
|
|
for (; '0' <= *fmt && *fmt <= '9'; ++fmt) {
|
|
|
|
width = width * 10 + *fmt - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse precision, if it exists
|
|
|
|
int prec = -1;
|
|
|
|
if (*fmt == '.') {
|
|
|
|
++fmt;
|
|
|
|
if (*fmt == '*') {
|
|
|
|
++fmt;
|
|
|
|
prec = va_arg(args, int);
|
|
|
|
} else {
|
|
|
|
prec = 0;
|
|
|
|
for (; '0' <= *fmt && *fmt <= '9'; ++fmt) {
|
|
|
|
prec = prec * 10 + *fmt - '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (prec < 0) {
|
|
|
|
prec = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse long specifiers (current not used)
|
|
|
|
//bool long_arg = false;
|
|
|
|
if (*fmt == 'l') {
|
|
|
|
++fmt;
|
|
|
|
//long_arg = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*fmt == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*fmt) {
|
|
|
|
case 'b':
|
|
|
|
if (va_arg(args, int)) {
|
|
|
|
chrs += mp_print_strn(print, "true", 4, flags, fill, width);
|
|
|
|
} else {
|
|
|
|
chrs += mp_print_strn(print, "false", 5, flags, fill, width);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
{
|
|
|
|
char str = va_arg(args, int);
|
|
|
|
chrs += mp_print_strn(print, &str, 1, flags, fill, width);
|
|
|
|
break;
|
|
|
|
}
|
2015-04-11 08:03:37 -04:00
|
|
|
case 'q':
|
|
|
|
{
|
|
|
|
qstr qst = va_arg(args, qstr);
|
2015-11-27 07:23:18 -05:00
|
|
|
size_t len;
|
2015-04-11 08:03:37 -04:00
|
|
|
const char *str = (const char*)qstr_data(qst, &len);
|
|
|
|
if (prec < 0) {
|
|
|
|
prec = len;
|
|
|
|
}
|
|
|
|
chrs += mp_print_strn(print, str, prec, flags, fill, width);
|
|
|
|
break;
|
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
const char *str = va_arg(args, const char*);
|
|
|
|
if (str) {
|
|
|
|
if (prec < 0) {
|
|
|
|
prec = strlen(str);
|
|
|
|
}
|
|
|
|
chrs += mp_print_strn(print, str, prec, flags, fill, width);
|
|
|
|
} else {
|
|
|
|
chrs += mp_print_strn(print, "(null)", 6, flags, fill, width);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'u':
|
|
|
|
chrs += mp_print_int(print, va_arg(args, int), 0, 10, 'a', flags, fill, width);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
chrs += mp_print_int(print, va_arg(args, int), 1, 10, 'a', flags, fill, width);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
chrs += mp_print_int(print, va_arg(args, int), 0, 16, 'a', flags, fill, width);
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
chrs += mp_print_int(print, va_arg(args, int), 0, 16, 'A', flags, fill, width);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
case 'P': // don't bother to handle upcase for 'P'
|
|
|
|
chrs += mp_print_int(print, va_arg(args, unsigned int), 0, 16, 'a', flags, fill, width);
|
|
|
|
break;
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
{
|
2015-11-30 10:00:41 -05:00
|
|
|
#if ((MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT) || (MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE))
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_float_t f = va_arg(args, double);
|
|
|
|
chrs += mp_print_float(print, f, *fmt, flags, fill, width, prec);
|
|
|
|
#else
|
|
|
|
#error Unknown MICROPY FLOAT IMPL
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2015-12-17 06:41:10 -05:00
|
|
|
// Because 'l' is eaten above, another 'l' means %ll. We need to support
|
|
|
|
// this length specifier for OBJ_REPR_D (64-bit NaN boxing).
|
|
|
|
// TODO Either enable this unconditionally, or provide a specific config var.
|
2015-12-18 04:20:33 -05:00
|
|
|
#if (MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D) || defined(_WIN64)
|
2015-12-17 06:41:10 -05:00
|
|
|
case 'l': {
|
|
|
|
unsigned long long int arg_value = va_arg(args, unsigned long long int);
|
|
|
|
++fmt;
|
|
|
|
if (*fmt == 'u' || *fmt == 'd') {
|
|
|
|
chrs += mp_print_int(print, arg_value, *fmt == 'd', 10, 'a', flags, fill, width);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through to default case to print unknown format char
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-09 18:56:15 -04:00
|
|
|
default:
|
|
|
|
print->print_strn(print->data, fmt, 1);
|
|
|
|
chrs += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
return chrs;
|
|
|
|
}
|