Skip to content

Generic to_string/formatters with format-spec rendering in hertz (1.2.0)#2

Merged
cleishm merged 2 commits into
mainfrom
feat/format-spec-rendering
Jul 10, 2026
Merged

Generic to_string/formatters with format-spec rendering in hertz (1.2.0)#2
cleishm merged 2 commits into
mainfrom
feat/format-spec-rendering

Conversation

@cleishm

@cleishm cleishm commented Jul 10, 2026

Copy link
Copy Markdown
Owner

Summary

Mirrors thermo-cpp's formatter generalization (cleishm/thermo-cpp#5): the six per-type to_string overloads and six std::formatter specializations are replaced by one generic template each, covering every frequency<Rep, Precision> instantiation (including user-defined ones like frequency<double, std::mega>). An empty format spec keeps today's exact-count form; any floating-point spec renders the value in hertz:

std::format("{}", kilohertz(433));        // "433kHz" (exact stored value, unchanged)
std::format("{:.1f}", millihertz(1500));  // "1.5Hz"
std::format("{:.2f}", millihertz(1534));  // "1.53Hz"
  • New internal _si_prefix trait maps precision ratios femto→tera; an unmapped precision with an empty spec throws format_error from parse() (a compile error for literal format strings), while explicit specs work for any precision.
  • Output strings are byte-identical for all existing typedefs; non-empty specs were previously compile errors, so the spec path is purely additive.
  • Version synced to 1.2.0 across CMakeLists.txt, idf_component.yml, docs/Doxyfile, and vcpkg-port/vcpkg.json (they had drifted: 1.1.2 / 1.1.3 / 1.0.0 / 1.0.0).

Test plan

  • New test pins all six empty-spec output strings byte-for-byte (previously untested)
  • New spec-rendering tests including width pass-through
  • to_string coverage extended to terahertz
  • Non-format build path verified: -DCONFIG_FREQUENCY_STD_FORMAT=0 -fsyntax-only
  • 43/43 tests pass

cleishm added 2 commits July 10, 2026 15:07
Replace the six per-type to_string overloads and six std::formatter
specializations with one generic template each, covering every
frequency<Rep, Precision> instantiation. An empty format spec keeps the
exact-count form ("433kHz"); any floating-point spec renders the value
in hertz: std::format("{:.1f}", millihertz(1500)) == "1.5Hz".

Output strings are unchanged for all existing typedefs; specs were
previously compile errors, so the spec path is purely additive.
@cleishm cleishm merged commit aabf807 into main Jul 10, 2026
6 checks passed
@cleishm cleishm deleted the feat/format-spec-rendering branch July 10, 2026 19:12
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.

1 participant