Merge branch 'rs/strbuf-add-uint'
Adding a decimal integer with strbuf_addf("%u") appears commonly;
they have been optimized by using a custom formatter.
* rs/strbuf-add-uint:
ls-tree: use strbuf_add_uint()
ls-files: use strbuf_add_uint()
cat-file: use strbuf_add_uint()
strbuf: add strbuf_add_uint()
main
commit
7eaa3c82a8
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
12
strbuf.c
12
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,
|
||||
|
|
|
|||
6
strbuf.h
6
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.
|
||||
|
|
|
|||
Loading…
Reference in New Issue