Skip to content

Add snprintf fallback for float/double formatting (IAR support)#2

Merged
willson556 merged 1 commit into
mainfrom
feature/iar-float-to-chars-fallback
Jul 7, 2026
Merged

Add snprintf fallback for float/double formatting (IAR support)#2
willson556 merged 1 commit into
mainfrom
feature/iar-float-to-chars-fallback

Conversation

@willson556

Copy link
Copy Markdown
Member

Description

Guards the four floating-point StringBuilder formatters (append/prepend for float and double) behind a new NFX_STRINGBUILDER_HAS_FLOAT_TO_CHARS macro. When floating std::to_chars is unavailable, they fall back to std::snprintf("%.*g", max_digits10, ...) into a stack buffer and reuse the existing append/prepend(std::string_view) path. Integer formatting is unchanged.

The macro defaults to 0 under IAR (__IAR_SYSTEMS_ICC__) and 1 elsewhere, and can be overridden by the consumer before including the header.

Motivation and Context

Integer std::to_chars is universally available, but the floating-point overloads are not: IAR's libc++ (bxarm) ships std::to_chars(float/double) as deleted functions. Because StringBuilder.cpp instantiates every append/prepend overload, the library currently fails to cross-compile under IAR even when the consumer never formats a float or double.

This came up integrating nfx-stringbuilder into an embedded (arm-cm7-iar) firmware build for MQTT topic construction, which only ever appends integers and string literals.

How Has This Been Tested?

  • Cross-compiled the consuming static library (which includes and links StringBuilder.cpp) with IAR bxarm 9.50.2 for arm-cm7 — fails without this change (deleted std::to_chars), links cleanly with it.
  • Host build (Clang, C++17) unaffected: the default keeps the std::to_chars path.

🤖 Generated with Claude Code

…ut floating std::to_chars

Integer std::to_chars is universally available, but the floating-point
overloads are not implemented everywhere: IAR's libc++ (bxarm) ships
std::to_chars(float/double) as deleted functions, so StringBuilder.cpp
fails to compile there even when no float/double is ever formatted.

Guard the four floating-point formatters (append/prepend for float and
double) behind NFX_STRINGBUILDER_HAS_FLOAT_TO_CHARS, which defaults to 0
under IAR and 1 elsewhere, and can be overridden by the consumer. When
disabled, format via std::snprintf("%.*g", max_digits10, ...) into a
stack buffer and reuse the existing append/prepend(string_view) path.
Integer formatting is untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Comment thread include/nfx/detail/string/StringBuilder.inl
@willson556 willson556 merged commit f737f55 into main Jul 7, 2026
9 checks passed
@willson556 willson556 deleted the feature/iar-float-to-chars-fallback branch July 7, 2026 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants