[ox/std] Cleanup

This commit is contained in:
2026-04-29 01:43:41 -05:00
parent 8c7d8cd08b
commit 0885919dbd
2 changed files with 40 additions and 34 deletions
+10 -4
View File
@@ -62,13 +62,19 @@ struct [[nodiscard]] Error {
return errCode; return errCode;
} }
constexpr void throwException() const; /**
* Checks the Error to see if is not OK, and throws the Error as an ox::Exception if not.
*/
constexpr void checkThrow() const;
constexpr Error reoriginate( constexpr Error reoriginate(
ErrorCode const pErrCode, ErrorCode const pErrCode,
CString const pMsg = nullptr, CString const pMsg = nullptr,
std::source_location const &pSrc = std::source_location::current()) const noexcept { std::source_location const &pSrc = std::source_location::current()) const noexcept {
return Error{pErrCode, pMsg, pSrc}; if (errCode) {
return Error{pErrCode, pMsg, pSrc};
}
return Error{};
} }
constexpr Error reoriginate( constexpr Error reoriginate(
@@ -102,7 +108,7 @@ struct Exception: public std::exception {
explicit Exception( explicit Exception(
ErrorCode const errCode, ErrorCode const errCode,
CString msg, CString const msg,
std::source_location const &src = std::source_location::current()) noexcept: std::source_location const &src = std::source_location::current()) noexcept:
src{src}, src{src},
msg{msg}, msg{msg},
@@ -124,7 +130,7 @@ struct Exception: public std::exception {
}; };
constexpr void Error::throwException() const { constexpr void Error::checkThrow() const {
if (errCode) [[unlikely]] { if (errCode) [[unlikely]] {
throw Exception{*this}; throw Exception{*this};
} }
+30 -30
View File
@@ -31,27 +31,27 @@ namespace ox {
namespace detail { namespace detail {
template<bool force = false> template<bool force = false>
constexpr StringView toStringView(const StringView &s) noexcept { constexpr StringView toStringView(StringViewCR s) noexcept {
return s; return s;
} }
template<bool force = false> template<bool force = false>
constexpr StringView toStringView(const char *s) noexcept { constexpr StringView toStringView(CString const s) noexcept {
return s; return s;
} }
template<bool force = false, std::size_t size> template<bool force = false, std::size_t size>
constexpr StringView toStringView(const IString<size> &s) noexcept { constexpr StringView toStringView(IString<size> const &s) noexcept {
return s; return s;
} }
template<bool force = false> template<bool force = false>
constexpr StringView toStringView(ox::StringLiteral s) noexcept { constexpr StringView toStringView(StringLiteral const &s) noexcept {
return s; return s;
} }
template<bool force = false, std::size_t size> template<bool force = false, std::size_t size>
constexpr StringView toStringView(const BasicString<size> &s) noexcept { constexpr StringView toStringView(BasicString<size> const &s) noexcept {
return s; return s;
} }
@@ -62,20 +62,20 @@ constexpr
#else #else
inline inline
#endif #endif
StringView toStringView(const std::string &s) noexcept { StringView toStringView(std::string const &s) noexcept {
return s.c_str(); return s.c_str();
} }
#endif #endif
#if __has_include(<QString>) #if __has_include(<QString>)
template<bool force = false> template<bool force = false>
inline StringView toStringView(const QString &s) noexcept { inline StringView toStringView(QString const &s) noexcept {
return s.toUtf8().data(); return s.toUtf8().data();
} }
#endif #endif
template<bool force = false> template<bool force = false>
constexpr StringView toStringView(const auto&) noexcept requires(force) { constexpr StringView toStringView(auto const&) noexcept requires(force) {
return "<unstringable>"; return "<unstringable>";
} }
@@ -83,10 +83,10 @@ class FmtArg {
private: private:
static constexpr auto DataSz = 23; static constexpr auto DataSz = 23;
ox::Array<char, DataSz> dataStr{}; Array<char, DataSz> dataStr{};
template<typename T> template<typename T>
constexpr StringView sv(const T &v, ox::Span<char> dataStr) noexcept { constexpr StringView sv(T const &v, Span<char> dataStr) noexcept {
if constexpr(is_bool_v<T>) { if constexpr(is_bool_v<T>) {
return v ? "true" : "false"; return v ? "true" : "false";
} else if constexpr(is_integer_v<T>) { } else if constexpr(is_integer_v<T>) {
@@ -99,25 +99,25 @@ class FmtArg {
} }
public: public:
const StringView out = nullptr; StringView const out;
template<typename T> template<typename T>
constexpr FmtArg(const T &v) noexcept: out(sv(v, dataStr)) { constexpr FmtArg(T const &v) noexcept: out(sv(v, dataStr)) {
} }
}; };
[[nodiscard]] [[nodiscard]]
constexpr uint64_t argCount(StringView str) noexcept { constexpr uint64_t argCount(StringViewCR str) noexcept {
uint64_t cnt = 0; uint64_t cnt = 0;
const auto prev = [str](std::size_t i) -> char { auto const prev = [str](std::size_t i) -> char {
if (i > 0) { if (i > 0) {
return str[i - 1]; return str[i - 1];
} else { } else {
return '\0'; return '\0';
} }
}; };
const auto next = [str](std::size_t i) -> char { auto const next = [str](std::size_t i) -> char {
if (i < str.bytes() - 1) { if (i < str.bytes() - 1) {
return str[i + 1]; return str[i + 1];
} else { } else {
@@ -133,14 +133,14 @@ constexpr uint64_t argCount(StringView str) noexcept {
} }
struct FmtSegment { struct FmtSegment {
const char *str = nullptr; CString str{};
unsigned length = 0; unsigned length{};
constexpr bool operator==(const FmtSegment &o) const noexcept { constexpr bool operator==(FmtSegment const &o) const noexcept {
return length == o.length && ox::strncmp(str, o.str, length) == 0; return length == o.length && ox::strncmp(str, o.str, length) == 0;
} }
constexpr bool operator!=(const FmtSegment &o) const noexcept { constexpr bool operator!=(FmtSegment const &o) const noexcept {
return length != o.length || ox::strncmp(str, o.str, length) != 0; return length != o.length || ox::strncmp(str, o.str, length) != 0;
} }
}; };
@@ -148,9 +148,9 @@ struct FmtSegment {
template<std::size_t sz> template<std::size_t sz>
struct Fmt { struct Fmt {
static constexpr std::size_t size = sz; static constexpr std::size_t size = sz;
ox::Array<FmtSegment, sz> segments; Array<FmtSegment, sz> segments;
constexpr bool operator==(const Fmt<sz> &o) const noexcept { constexpr bool operator==(Fmt const &o) const noexcept {
for (std::size_t i = 0; i < sz; ++i) { for (std::size_t i = 0; i < sz; ++i) {
if (segments[i] != o.segments[i]) { if (segments[i] != o.segments[i]) {
return false; return false;
@@ -162,16 +162,16 @@ struct Fmt {
template<std::size_t segementCnt> template<std::size_t segementCnt>
[[nodiscard]] [[nodiscard]]
constexpr Fmt<segementCnt> fmtSegments(StringView fmt) noexcept { constexpr Fmt<segementCnt> fmtSegments(StringViewCR fmt) noexcept {
Fmt<segementCnt> out; Fmt<segementCnt> out;
const auto prev = [fmt](std::size_t i) -> char { auto const prev = [fmt](std::size_t const i) -> char {
if (i > 0) { if (i > 0) {
return fmt[i - 1]; return fmt[i - 1];
} else { } else {
return '\0'; return '\0';
} }
}; };
const auto next = [fmt](std::size_t i) -> char { auto const next = [fmt](std::size_t const i) -> char {
if (i < fmt.bytes() - 1) { if (i < fmt.bytes() - 1) {
return fmt[i + 1]; return fmt[i + 1];
} else { } else {
@@ -197,23 +197,23 @@ constexpr Fmt<segementCnt> fmtSegments(StringView fmt) noexcept {
template<typename StringType = String, typename ...Args> template<typename StringType = String, typename ...Args>
[[nodiscard]] [[nodiscard]]
constexpr StringType sfmt(StringView fmt, Args&&... args) noexcept { constexpr StringType sfmt(StringViewCR fmt, Args&&... args) noexcept {
assert(ox::detail::argCount(fmt) == sizeof...(args)); assert(ox::detail::argCount(fmt) == sizeof...(args));
StringType out; StringType out;
const auto fmtSegments = ox::detail::fmtSegments<sizeof...(args)+1>(fmt); auto const fmtSegments = detail::fmtSegments<sizeof...(args)+1>(fmt);
const auto &firstSegment = fmtSegments.segments[0]; auto const &firstSegment = fmtSegments.segments[0];
std::ignore = out.append(firstSegment.str, firstSegment.length); std::ignore = out.append(firstSegment.str, firstSegment.length);
const detail::FmtArg elements[sizeof...(args)] = {args...}; detail::FmtArg const elements[sizeof...(args)] = {args...};
for (size_t i = 0; i < fmtSegments.size - 1; ++i) { for (size_t i = 0; i < fmtSegments.size - 1; ++i) {
std::ignore = out.append(elements[i].out); std::ignore = out.append(elements[i].out);
const auto &s = fmtSegments.segments[i + 1]; auto const &s = fmtSegments.segments[i + 1];
std::ignore = out.append(s.str, s.length); std::ignore = out.append(s.str, s.length);
} }
return out; return out;
} }
template<typename T = String> template<typename T = String>
constexpr Result<T> join(auto const&d, auto const&list) { constexpr Result<T> join(auto const &d, auto const &list) {
if (!list.size()) { if (!list.size()) {
return T(""); return T("");
} }