summaryrefslogtreecommitdiff
path: root/src/user/knob/format.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/knob/format.c')
-rw-r--r--src/user/knob/format.c52
1 files changed, 32 insertions, 20 deletions
diff --git a/src/user/knob/format.c b/src/user/knob/format.c
index c5d2447..cdc1687 100644
--- a/src/user/knob/format.c
+++ b/src/user/knob/format.c
@@ -42,8 +42,8 @@ struct format_spec {
CHAR,
STRING,
UNSIGNED_DECIMAL,
- HEXADECIMAL
- //TODO: signed decimal
+ HEXADECIMAL,
+ SIGNED_DECIMAL
} kind;
};
@@ -73,6 +73,9 @@ static const char *get_format(const char *from, struct format_spec *format_out)
case 'x':
format_out->kind = HEXADECIMAL;
break;
+ case 'd':
+ format_out->kind = SIGNED_DECIMAL;
+ break;
default:
format_out->kind = UNKNOWN;
break;
@@ -81,7 +84,17 @@ static const char *get_format(const char *from, struct format_spec *format_out)
return from + 1;
}
-//char debug[] = "-- format_v: fmt = \" \"...";
+static uint8_t get_digits(uint32_t k) {
+ if (k >= 1000000000)
+ return 10;
+ uint8_t r = 1;
+ uint32_t n = 10;
+ while (k >= n) {
+ ++r;
+ n *= 10;
+ }
+ return r;
+}
//allocates new memory
char *format_v(const char *fmt, va_list args) {
@@ -92,11 +105,6 @@ char *format_v(const char *fmt, va_list args) {
buf_i = buf;
while (*fmt) {
- //debug[20] = *fmt;
- //debug[21] = fmt[1];
- //debug[22] = fmt[2];
- //_system_log(debug);
-
if (*fmt != '%') {
ensure(1);
*(buf_i++) = *(fmt++);
@@ -137,20 +145,24 @@ char *format_v(const char *fmt, va_list args) {
continue;
uint32_t k;
- case UNSIGNED_DECIMAL:
+ case SIGNED_DECIMAL:
k = va_arg(args, uint32_t);
- if (!form.len) {
- if (k >= 1000000000)
- form.len = 10;
- else {
- uint32_t n = 10;
- ++form.len;
- while (k >= n) {
- ++form.len;
- n *= 10;
- }
- }
+ bool is_neg = k & 0x80000000;
+ if (is_neg) {
+ ensure(1);
+ *(buf_i++) = '-';
+ k = -k;
}
+ if (!form.len)
+ form.len = get_digits(k);
+ else if (is_neg)
+ --form.len;
+ goto print_dec;
+ case UNSIGNED_DECIMAL:
+ k = va_arg(args, uint32_t);
+ if (!form.len)
+ form.len = get_digits(k);
+ print_dec:
ensure(form.len);
const uint32_t len_backup = form.len;
while (form.len--) {