-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.cpp
53 lines (44 loc) · 1.96 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <algorithm>
#include "md5-c.hpp"
#include "util.hpp"
std::string lpad( std::string const & s, std::size_t n, char c ) {
std::string r { s };
if ( n > r.length() ) r.append( n - r.length(), c );
return r;
};
std::string& ltrim( std::string& s ) {
s.erase( s.begin(), std::find_if( s.begin(), s.end(),
[]( unsigned char ch ) {
return !std::isspace( ch ); } ) );
return s;
}
std::string& rtrim( std::string& s ) {
s.erase( std::find_if( s.rbegin(), s.rend(),
[]( unsigned char ch ) {
return !std::isspace( ch ); } ).base(), s.end() );
return s;
}
bool iequal( std::string const & s1, std::string const & s2 ) {
return std::equal( s1.cbegin(), s1.cend(), s2.cbegin(), s2.cend(),
[]( unsigned char a, unsigned char b ) {
return tolower( a ) == tolower( b ); } );
}
char hashrate_pretty_unit( std::uint64_t count ) {
return
( count / 1'000.0 ) < 1'000 ? 'K' /* Kilo */
: ( count / 1'000'000.0 ) < 1'000 ? 'M' /* Mega */
: ( count / 1'000'000'000.0 ) < 1'000 ? 'G' /* Giga */
: ( count / 1'000'000'000'000.0 ) < 1'000 ? 'T' /* Tera */
: 'P' /* Peta */;
};
double hashrate_pretty_value( std::uint64_t count ) {
return
( count / 1'000.0 ) < 1'000 ? ( count / 1'000.0 ) /* Kilo */
: ( count / 1'000'000.0 ) < 1'000 ? ( count / 1'000'000.0 ) /* Mega */
: ( count / 1'000'000'000.0 ) < 1'000 ? ( count / 1'000'000'000.0 ) /* Giga */
: ( count / 1'000'000'000'000.0 ) < 1'000 ? ( count / 1'000'000'000'000.0 ) /* Tera */
: ( count / 1'000'000'000'000'000.0 ) /* Peta */;
};