[ox/std] Make sfmt usable as constexpr

This commit is contained in:
Gary Talent 2021-12-11 16:39:40 -06:00
parent 6da3d3b8f5
commit 5ed806f4c0
3 changed files with 11 additions and 5 deletions

View File

@ -8,6 +8,12 @@
#pragma once #pragma once
#if __has_include(<cassert>)
#include <cassert>
#else
#define assert(e) {}
#endif
#include "defines.hpp" #include "defines.hpp"
#include "error.hpp" #include "error.hpp"

View File

@ -45,7 +45,7 @@ class BString {
constexpr char &operator[](std::size_t i) noexcept; constexpr char &operator[](std::size_t i) noexcept;
Error append(const char *str, std::size_t strLen) noexcept; constexpr Error append(const char *str, std::size_t strLen) noexcept;
constexpr char *data() noexcept; constexpr char *data() noexcept;
@ -153,14 +153,14 @@ constexpr char &BString<buffLen>::operator[](std::size_t i) noexcept {
} }
template<std::size_t buffLen> template<std::size_t buffLen>
Error BString<buffLen>::append(const char *str, std::size_t strLen) noexcept { constexpr Error BString<buffLen>::append(const char *str, std::size_t strLen) noexcept {
Error err; Error err;
auto currentLen = len(); auto currentLen = len();
if (cap() < currentLen + strLen + 1) { if (cap() < currentLen + strLen + 1) {
strLen = cap() - currentLen; strLen = cap() - currentLen;
err = OxError(1, "Insufficient space for full string"); err = OxError(1, "Insufficient space for full string");
} }
ox_memcpy(m_buff + currentLen, str, strLen); ox_strncpy(m_buff + currentLen, str, strLen);
// make sure last element is a null terminator // make sure last element is a null terminator
m_buff[currentLen + strLen] = 0; m_buff[currentLen + strLen] = 0;
return err; return err;

View File

@ -169,8 +169,8 @@ constexpr Fmt<segementCnt> fmtSegments(const char *fmt) noexcept {
template<typename StringType = String, typename ...Args> template<typename StringType = String, typename ...Args>
[[nodiscard]] [[nodiscard]]
StringType sfmt(const char *fmt, Args... args) noexcept { constexpr StringType sfmt(const char *fmt, Args... args) noexcept {
oxAssert(ox::detail::argCount(fmt) == sizeof...(args), "Argument count mismatch."); assert(ox::detail::argCount(fmt) == sizeof...(args));
StringType out; StringType out;
const auto fmtSegments = ox::detail::fmtSegments<sizeof...(args)+1>(fmt); const auto fmtSegments = ox::detail::fmtSegments<sizeof...(args)+1>(fmt);
const auto &firstSegment = fmtSegments.segments[0]; const auto &firstSegment = fmtSegments.segments[0];