From c908df881f105d6a1004ae6a0e39da2d0149f3e9 Mon Sep 17 00:00:00 2001 From: Benjamin Hipple Date: Sun, 26 Aug 2018 18:12:06 -0400 Subject: [PATCH] Avoid overflow and use boost::format If the user has an object greater than 1024 yottabytes, it'll just display it as N yottabytes instead of overflowing. Swaps to use boost::format strings instead of std::setw and std::setprecision. --- src/nix/path-info.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/nix/path-info.cc b/src/nix/path-info.cc index ee48d483c..fa4f45fb4 100644 --- a/src/nix/path-info.cc +++ b/src/nix/path-info.cc @@ -4,8 +4,9 @@ #include "json.hh" #include "common-args.hh" -#include #include +#include +#include using namespace nix; @@ -67,20 +68,20 @@ struct CmdPathInfo : StorePathsCommand, MixJSON void printSize(int value) { if (!humanReadable) { - std::cout << '\t' << std::setw(11) << value; + std::cout << '\t' << boost::format("%11d") % value; return; } - static constexpr std::array idents = { - ' ', 'K', 'M', 'G', 'T', 'P' + static constexpr std::array idents = { + ' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' }; size_t power = 0; double res = value; - while (res > 1024) { + while (res > 1024 && power < idents.size()) { ++power; res /= 1024; } - std::cout << '\t' << std::setw(11) << std::setprecision(3) << res << idents[power]; + std::cout << '\t' << boost::format("%11.1f") % res << idents[power]; } void run(ref store, Paths storePaths) override