Answers
这个不作为答案哈,应 @李剑波 要求提供一下chrome里面的实现:
template <typename STR, typename INT, typename UINT, bool NEG>
struct IntToStringT {
template <typename INT2, typename UINT2, bool NEG2>
struct ToUnsignedT { };
template <typename INT2, typename UINT2>
struct ToUnsignedT<INT2, UINT2, false> {
static UINT2 ToUnsigned(INT2 value) {
return static_cast<UINT2>(value);
}
};
template <typename INT2, typename UINT2>
struct ToUnsignedT<INT2, UINT2, true> {
static UINT2 ToUnsigned(INT2 value) {
return static_cast<UINT2>(value < 0 ? -value : value);
}
};
static STR IntToString(INT value) {
const int kOutputBufSize = 3 * sizeof(INT) + 1;
STR outbuf(kOutputBufSize, 0);
bool is_neg = value < 0;
UINT res = ToUnsignedT<INT, UINT, NEG>::ToUnsigned(value);
for (typename STR::iterator it = outbuf.end();;) {
--it;
DCHECK(it != outbuf.begin());
*it = static_cast<typename STR::value_type>((res % 10) + '0');
res /= 10;
if (res == 0) {
if (is_neg) {
--it;
DCHECK(it != outbuf.begin());
*it = static_cast<typename STR::value_type>('-');
}
return STR(it, outbuf.end());
}
}
}
};
inline std::string IntToString(int value) {
return IntToStringT<std::string, int, unsigned int, true>::
IntToString(value);
}
inline std::wstring IntToWString(int value) {
return IntToStringT<std::wstring, int, unsigned int, true>::
IntToString(value);
}
inline std::string UintToString(unsigned int value) {
return IntToStringT<std::string, unsigned int, unsigned int, false>::
IntToString(value);
}
inline std::wstring UintToWString(unsigned int value) {
return IntToStringT<std::wstring, unsigned int, unsigned int, false>::
IntToString(value);
}
inline std::string Int64ToString(int64 value) {
return IntToStringT<std::string, int64, uint64, true>::
IntToString(value);
}
inline std::wstring Int64ToWString(int64 value) {
return IntToStringT<std::wstring, int64, uint64, true>::
IntToString(value);
}
inline std::string Uint64ToString(uint64 value) {
return IntToStringT<std::string, uint64, uint64, false>::
IntToString(value);
}
inline std::wstring Uint64ToWString(uint64 value) {
return IntToStringT<std::wstring, uint64, uint64, false>::
IntToString(value);
johnct
answered 12 years, 5 months ago