📄 waritoa.cpp
字号:
#include "StdAfx.h"#include "WarItoa.h" // class implemented#ifndef WAR_TYPES_H# include "WarTypes.h"#endiftemplate <class numT>int WarDoDivi (numT & n, int base){ numT res; res = n % (numT) base; n = n / (numT) base; return res;}template <class charT, class numT>std::basic_string<charT>& WarItoaSigned(std::basic_string <charT> &dst, numT val, const int base, const bool large_digits, char fmt_char){ bool is_negative = false; if (val < 0) { val *= -1; is_negative = true; } return WarItoaImpl(dst, val, base, large_digits, fmt_char, is_negative);}template <class charT, class numT>std::basic_string<charT>& WarItoaImpl(std::basic_string <charT> &dst, const numT val, const int base, const bool large_digits, char fmt_char, bool is_negative = false){ static const char s_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; static const char l_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char buf[64]; war_cstr_t p = buf; war_ccstr_t digits = large_digits ? l_digits : s_digits; numT my_val = val; if (my_val == 0) *p++ = '0'; else while (my_val != 0) *p++ = digits[WarDoDivi(my_val, base)]; if (is_negative) *p++ = '-'; *p = 0; int cnt = fmt_char ? (3 - (strlen(buf) % 3)) : 0; for (int i = (p - buf) - 1; i >= 0; i--) { if (fmt_char && !(cnt++ % 3)) dst += '.'; dst += buf[i]; } return dst;}#define IMPL(ct, nt, fn) \template std::basic_string<ct>& WarItoa<ct, nt>(std::basic_string<ct> &dst, \ const nt val, \ const int base, \ const bool large_digits, \ char fmt_char); \std::basic_string<ct>& WarItoa(std::basic_string<ct> &dst, \ const nt val, \ const int base, \ const bool large_digits, \ char fmt_char) \{ \ return fn(dst, val, base, large_digits, fmt_char); \}IMPL(char, war_int8_t, WarItoaSigned)IMPL(char, war_int16_t, WarItoaSigned)IMPL(char, war_int32_t, WarItoaSigned)IMPL(char, war_int64_t, WarItoaSigned)IMPL(char, war_uint8_t, WarItoaImpl)IMPL(char, war_uint16_t, WarItoaImpl)IMPL(char, war_uint32_t, WarItoaImpl)IMPL(char, war_uint64_t, WarItoaImpl)IMPL(wchar_t, war_int8_t, WarItoaSigned)IMPL(wchar_t, war_int16_t, WarItoaSigned)IMPL(wchar_t, war_int32_t, WarItoaSigned)IMPL(wchar_t, war_int64_t, WarItoaSigned)IMPL(wchar_t, war_uint8_t, WarItoaImpl)IMPL(wchar_t, war_uint16_t, WarItoaImpl)IMPL(wchar_t, war_uint32_t, WarItoaImpl)IMPL(wchar_t, war_uint64_t, WarItoaImpl)#if defined(NEED_TYPE_TEMPLATE) || defined(_MSC_VER) IMPL(char, char, WarItoaSigned) IMPL(char, short, WarItoaSigned) IMPL(char, int, WarItoaSigned) IMPL(char, long, WarItoaSigned) IMPL(char, unsigned char, WarItoaImpl) IMPL(char, unsigned short, WarItoaImpl) IMPL(char, unsigned int, WarItoaImpl) IMPL(char, unsigned long, WarItoaImpl) IMPL(wchar_t, char, WarItoaSigned) IMPL(wchar_t, short, WarItoaSigned) IMPL(wchar_t, int, WarItoaSigned) IMPL(wchar_t, long, WarItoaSigned) IMPL(wchar_t, unsigned char, WarItoaImpl) IMPL(wchar_t, unsigned short, WarItoaImpl) IMPL(wchar_t, unsigned int, WarItoaImpl) IMPL(wchar_t, unsigned long, WarItoaImpl)#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -