Go to file
2022-02-15 10:35:17 +00:00
const_fmt Merge pull request 'feature/int_hex' (#6) from feature/int_hex into master 2022-02-15 10:35:17 +00:00
examples Fixed bug in hex formatting, wrote tests for hex int formatting 2022-02-15 11:34:49 +01:00
test Fixed bug in hex formatting, wrote tests for hex int formatting 2022-02-15 11:34:49 +01:00
.gitignore Added build folder to gitignore 2022-02-09 17:21:46 +01:00
.gitmodules Started adding unit tests 2021-11-21 22:46:19 +01:00
CMakeLists.txt Added examples folder and CMakeLists.txt in repo root directory 2022-02-13 17:40:33 +01:00
LICENSE Added license 2022-02-13 23:48:17 +01:00
README.md Renamed a bunch of identifiers to make sure there are no clashes with software this might be used in (fmt -> const_fmt) 2022-02-13 17:56:09 +01:00

const_fmt

An extremely lightweight library, intentionally resembling fmtlib as much as possible. This is achieved by moving as much of the formatting process as possible to compile time.

Meant for systems with very few resources, such as embedded systems.

Overview

During compile-time, the string to be formatted is preprocessed to the point only the actual values to be formatted have to be written (If they are not available at compile time).

For example One number: {:03}; And another one: {:05.3} is preprocessed into One number: 000; And another one: 00.000. This is returned as a std::array<char, N>, where N is automatically evaluated. The only code executed at compile time then formats the numbers and writes them into their place in the array.

Disclaimer: The actual formatting code is largely shamelessly stolen from fmtlib.

Including in a project

In order to keep it as lightweight and optimizable as possible, const_fmt is implemented as a header-only library.

This means that using it in a project is as simple as cloning this repo (or e.g. adding it as a submodule) and adding the repository root folder to the compiler include directories.

Building and running the tests

  1. Initialize the googletest framework submodule
$ git submodule update --init
  1. Create the build directory
$ cmake -B build -S .
  1. Build the project
$ cmake --build build/
  1. Run the tests
$ ctest --test-dir build/

Limitations

Only a relatively limited subset of the fmtlib syntax is recognized (for now anyway). In particular, there is no support for positional arguments, alignment, chrono format specs and custom const_format specifications.

By nature of the library design, which forces compile-time preprocessing of the const_format string, no dynamic width or dynamic precision can be implemented.