From d40fec5c5ce2b58176f973272bca34b6fa7b6cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Mon, 25 Aug 2014 20:48:32 -0700 Subject: [PATCH] Added prettify function to convert size in bytes to human readable form. --- include/bx/string.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/bx/string.h b/include/bx/string.h index 0057173..15afb27 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -311,6 +311,22 @@ namespace bx return _filePath; } + /// Convert size in bytes to human readable string. + inline void prettify(char* _out, size_t _count, uint64_t _size) + { + uint8_t idx = 0; + double size = double(_size); + while (_size != (_size&0x7ff) + && idx < 9) + { + _size >>= 10; + size *= 1.0/1024.0; + ++idx; + } + + snprintf(_out, _count, "%0.2f %c%c", size, "BkMGTPEZY"[idx], idx > 0 ? 'B' : '\0'); + } + /* * Copyright (c) 1998 Todd C. Miller *