More printf stuff for python

This commit is contained in:
K. Lange 2018-06-25 19:42:07 +09:00
parent 92fec3ece8
commit a97772d208
1 changed files with 27 additions and 4 deletions

View File

@ -100,6 +100,7 @@ size_t xvasprintf(char * buf, const char * fmt, va_list args) {
int i = 0; int i = 0;
char * s; char * s;
char * b = buf; char * b = buf;
int precision = 0;
for (const char *f = fmt; *f; f++) { for (const char *f = fmt; *f; f++) {
if (*f != '%') { if (*f != '%') {
*b++ = *f; *b++ = *f;
@ -127,6 +128,15 @@ size_t xvasprintf(char * buf, const char * fmt, va_list args) {
arg_width += *f - '0'; arg_width += *f - '0';
++f; ++f;
} }
if (*f == '.') {
++f;
precision = 0;
while (*f >= '0' && *f <= '9') {
precision *= 10;
precision += *f - '0';
++f;
}
}
if (*f == 'l') { if (*f == 'l') {
big = 1; big = 1;
++f; ++f;
@ -152,12 +162,21 @@ size_t xvasprintf(char * buf, const char * fmt, va_list args) {
if (s == NULL) { if (s == NULL) {
s = "(null)"; s = "(null)";
} }
if (precision > 0) {
while (*s && precision > 0) {
*b++ = *s++;
count++;
precision--;
if (arg_width && count == arg_width) break;
}
} else {
while (*s) { while (*s) {
*b++ = *s++; *b++ = *s++;
count++; count++;
if (arg_width && count == arg_width) break; if (arg_width && count == arg_width) break;
} }
} }
}
while (count < arg_width) { while (count < arg_width) {
*b++ = ' '; *b++ = ' ';
count++; count++;
@ -167,6 +186,10 @@ size_t xvasprintf(char * buf, const char * fmt, va_list args) {
case 'c': /* Single character */ case 'c': /* Single character */
*b++ = (char)va_arg(args, int); *b++ = (char)va_arg(args, int);
break; break;
case 'p':
if (!arg_width) {
arg_width = 8;
}
case 'x': /* Hexadecimal number */ case 'x': /* Hexadecimal number */
i = b - buf; i = b - buf;
print_hex((unsigned long)va_arg(args, unsigned long), arg_width, buf, &i); print_hex((unsigned long)va_arg(args, unsigned long), arg_width, buf, &i);