d7b8c8436b55a4f08f46a7caae8bf3ab88773f9f jcasper Fri May 17 13:40:41 2019 -0700 Changes in response to code review, refs #23481 diff --git src/lib/obscure.c src/lib/obscure.c index 49794eb..f6bea35 100644 --- src/lib/obscure.c +++ src/lib/obscure.c @@ -628,77 +628,71 @@ sprintf(s, "%lld", l); } void printLongWithCommas(FILE *f, long long l) /* Print out a long number with commas at thousands, millions, etc. */ { char ascii[32]; sprintLongWithCommas(ascii, l); fprintf(f, "%s", ascii); } void sprintWithGreekByte(char *s, int slength, long long size) /* Numbers formatted with PB, TB, GB, MB, KB, B */ { char *greek[] = {"B", "KB", "MB", "GB", "TB", "PB"}; +int maxGreek = (sizeof(greek)/sizeof(char*))-1; int i = 0; long long d = 1; -while ((size/d) >= 1024) +while (((size/d) >= 1024) && (i != maxGreek)) { ++i; d *= 1024; } -assert(i<(sizeof(greek)/sizeof(char*))); double result = ((double)size)/d; -if (result < 10) - safef(s,slength,"%3.1f %s",((double)size)/d, greek[i]); -else - safef(s,slength,"%3.0f %s",((double)size)/d, greek[i]); +safef(s, slength, "%3.*f %s", result < 10 ? 1 : 0, ((double)size)/d, greek[i]); } void printWithGreekByte(FILE *f, long long l) /* Print with formatting in gigabyte, terabyte, etc. */ { char buf[32]; sprintWithGreekByte(buf, sizeof(buf), l); fprintf(f, "%s", buf); } -void sprintWithSiBaseUnit(char *s, int slength, long long size) +void sprintWithMetricBaseUnit(char *s, int slength, long long size) /* Numbers formatted with Pb, Tb, Gb, Mb, kb, bp */ { char *unit[] = {"bp", "kB", "Mb", "Gb", "Tb", "Pb"}; +int maxUnit = (sizeof(unit)/sizeof(char*))-1; int i = 0; long long d = 1; -while ((size/d) >= 1000) +while (((size/d) >= 1000) && (i != maxUnit)) { ++i; d *= 1000; } -assert(i<(sizeof(unit)/sizeof(char*))); double result = ((double)size)/d; -if (result < 10) - safef(s,slength,"%3.1f %s",((double)size)/d, unit[i]); -else - safef(s,slength,"%3.0f %s",((double)size)/d, unit[i]); +safef(s, slength, "%3.*f %s", result < 10 ? 1 : 0, ((double)size)/d, unit[i]); } -void printWithSiBaseUnit(FILE *f, long long l) +void printWithMetricBaseUnit(FILE *f, long long l) /* Print with formatting in megabase, kilobase, etc. */ { char buf[32]; -sprintWithSiBaseUnit(buf, sizeof(buf), l); +sprintWithMetricBaseUnit(buf, sizeof(buf), l); fprintf(f, "%s", buf); } void shuffleArrayOfChars(char *array, int arraySize) /* Shuffle array of characters of given size given number of times. */ { char c; int i, randIx; /* Randomly permute an array using the method from Cormen, et al */ for (i=0; i<arraySize; ++i) { randIx = i + (rand() % (arraySize - i)); c = array[i]; array[i] = array[randIx];