Skip to content

Commit 5fa33bb

Browse files
committed
Fixes to compile with C++ exceptions globally disabled.
1 parent f3e452e commit 5fa33bb

File tree

5 files changed

+482
-235
lines changed

5 files changed

+482
-235
lines changed

include/quickcpplib/algorithm/string.hpp

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Distributed under the Boost Software License, Version 1.0.
2727

2828
#include "../span.hpp"
2929

30+
#include "../cpp_feature.h"
31+
3032
#include <algorithm>
3133
#include <locale>
3234
#include <string>
@@ -77,7 +79,11 @@ This lets one pack one byte of input into two bytes of output.
7779
unsigned const char *in = (unsigned const char *) _in;
7880
static constexpr char table[] = "0123456789abcdef";
7981
if(outlen < inlen * 2)
82+
#ifdef __cpp_exceptions
8083
throw std::invalid_argument("Output buffer too small.");
84+
#else
85+
abort();
86+
#endif
8187
if(inlen >= 2)
8288
{
8389
for(size_t n = inlen - 2; n <= inlen - 2; n -= 2)
@@ -102,7 +108,10 @@ This lets one pack one byte of input into two bytes of output.
102108
QUICKCPPLIB_TEMPLATE(class CharType, class T)
103109
QUICKCPPLIB_TREQUIRES(QUICKCPPLIB_TPRED(sizeof(T) == 1), QUICKCPPLIB_TPRED(std::is_trivially_copyable<T>::value),
104110
QUICKCPPLIB_TPRED(!std::is_const<CharType>::value))
105-
inline size_t to_hex_string(span::span<CharType> out, const span::span<T> in) { return to_hex_string(out.data(), out.size(), in.data(), in.size()); }
111+
inline size_t to_hex_string(span::span<CharType> out, const span::span<T> in)
112+
{
113+
return to_hex_string(out.data(), out.size(), in.data(), in.size());
114+
}
106115
//! \overload
107116
QUICKCPPLIB_TEMPLATE(class T)
108117
QUICKCPPLIB_TREQUIRES(QUICKCPPLIB_TPRED(sizeof(T) == 1), QUICKCPPLIB_TPRED(std::is_trivially_copyable<T>::value))
@@ -136,20 +145,30 @@ This lets one pack one byte of input into two bytes of output.
136145
inline size_t from_hex_string(T *out, size_t outlen, const CharType *in, size_t inlen)
137146
{
138147
if(inlen % 2)
148+
#ifdef __cpp_exceptions
139149
throw std::invalid_argument("Input buffer not multiple of two.");
150+
#else
151+
abort();
152+
#endif
140153
if(outlen < inlen / 2)
154+
#ifdef __cpp_exceptions
141155
throw std::invalid_argument("Output buffer too small.");
156+
#else
157+
abort();
158+
#endif
142159
bool is_invalid = false;
143-
auto fromhex = [&is_invalid](CharType c) -> unsigned char {
160+
auto fromhex = [&is_invalid](CharType c) -> unsigned char
161+
{
144162
#if 1
145163
// ASCII starting from 48 is 0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
146164
// 48 65 97
147-
static constexpr unsigned char table[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // +10 = 58
148-
255, 255, 255, 255, 255, 255, 255, // +7 = 65
149-
10, 11, 12, 13, 14, 15, // +6 = 71
150-
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
151-
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // +26 = 97
152-
10, 11, 12, 13, 14, 15};
165+
static constexpr unsigned char table[] = {
166+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // +10 = 58
167+
255, 255, 255, 255, 255, 255, 255, // +7 = 65
168+
10, 11, 12, 13, 14, 15, // +6 = 71
169+
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
170+
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // +26 = 97
171+
10, 11, 12, 13, 14, 15};
153172
unsigned char r = 255;
154173
if(c >= 48 && c <= 102)
155174
r = table[c - 48];
@@ -163,7 +182,11 @@ This lets one pack one byte of input into two bytes of output.
163182
return c - 'a' + 10;
164183
if(c >= 'A' && c <= 'F')
165184
return c - 'A' + 10;
185+
#ifdef __cpp_exceptions
166186
throw std::invalid_argument("Input is not hexadecimal.");
187+
#else
188+
abort();
189+
#endif
167190
#endif
168191
};
169192
const auto bulklen = inlen / 2 - (inlen / 2) % 4;
@@ -176,23 +199,27 @@ This lets one pack one byte of input into two bytes of output.
176199
c[1] = fromhex(in[n * 2 + 1]);
177200
c[2] = fromhex(in[n * 2 + 2]);
178201
c[3] = fromhex(in[n * 2 + 3]);
179-
out[n] = (T)((c[0] << 4) | c[1]);
202+
out[n] = (T) ((c[0] << 4) | c[1]);
180203
c[4] = fromhex(in[n * 2 + 4]);
181204
c[5] = fromhex(in[n * 2 + 5]);
182-
out[n + 1] = (T)((c[2] << 4) | c[3]);
205+
out[n + 1] = (T) ((c[2] << 4) | c[3]);
183206
c[6] = fromhex(in[n * 2 + 6]);
184207
c[7] = fromhex(in[n * 2 + 7]);
185-
out[n + 2] = (T)((c[4] << 4) | c[5]);
186-
out[n + 3] = (T)((c[6] << 4) | c[7]);
208+
out[n + 2] = (T) ((c[4] << 4) | c[5]);
209+
out[n + 3] = (T) ((c[6] << 4) | c[7]);
187210
}
188211
}
189212
for(size_t n = bulklen; n < inlen / 2; n++)
190213
{
191214
auto c1 = fromhex(in[n * 2]), c2 = fromhex(in[n * 2 + 1]);
192-
out[n] = (T)((c1 << 4) | c2);
215+
out[n] = (T) ((c1 << 4) | c2);
193216
}
194217
if(is_invalid)
218+
#ifdef __cpp_exceptions
195219
throw std::invalid_argument("Input is not hexadecimal.");
220+
#else
221+
abort();
222+
#endif
196223
return inlen / 2;
197224
}
198225
} // namespace string

0 commit comments

Comments
 (0)