Files
textmate/Frameworks/text/src/format.cc
Allan Odgaard 9894969e67 Initial commit
2012-08-09 16:25:56 +02:00

33 lines
563 B
C++

#include "format.h"
namespace text
{
std::string format_size (size_t inBytes)
{
double size = (double)inBytes;
char const* unitStr = inBytes == 1 ? "byte" : "bytes";
if(inBytes > 1000 * 1024*1024)
{
size /= 1024*1024*1024;
unitStr = "GiB";
}
else if(inBytes > 1000 * 1024)
{
size /= 1024*1024;
unitStr = "MiB";
}
else if(inBytes > 1000)
{
size /= 1024;
unitStr = "KiB";
}
else
{
return text::format("%zu %s", inBytes, unitStr);
}
return text::format("%.1f %s", size, unitStr);
}
} /* text */