-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnformat.hpp
96 lines (87 loc) · 2.6 KB
/
nformat.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#ifndef NFORMAT_HPP
#define NFORMAT_HPP
#include <tchar.h>
#include <locale>
#include <string>
#include <sstream>
std::locale get_numpunct_locale(std::locale const &lo);
template<class T>
std::basic_string<TCHAR> nformat(T v, std::locale const &loc, bool is_numpunct_locale)
{
std::basic_ostringstream<TCHAR> ss;
ss.imbue(is_numpunct_locale ? loc : get_numpunct_locale(loc));
ss << v;
return ss.str();
}
#if defined(_MSC_VER) && !defined(_WIN64) && (!defined(_CPPLIB_VER) || _CPPLIB_VER < 403)
template<class V>
std::basic_string<TCHAR> nformat64(V v, std::locale const &loc, bool is_numpunct_locale)
{
struct SS : public std::basic_ostringstream<TCHAR>
{
struct NumPunctFacet : public _Nput
{
typedef TCHAR _E;
typedef std::ostreambuf_iterator<_E, std::char_traits<_E> > _OI;
static char *__cdecl _Ifmt(char *_Fmt, const char *_Spec, ios_base::fmtflags _Fl)
{
char *_S = _Fmt;
*_S++ = '%';
if (_Fl & ios_base::showpos)
{ *_S++ = '+'; }
if (_Fl & ios_base::showbase)
{ *_S++ = '#'; }
*_S++ = _Spec[0];
*_S++ = _Spec[1];
*_S++ = _Spec[2];
*_S++ = _Spec[3];
*_S++ = _Spec[4];
ios_base::fmtflags _Bfl = _Fl & ios_base::basefield;
*_S++ = _Bfl == ios_base::oct ? 'o'
: _Bfl != ios_base::hex ? _Spec[5] // 'd' or 'u'
: _Fl & ios_base::uppercase ? 'X' : 'x';
*_S = '\0';
return (_Fmt);
}
_OI do_put(_OI _F, ios_base& _X, _E _Fill, __int64 _V) const
{
char _Buf[2 * _MAX_INT_DIG], _Fmt[12];
return (_Iput(_F, _X, _Fill, _Buf, sprintf(_Buf, _Ifmt(_Fmt, "I64lld", _X.flags()), _V)));
}
_OI do_put(_OI _F, ios_base& _X, _E _Fill, unsigned __int64 _V) const
{
char _Buf[2 * _MAX_INT_DIG], _Fmt[12];
return (_Iput(_F, _X, _Fill, _Buf, sprintf(_Buf, _Ifmt(_Fmt, "I64llu", _X.flags()), _V)));
}
};
SS &operator <<(V _X)
{
iostate _St = goodbit;
const sentry _Ok(*this);
if (_Ok)
{
const _Nput& _Fac = _USE(getloc(), _Nput);
_TRY_IO_BEGIN
if (static_cast<NumPunctFacet const &>(_Fac).do_put(_Iter(rdbuf()), *this, fill(), _X).failed())
{
_St |= badbit;
}
_CATCH_IO_END
}
setstate(_St);
return *this;
}
} ss;
ss.imbue(is_numpunct_locale ? loc : get_numpunct_locale(loc));
ss << v;
return ss.str();
}
template<>
std::basic_string<TCHAR> nformat< int64_t>( int64_t v, std::locale const &loc, bool is_numpunct_locale)
{ return nformat64(v, loc, is_numpunct_locale); }
template<>
std::basic_string<TCHAR> nformat<uint64_t>(uint64_t v, std::locale const &loc, bool is_numpunct_locale)
{ return nformat64(v, loc, is_numpunct_locale); }
#endif
#endif