forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodec.h
112 lines (102 loc) · 2.34 KB
/
todec.h
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <[email protected]>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCMS_IMPL_TODEC_H
#define CPPCMS_IMPL_TODEC_H
#include <limits>
#include <ostream>
namespace cppcms {
namespace impl {
namespace details {
template<bool sig>
struct decimal_traits;
template<>
struct decimal_traits<false> {
template<typename T>
static void conv(T v,char *&begin,char *&buf)
{
begin = buf;
while(v!=0) {
*buf++ = '0' + v % 10;
v/=10;
}
}
};
template<>
struct decimal_traits<true> {
template<typename T>
static void conv(T v,char *&begin,char *&buf)
{
if(v<0) {
*buf++ = '-';
begin=buf;
while(v!=0) {
*buf++ = '0' - (v % 10);
v/=10;
}
}
else {
decimal_traits<false>::conv(v,begin,buf);
}
}
};
}
template<typename Integer>
void todec(Integer v,char *buf)
{
typedef std::numeric_limits<Integer> limits;
if(v == 0) {
*buf++ = '0';
*buf++ = 0;
return;
}
char *begin=0;
details::decimal_traits<limits::is_signed>::conv(v,begin,buf);
*buf-- = 0;
while(begin < buf) {
char tmp = *begin;
*begin = *buf;
*buf = tmp;
buf--;
begin++;
}
}
template<typename I>
std::string todec_string(I v)
{
char buf[std::numeric_limits<I>::digits10 + 4];
todec<I>(v,buf);
std::string tmp = buf;
return tmp;
}
namespace details {
template<typename T>
struct write_int_to_stream {
write_int_to_stream(T vi = 0) : v(vi) {}
T v;
void operator()(std::ostream &out) const
{
char buf[std::numeric_limits<T>::digits10 + 4];
todec<T>(v,buf);
out << buf;
}
};
template<typename T>
std::ostream &operator<<(std::ostream &out,write_int_to_stream<T> const &v)
{
v(out);
return out;
}
} // details
template<typename T>
details::write_int_to_stream<T> cint(T v)
{
return details::write_int_to_stream<T>(v);
}
}
}
#endif