Zserio C++ runtime library  1.0.2
Built for Zserio 2.14.1
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  {
59  *(--bufferEnd) = '-';
60  }
61 
62  return &(*bufferEnd);
63 }
64 
65 } // namespace detail
66 
77 template <typename T, typename std::enable_if<std::is_unsigned<T>::value, int>::type = 0>
78 const char* convertIntToString(std::array<char, 24>& buffer, T value)
79 {
80  return detail::convertIntToString(buffer, value, false);
81 }
82 
93 template <typename T, typename std::enable_if<std::is_signed<T>::value, int>::type = 0>
94 const char* convertIntToString(std::array<char, 24>& buffer, T value)
95 {
96  using unsigned_type = typename std::make_unsigned<T>::type;
97  unsigned_type absValue = static_cast<unsigned_type>(value);
98  const bool isNegative = value < 0;
99  if (isNegative)
100  {
101  absValue = static_cast<unsigned_type>(0 - absValue);
102  }
103 
104  return detail::convertIntToString(buffer, absValue, isNegative);
105 }
106 
118 inline void convertFloatToString(std::array<char, 24>& integerPartBuffer,
119  std::array<char, 24>& floatingPartBuffer, float value, const char*& integerPartString,
120  const char*& floatingPartString)
121 {
122  if (value >= static_cast<float>(std::numeric_limits<int64_t>::max()))
123  {
124  integerPartString = "+Inf";
125  floatingPartString = nullptr;
126  }
127  else if (value <= static_cast<float>(std::numeric_limits<int64_t>::min()))
128  {
129  integerPartString = "-Inf";
130  floatingPartString = nullptr;
131  }
132  else
133  {
134  const int64_t integerPart = static_cast<int64_t>(value);
135  const int64_t floatingPart =
136  static_cast<int64_t>((value - static_cast<float>(integerPart)) * 1e3F); // 3 digits
137  const int64_t floatingPartAbs = (floatingPart < 0) ? 0 - floatingPart : floatingPart;
138  integerPartString = convertIntToString(integerPartBuffer, integerPart);
139  floatingPartString = convertIntToString(floatingPartBuffer, floatingPartAbs);
140  }
141 }
142 
150 inline const char* convertBoolToString(bool value)
151 {
152  return value ? "true" : "false";
153 }
154 
163 template <typename ALLOC, typename T>
164 string<ALLOC> toString(T value, const ALLOC& allocator = ALLOC())
165 {
166  std::array<char, 24> buffer = {};
167  return string<ALLOC>(convertIntToString(buffer, value), allocator);
168 }
169 
179 template <typename ALLOC>
180 string<ALLOC> toString(bool value, const ALLOC& allocator = ALLOC())
181 {
182  return string<ALLOC>(convertBoolToString(value), allocator);
183 }
184 
192 template <typename T>
194 {
195  return toString<std::allocator<char>>(value);
196 }
197 
198 } // namespace zserio
199 
200 #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())