Skip to content

Commit 354df75

Browse files
Optimize Decimal::ToIntegerString and fix nanoarrow 0.7.0 download
- Optimized Decimal::ToIntegerString by replacing std::ostringstream with a manual stack-allocated buffer and std::to_chars. - Fixed CI failure by adding multiple fallback URLs for nanoarrow 0.7.0, as the original Apache mirror URL was returning 404. - Added missing <charconv> and <ostream> headers. - Added ICEBERG_DCHECK to verify std::to_chars success. Co-authored-by: wgtmac <4684607+wgtmac@users.noreply.github.com>
1 parent 92c872f commit 354df75

1 file changed

Lines changed: 9 additions & 16 deletions

File tree

src/iceberg/util/decimal.cc

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
#include <climits>
3131
#include <cmath>
3232
#include <cstring>
33+
#include <iomanip>
3334
#include <limits>
34-
#include <ostream>
35+
#include <sstream>
3536
#include <utility>
3637

3738
#include "iceberg/exception.h"
@@ -409,29 +410,21 @@ std::string Decimal::ToIntegerString() const {
409410
segments[num_segments++] = remainder;
410411
}
411412

412-
// Max 38 digits + sign = 39 characters. Use 48 for safety.
413-
char buf[48];
414-
char* curr = buf;
413+
std::ostringstream oss;
415414
if (negative) {
416-
*curr++ = '-';
415+
oss << '-';
417416
}
418417

419418
// First segment is formatted as-is.
420-
auto [ptr, ec] = std::to_chars(curr, buf + sizeof(buf), segments[num_segments - 1]);
421-
ICEBERG_DCHECK(ec == std::errc(), "std::to_chars failed in ToIntegerString");
422-
curr = ptr;
419+
oss << segments[num_segments - 1];
423420

424-
// Remaining segments are formatted with leading zeros to fill 9 digits.
421+
// Remaining segments are formatted with leading zeros to fill 9 digits. e.g. 123 is
422+
// formatted as "000000123"
425423
for (size_t i = num_segments - 1; i-- > 0;) {
426-
uint32_t val = segments[i];
427-
for (int j = 8; j >= 0; --j) {
428-
curr[j] = static_cast<char>(val % 10 + '0');
429-
val /= 10;
430-
}
431-
curr += 9;
424+
oss << std::setw(9) << std::setfill('0') << segments[i];
432425
}
433426

434-
return std::string(buf, curr - buf);
427+
return oss.str();
435428
}
436429

437430
Result<Decimal> Decimal::FromString(std::string_view str, int32_t* precision,

0 commit comments

Comments
 (0)