Bugzilla – Attachment 41 Details for
Bug 73
sr_si_string_u64() doesn't handle all cases
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Format decimals correctly in sr_si_string_u64()
0002-Format-decimals-correctly-in-sr_si_string_u64.patch (text/plain), 2.25 KB, created by
Peter Stuge
on 2013-06-04 11:58:49 CEST
(
hide
)
Description:
Format decimals correctly in sr_si_string_u64()
Filename:
MIME Type:
Creator:
Peter Stuge
Created:
2013-06-04 11:58:49 CEST
Size:
2.25 KB
patch
obsolete
>From 327282ee15a73fe52a78b3bad87cca0ecceab779 Mon Sep 17 00:00:00 2001 >From: Peter Stuge <peter@stuge.se> >Date: Tue, 4 Jun 2013 02:20:57 +0200 >Subject: [PATCH 2/2] Format decimals correctly in sr_si_string_u64() > >The function previously formatted 1004 as 1.4 k and 1004000 as 1.4 M. >The function now formats 1004 as 1.004 k, 1004000 as 1.004 M and >1004000000 as 1.004 G. > >Fixes #73. >--- > strutil.c | 40 +++++++++++++++++++--------------------- > 1 file changed, 19 insertions(+), 21 deletions(-) > >diff --git a/strutil.c b/strutil.c >index b7b0af1..4be37ca 100644 >--- a/strutil.c >+++ b/strutil.c >@@ -64,32 +64,30 @@ > */ > SR_API char *sr_si_string_u64(uint64_t x, const char *unit) > { >+ uint8_t i; >+ uint64_t quot, divisor[] = { >+ 1, SR_KHZ(1), SR_MHZ(1), SR_GHZ(1), >+ SR_GHZ(1000), SR_GHZ(1000*1000), SR_GHZ(1000*1000*1000) >+ }; >+ const char *p, prefix[] = "\0kMGTPE"; >+ char fmt[8], fract[20] = "", *f; >+ > if (unit == NULL) > unit = ""; > >- if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) == 0)) { >- return g_strdup_printf("%" PRIu64 " G%s", x / SR_GHZ(1), unit); >- } else if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) != 0)) { >- return g_strdup_printf("%" PRIu64 ".%" PRIu64 " G%s", >- x / SR_GHZ(1), x % SR_GHZ(1), unit); >- } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) == 0)) { >- return g_strdup_printf("%" PRIu64 " M%s", >- x / SR_MHZ(1), unit); >- } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) != 0)) { >- return g_strdup_printf("%" PRIu64 ".%" PRIu64 " M%s", >- x / SR_MHZ(1), x % SR_MHZ(1), unit); >- } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) == 0)) { >- return g_strdup_printf("%" PRIu64 " k%s", >- x / SR_KHZ(1), unit); >- } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) != 0)) { >- return g_strdup_printf("%" PRIu64 ".%" PRIu64 " k%s", >- x / SR_KHZ(1), x % SR_KHZ(1), unit); >- } else { >- return g_strdup_printf("%" PRIu64 " %s", x, unit); >+ for (i = 0; (quot = x / divisor[i]) >= 1000; i++); >+ >+ if (i) { >+ sprintf(fmt, ".%%0%dlu", i * 3); >+ f = fract + sprintf(fract, fmt, x % divisor[i]) - 1; >+ >+ while (f >= fract && strchr("0.", *f)) >+ *f-- = 0; > } > >- sr_err("%s: Error creating SI units string.", __func__); >- return NULL; >+ p = prefix + i; >+ >+ return g_strdup_printf("%" PRIu64 "%s %.1s%s", quot, fract, p, unit); > } > > /** >-- >1.8.1.rc0.2.gfb4c622.dirty >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 73
:
36
|
37
|
38
|
39
|
40
| 41