Re: [PATCH 2] count-objects: add human-readable size option

3 messages, 3 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH 2] count-objects: add human-readable size option

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:45:09

"Shawn O. Pearce" [off-list ref] writes:
Marcus Griep [off-list ref] wrote:
quoted
diff --git a/Documentation/git-count-objects.txt b/Documentation/git-count-objects.txt
index 75a8da1..291bc5e 100644
--- a/Documentation/git-count-objects.txt
+++ b/Documentation/git-count-objects.txt
+++ b/builtin-count-objects.c
...
quoted
+void human_readable_size(char *buf, int buf_size, double size /* in bytes */)
Hmm.  This probably should be static.  Or if it really is meant
to be a utility for use elsewhere in Git, moved to someplace where
string handling is done.  Its not strbuf related, but maybe strbuf.c
is a better location for this sort of library function.
Yes, with customizable precision (so that the caller can control "1.6k" vs
"1.62k"), and perhaps cutomizable unit (so that you can use this for
"3.6kB" and "2.6Mbps"), this kind of thing is a good candidate to be a
library function in strbuf.c.
quoted
+{
+	char human_readable_prefixes[10] = "BKMGTPEZY";
This enumerates suffix if I am not mistaken.  Do you have to say "10"
here, or does the compiler counts them for you?
quoted
+	for (; i < 8 && size >= 1000 ; ++i, size = size / 1024)
+		;
I do not think you would need to use the magic number "8" here.

I have this suspicion that the caller, if this is made into a generic
library, would want to pass in a list of units, not magnitude suffixes,
like this:

    extern int human_readable(struct strbuf *,
    			      double value, int precision,
                              const char **unit);

    static const char **size_unit = {
    	"byte", "KB", "MB", "GB", NULL,
    };
    static const char **throughput_unit = {
    	"bps", "Kbps", "Mbps", "Gbps", NULL,
    };

    human_readble(&sb, (double) bytes_transferred, 0, size_unit);
    human_readble(&sb, (double) throughput, 2, throughput_unit);

Re: [PATCH 2] count-objects: add human-readable size option

From: Johannes Schindelin <hidden>
Date: 2016-06-15 22:45:09

Hi,

On Wed, 13 Aug 2008, Junio C Hamano wrote:
"Shawn O. Pearce" [off-list ref] writes:
quoted
Marcus Griep [off-list ref] wrote:
quoted
diff --git a/Documentation/git-count-objects.txt b/Documentation/git-count-objects.txt
index 75a8da1..291bc5e 100644
--- a/Documentation/git-count-objects.txt
+++ b/Documentation/git-count-objects.txt
+++ b/builtin-count-objects.c
...
quoted
+void human_readable_size(char *buf, int buf_size, double size /* in bytes */)
Hmm.  This probably should be static.  Or if it really is meant
to be a utility for use elsewhere in Git, moved to someplace where
string handling is done.  Its not strbuf related, but maybe strbuf.c
is a better location for this sort of library function.
Yes, with customizable precision (so that the caller can control "1.6k" vs
"1.62k"), and perhaps cutomizable unit (so that you can use this for
"3.6kB" and "2.6Mbps"), this kind of thing is a good candidate to be a
library function in strbuf.c.
Or, should it ever be merged into git.git, the strbuf_vaddf() routine I 
wrote.

Ciao,
Dscho

Re: [PATCH 2] count-objects: add human-readable size option

From: Marcus Griep <hidden>
Date: 2016-06-15 22:45:09

Junio C Hamano wrote:
    extern int human_readable(struct strbuf *,
    			      double value, int precision,
                              const char **unit);

    static const char **size_unit = {
    	"byte", "KB", "MB", "GB", NULL,
    };
    static const char **throughput_unit = {
    	"bps", "Kbps", "Mbps", "Gbps", NULL,
    };

    human_readble(&sb, (double) bytes_transferred, 0, size_unit);
    human_readble(&sb, (double) throughput, 2, throughput_unit);
Here's what I'm looking at as a new signature:

extern int strbuf_add_human_readable(struct strbuf *,
				double value, int maxlen,
				int scale, const char *suffix,
				int flags);

where 'maxlen' specifies the longest string that should be returned.
That will make it easier for any pretty-ish formatting like ls and
du use. A value of 0 is unlimited length.

'scale' is used to specify a boundary, above which value should be
reduced, and below which it should be reported. Commonly this is
1000.  If 0, then it will find a scale that best fits into 'maxlen'.
If both 'maxlen' and 'scale' are 0, then it will use the default
of 1000.

'suffix' is appended onto every formatted string.  This would often be
"", "B", "bps".

'flags' would provide the ability to switch between a binary (1024)
and an si (1000) period.  Also, adding a space between number and
unit.

On success, would return 0.  If maxlen is specified and there is not
enough space given the scale or an inordinately large value, return
-n, where n is the amount of additional length that would have been
needed.

Does this sound appropriate?

-- 
Marcus Griep
GPG Key ID: 0x5E968152
——
http://www.boohaunt.net
את.ψο´
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help