17 template <
typename Stream>
18 void send(Stream & s,
char const * str)
20 for (; *str != 0; ++str)
24 template <
typename Stream>
25 void send(Stream & s,
const std::string& str)
31 template <
typename Stream>
32 void send_bool(Stream & s,
bool value)
34 send(s, value?
"true":
"false");
37 template <
typename Stream,
typename Un
signed>
38 void send_bin_text(Stream & s, Unsigned v, uint8_t width = 0,
char fill =
'0')
53 for (; v != 0; v >>= 1)
55 buf[i++] =
'0' + (v & 0x1);
66 template <
typename Stream,
typename Un
signed>
67 void send_hex(Stream & s, Unsigned v, uint8_t width = 0,
char fill =
'0')
69 static char const digits[] =
"0123456789ABCDEF";
84 for (; v != 0; v >>= 4)
86 buf[i++] = digits[v & 0xF];
97 template <
typename Stream,
typename Signed>
98 void send_shex(Stream & s, Signed v, uint8_t width = 0,
char fill =
' ')
100 static char const digits[] =
"0123456789ABCDEF";
105 bool negative = (v < 0);
115 for (; v != 0; v >>= 4)
117 buf[i++] = digits[v & 0xF];
131 template <
typename Stream,
typename Integer>
132 void send_int(Stream & s, Integer v, uint8_t width = 0,
char fill =
' ')
136 bool negative =
false;
150 for (; v != 0; v /= 10)
152 buf[i++] = (v % 10) +
'0';
166 template <
typename Stream,
typename T>
167 void send_bin(Stream & s, T
const & t)
169 char const * ptr =
reinterpret_cast<char const *
>(&t);
170 for (uint8_t i = 0; i <
sizeof t; ++i)
174 template <
typename Stream>
175 uint8_t readline(Stream & s, uint8_t * buffer, uint8_t len)
187 uint8_t ch = s.read();
200 bool bufcmp(uint8_t
const * buf, uint8_t len,
char const (&pattern)[N])
202 for (
int i = 0; i < N - 1; ++i)
203 if (buf[i] != pattern[i])
208 template <
typename Stream,
typename Pattern>
213 : m_out(out), m_pattern(pattern)
215 this->write_literal();
220 if(m_pattern.empty())
224 this->write_literal();
230 if(m_pattern.empty())
235 this->write_literal();
241 if(m_pattern.empty())
246 this->write_literal();
252 if(m_pattern.empty())
254 m_out.write(v ?
'1' :
'0');
256 this->write_literal();
262 if(m_pattern.empty())
265 static const int ft_size = 12;
266 char float_text[ft_size];
267 char * float_text_ptr = float_text;
268 snprintf(float_text, ft_size,
"%g", v);
269 while (*float_text_ptr)
270 m_out.write(*float_text_ptr++);
273 this->write_literal();
279 if(m_pattern.empty())
282 static const int dt_size = 20;
283 char double_text[dt_size];
284 char * double_text_ptr = double_text;
285 snprintf(double_text, dt_size,
"%f", v);
286 while (*double_text_ptr)
287 m_out.write(*double_text_ptr++);
290 this->write_literal();
294 template <
typename T >
295 typename std::enable_if<std::is_integral<T>::value,
format_impl&>::type operator%(T
const& t)
297 if (m_pattern.empty())
return *
this;
298 char f = m_pattern.top();
306 }
else if (f ==
'b') {
311 if (f >=
'0' && f <=
'9') width = f -
'0';
313 send_hex(m_out, t, width);
315 send_bin_text(m_out, t, width);
317 send_int(m_out, t, width);
318 if (!m_pattern.empty()) m_pattern.pop();
319 this->write_literal();
327 for (; !m_pattern.empty(); m_pattern.pop())
329 char ch = m_pattern.top();
367 return *m_pattern == 0;
381 char const * m_pattern;
386 template <
typename Stream>