src/zserio/StringConvertUtil.h
Line | Count | Source |
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 | | #include "zserio/Types.h" |
11 | | |
12 | | namespace zserio |
13 | | { |
14 | | |
15 | | namespace detail |
16 | | { |
17 | | |
18 | | /** |
19 | | * Converts integer value to string into the given buffer. |
20 | | * |
21 | | * The string is filled from backwards starting at the 24th byte. |
22 | | * |
23 | | * \return Beginning of the resulting string which is null-terminated. |
24 | | */ |
25 | | template <typename T, |
26 | | typename std::enable_if<std::is_unsigned<T>::value && !std::is_same<T, bool>::value, int>::type = 0> |
27 | | const char* convertIntToString(std::array<char, 24>& buffer, T value, bool isNegative) |
28 | 2.47k | { |
29 | 2.47k | static const std::array<char, 201> DIGITS_100_10 = { |
30 | 2.47k | "0001020304050607080910111213141516171819" |
31 | 2.47k | "2021222324252627282930313233343536373839" |
32 | 2.47k | "4041424344454647484950515253545556575859" |
33 | 2.47k | "6061626364656667686970717273747576777879" |
34 | 2.47k | "8081828384858687888990919293949596979899"}; |
35 | 2.47k | static const std::array<char, 11> DIGITS_1 = {"0123456789"}; |
36 | | |
37 | 2.47k | auto bufferEnd = buffer.end(); |
38 | 2.47k | *(--bufferEnd) = '\0'; // always terminate with '\0' |
39 | | |
40 | 5.01k | while (value >= 100) |
41 | 2.53k | { |
42 | 2.53k | const unsigned int index = static_cast<unsigned int>((value % 100) * 2); |
43 | 2.53k | value /= 100; |
44 | 2.53k | *(--bufferEnd) = DIGITS_100_10[index + 1]; |
45 | 2.53k | *(--bufferEnd) = DIGITS_100_10[index]; |
46 | 2.53k | } |
47 | | |
48 | 2.47k | if (value < 10) |
49 | 1.67k | { |
50 | 1.67k | *(--bufferEnd) = DIGITS_1[static_cast<unsigned int>(value)]; |
51 | 1.67k | } |
52 | 806 | else |
53 | 806 | { |
54 | 806 | const unsigned int index = static_cast<unsigned int>(value * 2); |
55 | 806 | *(--bufferEnd) = DIGITS_100_10[index + 1]; |
56 | 806 | *(--bufferEnd) = DIGITS_100_10[index]; |
57 | 806 | } |
58 | | |
59 | 2.47k | if (isNegative) |
60 | 238 | { |
61 | 238 | *(--bufferEnd) = '-'; |
62 | 238 | } |
63 | | |
64 | 2.47k | return &(*bufferEnd); |
65 | 2.47k | } |
66 | | |
67 | | } // namespace detail |
68 | | |
69 | | /** |
70 | | * Converts unsigned integral value to string and writes the result to the given buffer. |
71 | | * |
72 | | * Note that the buffer is filled from behind. |
73 | | * |
74 | | * \param buffer Buffer to fill with the string representation of the given value. |
75 | | * \param value Value to convert. |
76 | | * |
77 | | * \return Pointer to the beginning of the resulting string. |
78 | | */ |
79 | | template <typename T, typename std::enable_if<std::is_unsigned<T>::value, int>::type = 0> |
80 | | const char* convertIntToString(std::array<char, 24>& buffer, T value) |
81 | 1.96k | { |
82 | 1.96k | return detail::convertIntToString(buffer, value, false); |
83 | 1.96k | } |
84 | | |
85 | | /** |
86 | | * Converts signed integral value to string and writes the result to the given buffer. |
87 | | * |
88 | | * Note that the buffer is filled from behind. |
89 | | * |
90 | | * \param buffer Buffer to fill with the string representation of the given value. |
91 | | * \param value Value to convert. |
92 | | * |
93 | | * \return Pointer to the beginning of the resulting string. |
94 | | */ |
95 | | template <typename T, typename std::enable_if<std::is_signed<T>::value, int>::type = 0> |
96 | | const char* convertIntToString(std::array<char, 24>& buffer, T value) |
97 | 511 | { |
98 | 511 | using unsigned_type = typename std::make_unsigned<T>::type; |
99 | 511 | unsigned_type absValue = static_cast<unsigned_type>(value); |
100 | 511 | const bool isNegative = value < 0; |
101 | 511 | if (isNegative) |
102 | 238 | { |
103 | 238 | absValue = static_cast<unsigned_type>(0 - absValue); |
104 | 238 | } |
105 | | |
106 | 511 | return detail::convertIntToString(buffer, absValue, isNegative); |
107 | 511 | } |
108 | | |
109 | | /** |
110 | | * Converts float to string and writes the result to the given buffer. |
111 | | * |
112 | | * Note that only five three digits after point are used and that the buffers are filled from behind. |
113 | | * |
114 | | * \param integerPartBuffer Buffer to fill with the string representation of the integer part. |
115 | | * \param floatingPartBuffer Buffer to fill with the string representation of the floating part. |
116 | | * \param value Value to convert. |
117 | | * \param floatingPartString Reference where to fill pointer to the beginning of the floating part in string. |
118 | | * \param integerPartString Reference where to fill pointer to the beginning of the integer part in string. |
119 | | */ |
120 | | inline void convertFloatToString(std::array<char, 24>& integerPartBuffer, |
121 | | std::array<char, 24>& floatingPartBuffer, float value, const char*& integerPartString, |
122 | | const char*& floatingPartString) |
123 | 21 | { |
124 | 21 | if (value >= static_cast<float>(std::numeric_limits<int64_t>::max())) |
125 | 2 | { |
126 | 2 | integerPartString = "+Inf"; |
127 | 2 | floatingPartString = nullptr; |
128 | 2 | } |
129 | 19 | else if (value <= static_cast<float>(std::numeric_limits<int64_t>::min())) |
130 | 1 | { |
131 | 1 | integerPartString = "-Inf"; |
132 | 1 | floatingPartString = nullptr; |
133 | 1 | } |
134 | 18 | else |
135 | 18 | { |
136 | 18 | const int64_t integerPart = static_cast<int64_t>(value); |
137 | 18 | const int64_t floatingPart = |
138 | 18 | static_cast<int64_t>((value - static_cast<float>(integerPart)) * 1e3F); // 3 digits |
139 | 18 | const int64_t floatingPartAbs = (floatingPart < 0) ? 0 - floatingPart1 : floatingPart17 ; |
140 | 18 | integerPartString = convertIntToString(integerPartBuffer, integerPart); |
141 | 18 | floatingPartString = convertIntToString(floatingPartBuffer, floatingPartAbs); |
142 | 18 | } |
143 | 21 | } |
144 | | |
145 | | /** |
146 | | * Converts bool value to boolalpha C-string ("true" or "false"). |
147 | | * |
148 | | * \param value Value to convert. |
149 | | * |
150 | | * \return C-string representation of the given bool value. |
151 | | */ |
152 | | inline const char* convertBoolToString(bool value) |
153 | 23 | { |
154 | 23 | return value ? "true"13 : "false"10 ; |
155 | 23 | } |
156 | | |
157 | | /** |
158 | | * Converts an integral value to string using the given allocator. Defined for convenience. |
159 | | * |
160 | | * \param value Value to convert. |
161 | | * \param allocator Allocator to use for the string allocation. |
162 | | * |
163 | | * \return String representation of the given integral value. |
164 | | */ |
165 | | template <typename ALLOC, typename T> |
166 | | string<ALLOC> toString(T value, const ALLOC& allocator = ALLOC()) |
167 | 497 | { |
168 | 497 | std::array<char, 24> buffer = {}; |
169 | 497 | return string<ALLOC>(convertIntToString(buffer, value), allocator); |
170 | 497 | } |
171 | | |
172 | | /** |
173 | | * Converts a boolean value to string using the given allocator. Defined for convenience. |
174 | | * |
175 | | * Note that in contrast to std::to_string, this behaves as STL streams with boolalpha flag and produces |
176 | | * "true" and "false" strings. |
177 | | * |
178 | | * \param value Value to convert. |
179 | | * \param allocator Allocator to use for the string allocation. |
180 | | */ |
181 | | template <typename ALLOC> |
182 | | string<ALLOC> toString(bool value, const ALLOC& allocator = ALLOC()) |
183 | 21 | { |
184 | 21 | return string<ALLOC>(convertBoolToString(value), allocator); |
185 | 21 | } |
186 | | |
187 | | /** |
188 | | * Converts an integral (or a boolean) value to string. Convenience wrapper to call without allocator. |
189 | | * |
190 | | * \param value Value to convert. |
191 | | * |
192 | | * \return String representation of the given value. |
193 | | */ |
194 | | template <typename T> |
195 | | string<std::allocator<char>> toString(T value) |
196 | 287 | { |
197 | 287 | return toString<std::allocator<char>>(value); |
198 | 287 | } |
199 | | |
200 | | } // namespace zserio |
201 | | |
202 | | #endif // ifndef ZSERIO_STRING_CONVERT_UTIL_H_INC |