Zserio C++ runtime library  1.0.1
Built for Zserio 2.14.0
StringConvertUtil.h
Go to the documentation of this file.
1 #ifndef ZSERIO_STRING_CONVERT_UTIL_H_INC
2 #define ZSERIO_STRING_CONVERT_UTIL_H_INC
3 
4 #include <array>
5 #include <limits>
6 #include <sstream>
7 
8 #include "zserio/RebindAlloc.h"
9 #include "zserio/String.h"
10 
11 namespace zserio
12 {
13 
14 namespace detail
15 {
16 
24 template <typename T,
25  typename std::enable_if<std::is_unsigned<T>::value && !std::is_same<T, bool>::value, int>::type = 0>
26 const char* convertIntToString(std::array<char, 24>& buffer, T value, bool isNegative)
27 {
28  static const std::array<char, 201> DIGITS = {
29  "0001020304050607080910111213141516171819"
30  "2021222324252627282930313233343536373839"
31  "4041424344454647484950515253545556575859"
32  "6061626364656667686970717273747576777879"
33  "8081828384858687888990919293949596979899"};
34 
35  auto bufferEnd = buffer.end();
36  *--bufferEnd = 0; // always terminate with '\0'
37 
38  while (value >= 100)
39  {
40  const unsigned int index = static_cast<unsigned int>((value % 100) * 2);
41  value /= 100;
42  *--bufferEnd = DIGITS[index + 1];
43  *--bufferEnd = DIGITS[index];
44  }
45 
46  if (value < 10)
47  {
48  *--bufferEnd = static_cast<char>('0' + value);
49  }
50  else
51  {
52  const unsigned int index = static_cast<unsigned int>(value * 2);
53  *--bufferEnd = DIGITS[index + 1];
54  *--bufferEnd = DIGITS[index];
55  }
56 
57  if (isNegative)
58  *--bufferEnd = '-';
59 
60  return &(*bufferEnd);
61 }
62 
63 } // namespace detail
64 
75 template <typename T, typename std::enable_if<std::is_unsigned<T>::value, int>::type = 0>
76 const char* convertIntToString(std::array<char, 24>& buffer, T value)
77 {
78  return detail::convertIntToString(buffer, value, false);
79 }
80 
91 template <typename T, typename std::enable_if<std::is_signed<T>::value, int>::type = 0>
92 const char* convertIntToString(std::array<char, 24>& buffer, T value)
93 {
94  using unsigned_type = typename std::make_unsigned<T>::type;
95  unsigned_type absValue = static_cast<unsigned_type>(value);
96  const bool isNegative = value < 0;
97  if (isNegative)
98  absValue = static_cast<unsigned_type>(0 - absValue);
99 
100  return detail::convertIntToString(buffer, absValue, isNegative);
101 }
102 
114 inline void convertFloatToString(std::array<char, 24>& integerPartBuffer,
115  std::array<char, 24>& floatingPartBuffer, float value, const char*& integerPartString,
116  const char*& floatingPartString)
117 {
118  if (value >= static_cast<float>(std::numeric_limits<int64_t>::max()))
119  {
120  integerPartString = "+Inf";
121  floatingPartString = nullptr;
122  }
123  else if (value <= static_cast<float>(std::numeric_limits<int64_t>::min()))
124  {
125  integerPartString = "-Inf";
126  floatingPartString = nullptr;
127  }
128  else
129  {
130  const int64_t integerPart = static_cast<int64_t>(value);
131  const int64_t floatingPart =
132  static_cast<int64_t>((value - static_cast<float>(integerPart)) * 1e3F); // 3 digits
133  const int64_t floatingPartAbs = (floatingPart < 0) ? 0 - floatingPart : floatingPart;
134  integerPartString = convertIntToString(integerPartBuffer, integerPart);
135  floatingPartString = convertIntToString(floatingPartBuffer, floatingPartAbs);
136  }
137 }
138 
146 inline const char* convertBoolToString(bool value)
147 {
148  return value ? "true" : "false";
149 }
150 
159 template <typename ALLOC, typename T>
160 string<ALLOC> toString(T value, const ALLOC& allocator = ALLOC())
161 {
162  std::array<char, 24> buffer = {};
163  return string<ALLOC>(convertIntToString(buffer, value), allocator);
164 }
165 
175 template <typename ALLOC>
176 string<ALLOC> toString(bool value, const ALLOC& allocator = ALLOC())
177 {
178  return string<ALLOC>(convertBoolToString(value), allocator);
179 }
180 
188 template <typename T>
190 {
191  return toString<std::allocator<char>>(value);
192 }
193 
194 } // namespace zserio
195 
196 #endif // ifndef ZSERIO_STRING_CONVERT_UTIL_H_INC
const char * convertIntToString(std::array< char, 24 > &buffer, T value)
std::basic_string< char, std::char_traits< char >, RebindAlloc< ALLOC, char > > string
Definition: String.h:17
const char * convertBoolToString(bool value)
void convertFloatToString(std::array< char, 24 > &integerPartBuffer, std::array< char, 24 > &floatingPartBuffer, float value, const char *&integerPartString, const char *&floatingPartString)
string< ALLOC > toString(T value, const ALLOC &allocator=ALLOC())