Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
f10944b0
Commit
f10944b0
authored
Apr 03, 2018
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update human_readble_byte_count algo
parent
0759f146
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
35 deletions
+24
-35
space.h
space.h
+24
-35
No files found.
space.h
View file @
f10944b0
#pragma once
#include <string>
#include <cmath>
#include <iomanip>
#include <sstream>
namespace
jlib
{
static
std
::
string
format_space
(
uintmax_t
bytes
)
{
static
constexpr
uintmax_t
FACTOR
=
1024
;
static
constexpr
uintmax_t
KB
=
FACTOR
;
static
constexpr
uintmax_t
MB
=
KB
*
FACTOR
;
static
constexpr
uintmax_t
GB
=
MB
*
FACTOR
;
static
constexpr
uintmax_t
TB
=
GB
*
FACTOR
;
enum
PositionalNotation
{
//! Used for memory size or Microsoft disk space
Binary
,
//! Usually used by Disk Space
Decimal
,
};
uintmax_t
kb
=
bytes
/
FACTOR
;
uintmax_t
mb
=
kb
/
FACTOR
;
uintmax_t
gb
=
mb
/
FACTOR
;
uintmax_t
tb
=
gb
/
FACTOR
;
/**
* @brief Format byte count to human readable string
* @note bytes must less than 1DB(DoggaByte)
*/
static
std
::
string
human_readable_byte_count
(
uintmax_t
bytes
,
PositionalNotation
po
=
PositionalNotation
::
Binary
)
{
// http://programming.guide/java/formatting-byte-size-to-human-readable-format.html
std
::
string
s
;
if
(
tb
>
0
)
{
gb
-=
tb
*
FACTOR
;
gb
=
gb
*
1000
/
FACTOR
;
gb
/=
10
;
return
std
::
to_string
(
tb
)
+
"."
+
std
::
to_string
(
gb
)
+
"T"
;
}
else
if
(
gb
>
0
)
{
mb
-=
gb
*
FACTOR
;
mb
=
mb
*
1000
/
FACTOR
;
mb
/=
10
;
return
std
::
to_string
(
gb
)
+
"."
+
std
::
to_string
(
mb
)
+
"G"
;
}
else
if
(
mb
>
0
)
{
kb
-=
mb
*
FACTOR
;
kb
=
kb
*
1000
/
FACTOR
;
kb
/=
10
;
return
std
::
to_string
(
mb
)
+
"."
+
std
::
to_string
(
kb
)
+
"M"
;
}
else
if
(
kb
>
0
)
{
bytes
-=
kb
*
FACTOR
;
bytes
=
bytes
*
1000
/
FACTOR
;
bytes
/=
10
;
return
std
::
to_string
(
kb
)
+
"."
+
std
::
to_string
(
bytes
)
+
"K"
;
}
else
{
return
std
::
to_string
(
bytes
)
+
"B"
;
}
auto
unit
=
po
==
PositionalNotation
::
Binary
?
1024
:
1000
;
if
(
bytes
<
unit
)
{
return
std
::
to_string
(
bytes
)
+
"B"
;
}
auto
exp
=
static_cast
<
int
>
(
std
::
log
(
bytes
)
/
std
::
log
(
unit
));
auto
pre
=
std
::
string
(
"KMGTPEZYBND"
).
at
(
exp
-
1
)
+
std
::
string
(
po
==
PositionalNotation
::
Binary
?
"i"
:
""
);
auto
var
=
bytes
/
std
::
pow
(
unit
,
exp
);
std
::
stringstream
ss
;
ss
<<
std
::
fixed
<<
std
::
setprecision
(
1
)
<<
var
<<
pre
<<
"B"
;
return
ss
.
str
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment