mirror of https://github.com/aria2/aria2
Rewritten util::uitos()
parent
1c72b80aa5
commit
3b7566faf1
33
src/util.h
33
src/util.h
|
@ -172,25 +172,28 @@ void divide
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::string uitos(T value, bool comma = false)
|
std::string uitos(T n, bool comma = false)
|
||||||
{
|
{
|
||||||
std::string str;
|
std::string res;
|
||||||
if(value == 0) {
|
if(n == 0) {
|
||||||
str = "0";
|
res = "0";
|
||||||
return str;
|
return res;
|
||||||
}
|
}
|
||||||
int count = 0;
|
int i = 0;
|
||||||
while(value) {
|
T t = n;
|
||||||
++count;
|
for(; t; t /= 10, ++i);
|
||||||
char digit = value%10+'0';
|
if(comma) {
|
||||||
if(comma && count > 3 && count%3 == 1) {
|
i += (i-1)/3;
|
||||||
str += ",";
|
}
|
||||||
|
res.resize(i);
|
||||||
|
--i;
|
||||||
|
for(int j = 0; n; --i, ++j, n /= 10) {
|
||||||
|
res[i] = (n%10) + '0';
|
||||||
|
if(comma && (j+1)%3 == 0) {
|
||||||
|
res[--i] = ',';
|
||||||
}
|
}
|
||||||
str += digit;
|
|
||||||
value /= 10;
|
|
||||||
}
|
}
|
||||||
std::reverse(str.begin(), str.end());
|
return res;
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string itos(int64_t value, bool comma = false);
|
std::string itos(int64_t value, bool comma = false);
|
||||||
|
|
Loading…
Reference in New Issue