Bugzilla – Attachment 37 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]
This patch fixes missing leading zeroes, trims trailing, and makes sure tests are consistent re: trailing zeroes.
0001-Format-decimals-correctly-in-sr_si_string_u64-drop-t.patch (text/plain), 6.07 KB, created by
Paul Sokolovsky
on 2013-06-03 21:30:40 CEST
(
hide
)
Description:
This patch fixes missing leading zeroes, trims trailing, and makes sure tests are consistent re: trailing zeroes.
Filename:
MIME Type:
Creator:
Paul Sokolovsky
Created:
2013-06-03 21:30:40 CEST
Size:
6.07 KB
patch
obsolete
>From 9c48a469df4c138fa63f01de84b8425a39cf0f90 Mon Sep 17 00:00:00 2001 >From: Paul Sokolovsky <pfalcon@users.sourceforge.net> >Date: Mon, 3 Jun 2013 22:23:57 +0300 >Subject: [PATCH] Format decimals correctly in sr_si_string_u64(), drop > trailing zeroes. > >The function would previously format 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. > >Based on patch by Peter Stuge. >--- > strutil.c | 66 ++++++++++++++++++++++++++++++++++++------------- > tests/check_strutil.c | 14 +++++------ > 2 files changed, 55 insertions(+), 25 deletions(-) > >diff --git a/strutil.c b/strutil.c >index b7b0af1..6b798a2 100644 >--- a/strutil.c >+++ b/strutil.c >@@ -48,6 +48,32 @@ > */ > > /** >+ * Format decimal fraction (up to 9 decimal digits) in a conventional >+ * form (without trailing zeroes). >+ * >+ * @param buf Output string buffer >+ * @param pattern printf pattern for traction, must start with a '.' >+ * @param fract Fraction, up to 9 decimal digits >+ * >+ * @return buf input param, filled in with string representation of the >+ * fraction, or empty string if fraction is 0. May return NULL >+ * if underlying sprintf() function failed. >+ */ >+static char *format_fract(char *buf, char *pattern, uint32_t fract) >+{ >+ char *p; >+ int sz = sprintf(buf, pattern, fract); >+ if (sz <= 0) >+ return NULL; >+ for (p = buf + sz - 1; *p == '0'; p--) { >+ *p = 0; >+ } >+ if (*p == '.') >+ *p = 0; >+ return buf; >+} >+ >+/** > * Convert a numeric value value to its "natural" string representation. > * in SI units > * >@@ -64,26 +90,32 @@ > */ > SR_API char *sr_si_string_u64(uint64_t x, const char *unit) > { >+ /* At most 9 fractional digits */ >+ uint32_t fract; >+ /* dot + 9 digits */ >+ char fract_buf[11]; >+ > 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); >+ if (x >= SR_GHZ(1)) { >+ fract = x % SR_GHZ(1); >+ return g_strdup_printf("%" PRIu64 "%s G%s", >+ x / SR_GHZ(1), >+ format_fract(fract_buf, ".%09u", fract), >+ unit); >+ } else if (x >= SR_MHZ(1)) { >+ fract = x % SR_MHZ(1); >+ return g_strdup_printf("%" PRIu64 "%s M%s", >+ x / SR_MHZ(1), >+ format_fract(fract_buf, ".%06u", fract), >+ unit); >+ } else if (x >= SR_KHZ(1)) { >+ fract = x % SR_KHZ(1); >+ return g_strdup_printf("%" PRIu64 "%s k%s", >+ x / SR_KHZ(1), >+ format_fract(fract_buf, ".%03u", fract), >+ unit); > } else { > return g_strdup_printf("%" PRIu64 " %s", x, unit); > } >diff --git a/tests/check_strutil.c b/tests/check_strutil.c >index a439d6e..fa8083f 100644 >--- a/tests/check_strutil.c >+++ b/tests/check_strutil.c >@@ -57,8 +57,6 @@ static void test_samplerate(uint64_t samplerate, const char *expected) > * - Results which contain commas (e.g. 1.234 / 12.34 / 123.4 kHz). > * - Results with zeroes right after the comma (e.g. 1.034 Hz). > * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=73 >- * - Results with trailing zeroes (e.g. 1.230 kHz). >- * (This is currently allowed, but might be changed later) > * - Results with zeroes in the middle (e.g. 1.204 kHz). > * - All of the above, but using SR_MHZ() and friends. > * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=72 >@@ -95,7 +93,7 @@ START_TEST(test_khz) > test_samplerate(123456, "123.456 kHz"); > test_samplerate(1034, "1.034 kHz"); > test_samplerate(1004, "1.004 kHz"); >- test_samplerate(1230, "1.230 kHz"); >+ test_samplerate(1230, "1.23 kHz"); > > /* Again, but now using SR_KHZ(). */ > test_samplerate(SR_KHZ(1), "1 kHz"); >@@ -107,7 +105,7 @@ START_TEST(test_khz) > test_samplerate(SR_KHZ(1.204), "1.204 kHz"); > test_samplerate(SR_KHZ(1.034), "1.034 kHz"); > test_samplerate(SR_KHZ(1.004), "1.004 kHz"); >- test_samplerate(SR_KHZ(1.230), "1.230 kHz"); >+ test_samplerate(SR_KHZ(1.230), "1.23 kHz"); > } > END_TEST > >@@ -122,7 +120,7 @@ START_TEST(test_mhz) > test_samplerate(1230007, "1.230007 MHz"); > test_samplerate(1034567, "1.034567 MHz"); > test_samplerate(1000007, "1.000007 MHz"); >- test_samplerate(1234000, "1.234000 MHz"); >+ test_samplerate(1234000, "1.234 MHz"); > > /* Again, but now using SR_MHZ(). */ > test_samplerate(SR_MHZ(1), "1 MHz"); >@@ -134,7 +132,7 @@ START_TEST(test_mhz) > test_samplerate(SR_MHZ(1.230007), "1.230007 MHz"); > test_samplerate(SR_MHZ(1.034567), "1.034567 MHz"); > test_samplerate(SR_MHZ(1.000007), "1.000007 MHz"); >- test_samplerate(SR_MHZ(1.234000), "1.234000 MHz"); >+ test_samplerate(SR_MHZ(1.234000), "1.234 MHz"); > } > END_TEST > >@@ -152,7 +150,7 @@ START_TEST(test_ghz) > test_samplerate(441759000001ULL, "441.759000001 GHz"); > test_samplerate(441050000000ULL, "441.05 GHz"); > test_samplerate(441000000005ULL, "441.000000005 GHz"); >- test_samplerate(441500000000ULL, "441.500000000 GHz"); >+ test_samplerate(441500000000ULL, "441.5 GHz"); > > /* Again, but now using SR_GHZ(). */ > test_samplerate(SR_GHZ(1), "1 GHz"); >@@ -165,7 +163,7 @@ START_TEST(test_ghz) > test_samplerate(SR_GHZ(441.759000001), "441.759000001 GHz"); > test_samplerate(SR_GHZ(441.050000000), "441.05 GHz"); > test_samplerate(SR_GHZ(441.000000005), "441.000000005 GHz"); >- test_samplerate(SR_GHZ(441.500000000), "441.500000000 GHz"); >+ test_samplerate(SR_GHZ(441.500000000), "441.5 GHz"); > > /* Now check the biggest-possible samplerate (2^64 Hz). */ > test_samplerate(18446744073709551615ULL, "18446744073.709551615 GHz"); >-- >1.7.9.5 >
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