Skip to content

Commit ab3be8e

Browse files
committed
Refactor write_chars function to use void return type and replace error handling with assertions.
- Changed the return type of `write_chars` from `bool` to `void`. - Replaced error handling with `assert` statements to ensure the function's correctness. - Updated the function documentation to explain the maximum length of the conversion of a number to a string.
1 parent af400ae commit ab3be8e

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/common/buffer.hpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,26 +300,30 @@ class base_buffer {
300300
}
301301

302302
template<typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
303-
bool write_chars(T value) {
303+
void write_chars(T value) {
304+
/*
305+
** Maximum length of the conversion of a number to a string. Must be
306+
** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT.
307+
** (For a long long int, this is 19 digits plus a sign and a final '\0',
308+
** adding to 21. For a long double, it can go to a sign, 33 digits,
309+
** the dot, an exponent letter, an exponent sign, 5 exponent digits,
310+
** and a final '\0', adding to 43.)
311+
*/
304312
static constexpr size_t MAX_NUMBER_2_STR = 44;
305313
auto space = pair_.prepare(MAX_NUMBER_2_STR);
306314
auto* b = space.first;
307315
#ifndef _MSC_VER //std::to_chars in C++17: gcc and clang only integral types supported
308316
if constexpr (std::is_floating_point_v<T>) {
309317
int len = std::snprintf(b, MAX_NUMBER_2_STR, "%.16g", value);
310-
if (len < 0)
311-
return false;
318+
assert(len >= 0);
312319
commit(len);
313-
return true;
314320
} else
315321
#endif
316322
{
317323
auto* e = b + MAX_NUMBER_2_STR;
318324
auto res = std::to_chars(b, e, value);
319-
if (res.ec != std::errc())
320-
return false;
325+
assert(res.ec == std::errc());
321326
commit(res.ptr - b);
322-
return true;
323327
}
324328
}
325329

0 commit comments

Comments
 (0)