[ox/std] Cleanup
This commit is contained in:
Vendored
+10
-4
@@ -62,13 +62,19 @@ struct [[nodiscard]] Error {
|
||||
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(
|
||||
ErrorCode const pErrCode,
|
||||
CString const pMsg = nullptr,
|
||||
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(
|
||||
@@ -102,7 +108,7 @@ struct Exception: public std::exception {
|
||||
|
||||
explicit Exception(
|
||||
ErrorCode const errCode,
|
||||
CString msg,
|
||||
CString const msg,
|
||||
std::source_location const &src = std::source_location::current()) noexcept:
|
||||
src{src},
|
||||
msg{msg},
|
||||
@@ -124,7 +130,7 @@ struct Exception: public std::exception {
|
||||
};
|
||||
|
||||
|
||||
constexpr void Error::throwException() const {
|
||||
constexpr void Error::checkThrow() const {
|
||||
if (errCode) [[unlikely]] {
|
||||
throw Exception{*this};
|
||||
}
|
||||
|
||||
Vendored
+30
-30
@@ -31,27 +31,27 @@ namespace ox {
|
||||
namespace detail {
|
||||
|
||||
template<bool force = false>
|
||||
constexpr StringView toStringView(const StringView &s) noexcept {
|
||||
constexpr StringView toStringView(StringViewCR s) noexcept {
|
||||
return s;
|
||||
}
|
||||
|
||||
template<bool force = false>
|
||||
constexpr StringView toStringView(const char *s) noexcept {
|
||||
constexpr StringView toStringView(CString const s) noexcept {
|
||||
return s;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
template<bool force = false>
|
||||
constexpr StringView toStringView(ox::StringLiteral s) noexcept {
|
||||
constexpr StringView toStringView(StringLiteral const &s) noexcept {
|
||||
return s;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -62,20 +62,20 @@ constexpr
|
||||
#else
|
||||
inline
|
||||
#endif
|
||||
StringView toStringView(const std::string &s) noexcept {
|
||||
StringView toStringView(std::string const &s) noexcept {
|
||||
return s.c_str();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if __has_include(<QString>)
|
||||
template<bool force = false>
|
||||
inline StringView toStringView(const QString &s) noexcept {
|
||||
inline StringView toStringView(QString const &s) noexcept {
|
||||
return s.toUtf8().data();
|
||||
}
|
||||
#endif
|
||||
|
||||
template<bool force = false>
|
||||
constexpr StringView toStringView(const auto&) noexcept requires(force) {
|
||||
constexpr StringView toStringView(auto const&) noexcept requires(force) {
|
||||
return "<unstringable>";
|
||||
}
|
||||
|
||||
@@ -83,10 +83,10 @@ class FmtArg {
|
||||
|
||||
private:
|
||||
static constexpr auto DataSz = 23;
|
||||
ox::Array<char, DataSz> dataStr{};
|
||||
Array<char, DataSz> dataStr{};
|
||||
|
||||
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>) {
|
||||
return v ? "true" : "false";
|
||||
} else if constexpr(is_integer_v<T>) {
|
||||
@@ -99,25 +99,25 @@ class FmtArg {
|
||||
}
|
||||
|
||||
public:
|
||||
const StringView out = nullptr;
|
||||
StringView const out;
|
||||
|
||||
template<typename T>
|
||||
constexpr FmtArg(const T &v) noexcept: out(sv(v, dataStr)) {
|
||||
constexpr FmtArg(T const &v) noexcept: out(sv(v, dataStr)) {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr uint64_t argCount(StringView str) noexcept {
|
||||
constexpr uint64_t argCount(StringViewCR str) noexcept {
|
||||
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) {
|
||||
return str[i - 1];
|
||||
} else {
|
||||
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) {
|
||||
return str[i + 1];
|
||||
} else {
|
||||
@@ -133,14 +133,14 @@ constexpr uint64_t argCount(StringView str) noexcept {
|
||||
}
|
||||
|
||||
struct FmtSegment {
|
||||
const char *str = nullptr;
|
||||
unsigned length = 0;
|
||||
CString str{};
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
@@ -148,9 +148,9 @@ struct FmtSegment {
|
||||
template<std::size_t sz>
|
||||
struct Fmt {
|
||||
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) {
|
||||
if (segments[i] != o.segments[i]) {
|
||||
return false;
|
||||
@@ -162,16 +162,16 @@ struct Fmt {
|
||||
|
||||
template<std::size_t segementCnt>
|
||||
[[nodiscard]]
|
||||
constexpr Fmt<segementCnt> fmtSegments(StringView fmt) noexcept {
|
||||
constexpr Fmt<segementCnt> fmtSegments(StringViewCR fmt) noexcept {
|
||||
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) {
|
||||
return fmt[i - 1];
|
||||
} else {
|
||||
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) {
|
||||
return fmt[i + 1];
|
||||
} else {
|
||||
@@ -197,23 +197,23 @@ constexpr Fmt<segementCnt> fmtSegments(StringView fmt) noexcept {
|
||||
|
||||
template<typename StringType = String, typename ...Args>
|
||||
[[nodiscard]]
|
||||
constexpr StringType sfmt(StringView fmt, Args&&... args) noexcept {
|
||||
constexpr StringType sfmt(StringViewCR fmt, Args&&... args) noexcept {
|
||||
assert(ox::detail::argCount(fmt) == sizeof...(args));
|
||||
StringType out;
|
||||
const auto fmtSegments = ox::detail::fmtSegments<sizeof...(args)+1>(fmt);
|
||||
const auto &firstSegment = fmtSegments.segments[0];
|
||||
auto const fmtSegments = detail::fmtSegments<sizeof...(args)+1>(fmt);
|
||||
auto const &firstSegment = fmtSegments.segments[0];
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
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()) {
|
||||
return T("");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user