summaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-01-16 23:30:02 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-01-16 23:30:02 +0900
commit3b7566faf118510768d16912ecb47ce991495cdc (patch)
tree2f931baafd92111726da606dc9a4aa1b258a9741 /src/util.h
parent1c72b80aa53673ea9fda0012bf3dadf77ae76b30 (diff)
Rewritten util::uitos()
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h35
1 files changed, 19 insertions, 16 deletions
diff --git a/src/util.h b/src/util.h
index 744be965..2e1418cc 100644
--- a/src/util.h
+++ b/src/util.h
@@ -172,25 +172,28 @@ void divide
}
template<typename T>
-std::string uitos(T value, bool comma = false)
+std::string uitos(T n, bool comma = false)
{
- std::string str;
- if(value == 0) {
- str = "0";
- return str;
- }
- int count = 0;
- while(value) {
- ++count;
- char digit = value%10+'0';
- if(comma && count > 3 && count%3 == 1) {
- str += ",";
+ std::string res;
+ if(n == 0) {
+ res = "0";
+ return res;
+ }
+ int i = 0;
+ T t = n;
+ for(; t; t /= 10, ++i);
+ if(comma) {
+ i += (i-1)/3;
+ }
+ 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 str;
+ return res;
}
std::string itos(int64_t value, bool comma = false);