Fix FS#27924: don't display negative zeroes

Dan: don't compute lower bound unless needed, flip argument order so
out values are last, add param Doxygen documentation.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Thomas Dziedzic 2012-02-02 16:43:40 -06:00 committed by Dan McGee
parent 47313acee4
commit e8db984ce5
4 changed files with 22 additions and 13 deletions

View File

@ -693,8 +693,8 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
}
rate_human = humanize_size((off_t)rate, '\0', &rate_label);
xfered_human = humanize_size(xfered, '\0', &xfered_label);
rate_human = humanize_size((off_t)rate, '\0', -1, &rate_label);
xfered_human = humanize_size(xfered, '\0', -1, &xfered_label);
printf(" %ls%-*s ", wcfname, padwid, "");
/* We will show 1.62M/s, 11.6M/s, but 116K/s and 1116K/s */

View File

@ -119,14 +119,14 @@ void dump_pkg_full(alpm_pkg_t *pkg, int extra)
deplist_display(_("Conflicts With :"), alpm_pkg_get_conflicts(pkg));
deplist_display(_("Replaces :"), alpm_pkg_get_replaces(pkg));
size = humanize_size(alpm_pkg_get_size(pkg), 'K', &label);
size = humanize_size(alpm_pkg_get_size(pkg), 'K', 2, &label);
if(from == PKG_FROM_SYNCDB) {
printf(_("Download Size : %6.2f %s\n"), size, label);
} else if(from == PKG_FROM_FILE) {
printf(_("Compressed Size: %6.2f %s\n"), size, label);
}
size = humanize_size(alpm_pkg_get_isize(pkg), 'K', &label);
size = humanize_size(alpm_pkg_get_isize(pkg), 'K', 2, &label);
printf(_("Installed Size : %6.2f %s\n"), size, label);
string_display(_("Packager :"), alpm_pkg_get_packager(pkg));

View File

@ -36,6 +36,7 @@
#include <unistd.h>
#include <limits.h>
#include <wchar.h>
#include <math.h> /* pow */
#ifdef HAVE_TERMIOS_H
#include <termios.h> /* tcflush */
#endif
@ -791,13 +792,13 @@ static alpm_list_t *create_verbose_row(pm_target_t *target, int dl_size)
/* and size */
size -= target->remove ? alpm_pkg_get_isize(target->remove) : 0;
size += target->install ? alpm_pkg_get_isize(target->install) : 0;
human_size = humanize_size(size, 'M', &label);
human_size = humanize_size(size, 'M', 2, &label);
pm_asprintf(&str, "%.2f %s", human_size, label);
ret = alpm_list_add(ret, str);
if(dl_size) {
size = target->install ? alpm_pkg_download_size(target->install) : 0;
human_size = humanize_size(size, 'M', &label);
human_size = humanize_size(size, 'M', 2, &label);
if(size != 0) {
pm_asprintf(&str, "%.2f %s", human_size, label);
} else {
@ -881,21 +882,21 @@ static void _display_targets(alpm_list_t *targets, int verbose)
free(str);
if(dlsize > 0 || config->op_s_downloadonly) {
size = humanize_size(dlsize, 'M', &label);
size = humanize_size(dlsize, 'M', 2, &label);
printf(_("Total Download Size: %.2f %s\n"), size, label);
}
if(!config->op_s_downloadonly) {
if(isize > 0) {
size = humanize_size(isize, 'M', &label);
size = humanize_size(isize, 'M', 2, &label);
printf(_("Total Installed Size: %.2f %s\n"), size, label);
}
if(rsize > 0 && isize == 0) {
size = humanize_size(rsize, 'M', &label);
size = humanize_size(rsize, 'M', 2, &label);
printf(_("Total Removed Size: %.2f %s\n"), size, label);
}
/* only show this net value if different from raw installed size */
if(isize > 0 && rsize > 0) {
size = humanize_size(isize - rsize, 'M', &label);
size = humanize_size(isize - rsize, 'M', 2, &label);
printf(_("Net Upgrade Size: %.2f %s\n"), size, label);
}
}
@ -994,12 +995,14 @@ static char *pkg_get_location(alpm_pkg_t *pkg)
* @param target_unit '\0' or a short label. If equal to one of the short unit
* labels ('B', 'K', ...) bytes is converted to target_unit; if '\0', the first
* unit which will bring the value to below a threshold of 2048 will be chosen.
* @param long_labels whether to use short ("K") or long ("KiB") unit labels
* @param precision number of decimal places, ensures -0.0.0 gets rounded to
* 0.00; -1 if no rounding desired
* @param label will be set to the appropriate unit label
*
* @return the size in the appropriate unit
*/
double humanize_size(off_t bytes, const char target_unit, const char **label)
double humanize_size(off_t bytes, const char target_unit, int precision,
const char **label)
{
static const char *labels[] = {"B", "KiB", "MiB", "GiB",
"TiB", "PiB", "EiB", "ZiB", "YiB"};
@ -1021,6 +1024,11 @@ double humanize_size(off_t bytes, const char target_unit, const char **label)
*label = labels[index];
}
/* fix FS#27924 so that it doesn't display negative zeroes */
if(precision >= 0 && val < 0.0 && val > (-0.5 / pow(10, precision))) {
val = 0.0;
}
return val;
}

View File

@ -59,7 +59,8 @@ char *strtrim(char *str);
char *strreplace(const char *str, const char *needle, const char *replace);
alpm_list_t *strsplit(const char *str, const char splitchar);
void string_display(const char *title, const char *string);
double humanize_size(off_t bytes, const char target_unit, const char **label);
double humanize_size(off_t bytes, const char target_unit, int precision,
const char **label);
int table_display(const char *title, const alpm_list_t *header, const alpm_list_t *rows);
void list_display(const char *title, const alpm_list_t *list);
void list_display_linebreak(const char *title, const alpm_list_t *list);