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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
#if !defined(HISTORYSTATS_GUARD__FORMAT_H)
#define HISTORYSTATS_GUARD__FORMAT_H
/*
* ext::basic_format and helper routines
*/
namespace ext
{
template<typename T_>
class basic_format
{
public:
typedef typename T_ char_type;
typedef typename std::basic_string<T_> str_type;
typedef typename std::basic_string<T_>::size_type size_type;
typedef typename ext::basic_strfunc<T_> _Func;
private:
str_type m_Str;
char_type m_Sep;
size_type m_NextPos;
public:
explicit basic_format(const str_type& str, char_type sep = '|')
: m_Str(str), m_Sep(sep), m_NextPos(0)
{
}
const str_type& str() const
{
return m_Str;
}
basic_format& operator %(const char_type* szValue)
{
str_type::size_type pos = m_Str.find(m_Sep, m_NextPos);
if (pos != str_type::npos)
{
size_type len = _Func::len(szValue);
m_Str.replace(pos, 1, szValue, len);
m_NextPos = pos + len;
}
return *this;
}
basic_format& operator %(const str_type& strValue)
{
return (*this % strValue.c_str());
}
basic_format& operator %(int nValue)
{
std::basic_ostringstream<char_type> oss;
oss << nValue;
return (*this % oss.str().c_str());
}
};
template<typename T_>
inline const std::basic_string<T_>& str(const basic_format<T_>& format)
{
return format.str();
}
};
template<typename T_>
inline std::basic_ostream<T_>& operator <<(std::basic_ostream<T_>& os, const ext::basic_format<T_>& format)
{
os << format.str();
return os;
}
/*
* ext::basic_kformat and helper routines
*/
namespace ext
{
template<typename T_>
class basic_kformat
{
public:
typedef typename T_ char_type;
typedef typename std::basic_string<T_> str_type;
typedef typename std::basic_string<T_>::size_type size_type;
typedef typename ext::basic_strfunc<T_> _Func;
private:
str_type m_Str;
str_type m_Mask;
str_type m_CurKey;
public:
explicit basic_kformat(const str_type& str)
: m_Str(str), m_Mask(str.length(), '_')
{
}
const str_type& str() const
{
return m_Str;
}
basic_kformat& operator %(const char_type* szKey)
{
m_CurKey = szKey;
return *this;
}
basic_kformat& operator *(const char_type* szValue)
{
if (!m_CurKey.empty())
{
ext::string::size_type pos = 0;
ext::string::size_type key_len = m_CurKey.length();
ext::string::size_type value_len = _Func::len(szValue);
while ((pos = m_Str.find(m_CurKey, pos)) != ext::string::npos)
{
if (m_Mask.substr(pos, key_len).find('X') == ext::string::npos)
{
// replace since we didn't replace here before
m_Str.replace(pos, key_len, szValue, value_len);
m_Mask.replace(pos, key_len, value_len, 'X');
pos += value_len;
}
else
{
// skip since we already replaced in this area
pos += key_len;
}
}
}
return *this;
}
basic_kformat& operator *(const str_type& strValue)
{
return (*this * strValue.c_str());
}
basic_kformat& operator *(int nValue)
{
std::basic_ostringstream<char_type> oss;
oss << nValue;
return (*this * oss.str().c_str());
}
};
template<typename T_>
inline const std::basic_string<T_>& str(const basic_kformat<T_>& format)
{
return format.str();
}
}
template<typename T_>
inline std::basic_ostream<T_>& operator <<(std::basic_ostream<T_>& os, const ext::basic_kformat<T_>& format)
{
os << format.str();
return os;
}
#endif // HISTORYSTATS_GUARD__FORMAT_H
|