From dc5d223c24c52310f69f1df3c176ead91288f561 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 31 May 2021 13:11:22 +0100 Subject: CTF fixed string null termination --- lib/compileTimeFormatter.h | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/compileTimeFormatter.h b/lib/compileTimeFormatter.h index 6e8c813..17da62e 100644 --- a/lib/compileTimeFormatter.h +++ b/lib/compileTimeFormatter.h @@ -7,6 +7,7 @@ #include #include #include // IWYU pragma: export +#include namespace AdHoc { // Template char utils @@ -236,21 +237,49 @@ namespace AdHoc { // New C++20 implementation namespace support { - template class basic_fixed_string : public std::array { + template class basic_fixed_string { public: // cppcheck-suppress noExplicitConstructor constexpr basic_fixed_string(const CharT (&str)[N + 1]) { for (decltype(N) x = 0; x < N; x++) { - this->at(x) = str[x]; + arr.at(x) = str[x]; } + arr.at(N) = '\0'; } constexpr basic_fixed_string(const CharT * str, decltype(N) len) { for (decltype(N) x = 0; x < len; x++) { - this->at(x) = str[x]; + arr.at(x) = str[x]; } + arr.at(N) = '\0'; } + constexpr const char * + s() const + { + return arr.data(); + } + constexpr operator const char *() const + { + return s(); + } + constexpr std::string_view + v() const + { + return {arr.data(), arr.size() - 1}; + } + constexpr auto & + operator[](std::size_t n) const + { + return arr[n]; + } + constexpr auto + size() const + { + return arr.size() - 1; + } + + std::array arr; }; template -- cgit v1.2.3