6 Commits

3 changed files with 77 additions and 3 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
build build
cmake-build-debug cmake-build-debug
cmake-build-release cmake-build-release
cmake-build-relwithdebinfo
.idea .idea

View File

@@ -11,8 +11,7 @@ During compile-time, the string to be formatted is preprocessed to the point onl
have to be written (If they are not available at compile time). 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`. 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 This is returned as a `std::array<char, N>`, where `N` is automatically evaluated. The only code executed at runtime then formats the numbers and writes them into their places in the array.
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`. Disclaimer: The actual formatting code is largely shamelessly stolen from `fmtlib`.
@@ -48,8 +47,11 @@ $ ctest --test-dir build/
## Limitations ## Limitations
For the compile time preprocessing of format strings non non-type template parameters are heavily relied upon,
which means C++20 is required.
Only a relatively limited subset of the `fmtlib` syntax is recognized (for now anyway). In particular, 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. float formatting in decimal and integer formatting in decimal, binary and hexadecimal are supported.
By nature of the library design, which forces compile-time preprocessing of the const_format string, no dynamic width or 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. dynamic precision can be implemented.

71
const_fmt/std_lib.h Normal file
View File

@@ -0,0 +1,71 @@
#ifndef CONST_FMT_STD_LIB_H
#define CONST_FMT_STD_LIB_H
#ifdef CONST_FMT_NO_STD_LIB
#include <stdint.h>
namespace std {
using size_t = uint16_t;
// TODO: Is std::size_t really the best bet here?
template <typename data_t, std::size_t t_size>
class array {
public:
// Constructors
array() = default;
array(const array& other) = default;
array(array&& other) = default;
// Operators
array operator=(const array& other) = default;
array operator=(array&& other) = default;
// Element access
data_t& operator[](std::size_t index) {
return m_data[index];
}
const data_t& operator[](std::size_t index) const {
return m_data[index];
}
// Iterators
using iterator = data_t*;
using const_iterator = const data_t*;
iterator begin() {
return &(m_data[0]);
}
iterator end() {
return (&(m_data[t_size-1]) + 1);
}
const iterator cbegin() const {
return &(m_data[0]);
}
const iterator cend() const {
return (&(m_data[t_size-1]) + 1);
}
private:
data_t m_data[t_size];
};
} // namespace std
#endif
#endif // CONST_FMT_STD_LIB_H