OpenMPTL - Helper Library
C++ Microprocessor Template Library
poorman_cstring.hpp
Go to the documentation of this file.
1 /*
2  * OpenMPTL - C++ Microprocessor Template Library
3  *
4  * Copyright (C) 2012-2017 Axel Burri <axel@tty0.ch>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef POORMAN_CSTRING_HPP_INCLUDED
22 #define POORMAN_CSTRING_HPP_INCLUDED
23 
24 #include <type_traits>
25 
26 namespace poorman {
27 
28 /**
29  * Prints "value" as hexadecimal number to buf, '0'-padded, zero
30  * terminated.
31  *
32  * Negative values are printed as two's complement.
33  *
34  * NOTE: sizeof(buf) must be at least n+1
35  */
36 template<typename Tp>
37 void itoa_hex(char * buf, Tp value, int n = sizeof(Tp)*2, char padding = '0') {
38  using value_type = typename std::make_unsigned<Tp>::type;
39  value_type val = (value_type)value;
40  unsigned v;
41 
42  buf[n--] = 0;
43  if(val == 0)
44  buf[n--] = '0';
45  while(n >= 0 && val) {
46  v = val & 0xf;
47  buf[n--] = v < 10 ? '0' + v : 'a' + v - 10;
48  val >>= 4;
49  };
50  while(n >= 0) {
51  buf[n--] = padding;
52  }
53  if(val) {
54  buf[0] = '~';
55  }
56 }
57 
58 /**
59  * Prints n digits of "value" to buf, right aligned, "padding"-padded,
60  * zero terminated.
61  *
62  * Prints '~' as first character if "value" has more digits than n.
63  *
64  * NOTE: sizeof(buf) must be at least n+1
65  */
66 template<typename Tp>
67 typename std::enable_if< std::is_unsigned< Tp >::value >::type
68 itoa(char * buf, Tp value, int n, char padding = '0') {
69  buf[n--] = 0;
70  if(value == 0)
71  buf[n--] = '0';
72  else {
73  while(n >= 0 && value > 0) {
74  buf[n--] = '0' + (value % 10);
75  value /= 10;
76  }
77  }
78  while(n >= 0) {
79  buf[n--] = padding;
80  }
81  if(value > 0) {
82  buf[0] = '~';
83  }
84 }
85 template<typename Tp>
86 typename std::enable_if< std::is_signed< Tp >::value >::type
87 itoa(char * buf, Tp value, int n, char padding = '0') {
88  *buf++ = '-';
89  itoa(buf, (unsigned)(-value), n-1, padding);
90 }
91 
92 } // namespace poorman
93 
94 #endif // POORMAN_CSTRING_HPP_INCLUDED
void itoa_hex(char *buf, Tp value, int n=sizeof(Tp) *2, char padding='0')
Prints "value" as hexadecimal number to buf, &#39;0&#39;-padded, zero terminated.
Definition: poorman_cstring.hpp:37
Definition: poorman_cstring.hpp:26
std::enable_if< std::is_unsigned< Tp >::value >::type itoa(char *buf, Tp value, int n, char padding='0')
Prints n digits of "value" to buf, right aligned, "padding"-padded, zero terminated.
Definition: poorman_cstring.hpp:68