diff --git a/builtin/cat-file.c b/builtin/cat-file.c index fa45f774d7..5c47611b4a 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -344,12 +344,12 @@ static int expand_atom(struct strbuf *sb, const char *atom, int len, if (data->mark_query) data->info.sizep = &data->size; else - strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size); + strbuf_add_uint(sb, data->size); } else if (is_atom("objectsize:disk", atom, len)) { if (data->mark_query) data->info.disk_sizep = &data->disk_size; else - strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size); + strbuf_add_uint(sb, data->disk_size); } else if (is_atom("rest", atom, len)) { if (data->mark_query) data->split_on_whitespace = 1; diff --git a/builtin/ls-files.c b/builtin/ls-files.c index e1a22b41b9..12d5d828ff 100644 --- a/builtin/ls-files.c +++ b/builtin/ls-files.c @@ -250,20 +250,23 @@ static void expand_objectsize(struct repository *repo, struct strbuf *line, const struct object_id *oid, const enum object_type type, unsigned int padded) { + static const char padding[] = " "; + size_t min_len = padded ? strlen(padding) : 0; + size_t orig_len = line->len; + size_t len; + if (type == OBJ_BLOB) { unsigned long size; if (odb_read_object_info(repo->objects, oid, &size) < 0) die(_("could not get object info about '%s'"), oid_to_hex(oid)); - if (padded) - strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size); - else - strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size); - } else if (padded) { - strbuf_addf(line, "%7s", "-"); + strbuf_add_uint(line, size); } else { strbuf_addstr(line, "-"); } + len = line->len - orig_len; + if (len < min_len) + strbuf_insert(line, orig_len, padding, min_len - len); } static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce, diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 113e4a960d..57846911ce 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -26,20 +26,23 @@ static const char * const ls_tree_usage[] = { static void expand_objectsize(struct strbuf *line, const struct object_id *oid, const enum object_type type, unsigned int padded) { + static const char padding[] = " "; + size_t min_len = padded ? strlen(padding) : 0; + size_t orig_len = line->len; + size_t len; + if (type == OBJ_BLOB) { unsigned long size; if (odb_read_object_info(the_repository->objects, oid, &size) < 0) die(_("could not get object info about '%s'"), oid_to_hex(oid)); - if (padded) - strbuf_addf(line, "%7"PRIuMAX, (uintmax_t)size); - else - strbuf_addf(line, "%"PRIuMAX, (uintmax_t)size); - } else if (padded) { - strbuf_addf(line, "%7s", "-"); + strbuf_add_uint(line, size); } else { strbuf_addstr(line, "-"); } + len = line->len - orig_len; + if (len < min_len) + strbuf_insert(line, orig_len, padding, min_len - len); } struct ls_tree_options { diff --git a/strbuf.c b/strbuf.c index 8610965d53..764b629927 100644 --- a/strbuf.c +++ b/strbuf.c @@ -359,6 +359,18 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...) va_end(ap); } +void strbuf_add_uint(struct strbuf *sb, uintmax_t value) +{ + char buf[DIV_ROUND_UP(bitsizeof(value) * 10, 33)]; + char *end = buf + sizeof(buf); + char *p = end; + + do + *--p = "0123456789"[value % 10]; + while (value /= 10); + strbuf_add(sb, p, end - p); +} + static void add_lines(struct strbuf *out, const char *prefix, const char *buf, size_t size, diff --git a/strbuf.h b/strbuf.h index 06e284f9cc..1089ae687b 100644 --- a/strbuf.h +++ b/strbuf.h @@ -410,6 +410,12 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes); __attribute__((format (printf,2,3))) void strbuf_addf(struct strbuf *sb, const char *fmt, ...); + +/** + * Add an unsigned decimal number. + */ +void strbuf_add_uint(struct strbuf *sb, uintmax_t value); + /** * Add a formatted string prepended by a comment character and a * blank to the buffer.