QuickCppLib 0.10
Eliminate all the tedious hassle when making state-of-the-art C++ 14 - 23 libraries!
Loading...
Searching...
No Matches
Miscellaneous utilities

Small, dependency free helpers which the rest of the library reaches for. More...

Classes

class  quickcpplib::_xxx::algorithm::hamming_ecc::hamming_ecc< blocksize >
 Calculates the single error correcting Hamming Error Correcting Code for a blocksize block of bytes. For example, a hamming_ecc<8> would be the very common 72,64 Hamming code used in ECC RAM, or a hamming_ecc<4096> would be for a 32784,32768 Hamming code. More...

Functions

template<class CharType, class T, typename std::enable_if<(sizeof(T)==1), bool >::type = true, typename std::enable_if<(std::is_trivially_copyable< T >::value), bool >::type = true, typename std::enable_if<(!std::is_const< CharType >::value), bool >::type = true>
size_t quickcpplib::_xxx::algorithm::string::to_hex_string (CharType *out, size_t outlen, const T *_in, size_t inlen)
 Converts a number to a hex string. Out buffer can be same as in buffer.
template<class T, class CharType, typename std::enable_if<(sizeof(T)==1), bool >::type = true, typename std::enable_if<(std::is_trivially_copyable< T >::value), bool >::type = true, typename std::enable_if<(!std::is_const< T >::value), bool >::type = true>
size_t quickcpplib::_xxx::algorithm::string::from_hex_string (T *out, size_t outlen, const CharType *in, size_t inlen)
 Converts a hex string to a number. Out buffer can be same as in buffer.

Detailed Description

Small, dependency free helpers which the rest of the library reaches for.

They are grouped here by what they are rather than by where they live, because several parts of the library and its tests use them: number to hex string encoding and the case conversions below, the Hamming ECC in hamming_ecc.hpp, and so on. (Three \ingroup utils elsewhere in the library used to point at a group nobody defined, which doxygen 1.9 accepted silently and doxygen 1.10 and later report as "Found non-existing group 'utils'", failing the documentation build.)

Function Documentation

◆ to_hex_string()

template<class CharType, class T, typename std::enable_if<(sizeof(T)==1), bool >::type = true, typename std::enable_if<(std::is_trivially_copyable< T >::value), bool >::type = true, typename std::enable_if<(!std::is_const< CharType >::value), bool >::type = true>
size_t quickcpplib::_xxx::algorithm::string::to_hex_string ( CharType * out,
size_t outlen,
const T * _in,
size_t inlen )
inline

Converts a number to a hex string. Out buffer can be same as in buffer.

Note that the character range used is a 16 item table of:

0123456789abcdef

This lets one pack one byte of input into two bytes of output.

Complexity
O(N) where N is the length of the number.
Errors returnable
Throws exception if output buffer is too small for input.
88 {
89 unsigned const char *in = (unsigned const char *) _in;
90 static constexpr char table[] = "0123456789abcdef";
91 if(outlen < inlen * 2)
92#ifdef __cpp_exceptions
93 throw std::invalid_argument("Output buffer too small.");
94#else
95 abort();
96#endif
97 if(inlen >= 2)
98 {
99 for(size_t n = inlen - 2; n <= inlen - 2; n -= 2)
100 {
101 out[n * 2 + 3] = table[in[n + 1] & 0xf];
102 out[n * 2 + 2] = table[(in[n + 1] >> 4) & 0xf];
103 out[n * 2 + 1] = table[in[n] & 0xf];
104 out[n * 2 + 0] = table[(in[n] >> 4) & 0xf];
105 }
106 }
107 if(inlen & 1)
108 {
109 out[1] = table[in[0] & 0xf];
110 out[0] = table[(in[0] >> 4) & 0xf];
111 }
112 return inlen * 2;
113 }

◆ from_hex_string()

template<class T, class CharType, typename std::enable_if<(sizeof(T)==1), bool >::type = true, typename std::enable_if<(std::is_trivially_copyable< T >::value), bool >::type = true, typename std::enable_if<(!std::is_const< T >::value), bool >::type = true>
size_t quickcpplib::_xxx::algorithm::string::from_hex_string ( T * out,
size_t outlen,
const CharType * in,
size_t inlen )
inline

Converts a hex string to a number. Out buffer can be same as in buffer.

Note that this routine is about 43% slower than to_hex_string(), half of which is due to input validation.

Complexity
O(N) where N is the length of the string.
Errors returnable
Throws exception if output buffer is too small for input or input size is not multiple of two.
156 {
157 if(inlen % 2)
158#ifdef __cpp_exceptions
159 throw std::invalid_argument("Input buffer not multiple of two.");
160#else
161 abort();
162#endif
163 if(outlen < inlen / 2)
164#ifdef __cpp_exceptions
165 throw std::invalid_argument("Output buffer too small.");
166#else
167 abort();
168#endif
169 bool is_invalid = false;
170 auto fromhex = [&is_invalid](CharType c) -> unsigned char
171 {
172#if 1
173 // ASCII starting from 48 is 0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\‍]^_`abcdefghijklmnopqrstuvwxyz{|}~
174 // 48 65 97
175 static constexpr unsigned char table[] = {
176 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // +10 = 58
177 255, 255, 255, 255, 255, 255, 255, // +7 = 65
178 10, 11, 12, 13, 14, 15, // +6 = 71
179 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
180 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // +26 = 97
181 10, 11, 12, 13, 14, 15};
182 unsigned char r = 255;
183 if(c >= 48 && c <= 102)
184 r = table[c - 48];
185 if(r == 255)
186 is_invalid = true;
187 return r;
188#else
189 if(c >= '0' && c <= '9')
190 return c - '0';
191 if(c >= 'a' && c <= 'f')
192 return c - 'a' + 10;
193 if(c >= 'A' && c <= 'F')
194 return c - 'A' + 10;
195#ifdef __cpp_exceptions
196 throw std::invalid_argument("Input is not hexadecimal.");
197#else
198 abort();
199#endif
200#endif
201 };
202 const auto bulklen = inlen / 2 - (inlen / 2) % 4;
203 if(bulklen >= 4)
204 {
205 for(size_t n = 0; n < bulklen; n += 4)
206 {
207 unsigned char c[8];
208 c[0] = fromhex(in[n * 2]);
209 c[1] = fromhex(in[n * 2 + 1]);
210 c[2] = fromhex(in[n * 2 + 2]);
211 c[3] = fromhex(in[n * 2 + 3]);
212 out[n] = (T) ((c[0] << 4) | c[1]);
213 c[4] = fromhex(in[n * 2 + 4]);
214 c[5] = fromhex(in[n * 2 + 5]);
215 out[n + 1] = (T) ((c[2] << 4) | c[3]);
216 c[6] = fromhex(in[n * 2 + 6]);
217 c[7] = fromhex(in[n * 2 + 7]);
218 out[n + 2] = (T) ((c[4] << 4) | c[5]);
219 out[n + 3] = (T) ((c[6] << 4) | c[7]);
220 }
221 }
222 for(size_t n = bulklen; n < inlen / 2; n++)
223 {
224 auto c1 = fromhex(in[n * 2]), c2 = fromhex(in[n * 2 + 1]);
225 out[n] = (T) ((c1 << 4) | c2);
226 }
227 if(is_invalid)
228#ifdef __cpp_exceptions
229 throw std::invalid_argument("Input is not hexadecimal.");
230#else
231 abort();
232#endif
233 return inlen / 2;
234 }