src: Refactor utos

This commit is contained in:
Tatsuhiro Tsujikawa 2018-09-01 22:29:11 +09:00
parent fb9a204de2
commit e959e7338e
1 changed files with 11 additions and 15 deletions

View File

@ -370,14 +370,12 @@ template <typename T> std::string utos(T n) {
res = "0"; res = "0";
return res; return res;
} }
int i = 0; size_t nlen = 0;
T t = n; for (auto t = n; t; t /= 10, ++nlen)
for (; t; t /= 10, ++i)
; ;
res.resize(i); res.resize(nlen);
--i; for (; n; n /= 10) {
for (; n; --i, n /= 10) { res[--nlen] = (n % 10) + '0';
res[i] = (n % 10) + '0';
} }
return res; return res;
} }
@ -387,15 +385,13 @@ template <typename T, typename OutputIt> OutputIt utos(OutputIt dst, T n) {
*dst++ = '0'; *dst++ = '0';
return dst; return dst;
} }
int i = 0; size_t nlen = 0;
T t = n; for (auto t = n; t; t /= 10, ++nlen)
for (; t; t /= 10, ++i)
; ;
--i; auto p = dst + nlen;
auto p = dst + i; auto res = p;
auto res = p + 1; for (; n; n /= 10) {
for (; n; --i, n /= 10) { *--p = (n % 10) + '0';
*p-- = (n % 10) + '0';
} }
return res; return res;
} }