Compare commits

..

8 Commits

30 changed files with 153 additions and 224 deletions

View File

@ -104,6 +104,11 @@ using size_t = decltype(alignof(int));
#endif #endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#endif
namespace cityhash::detail { namespace cityhash::detail {
template<typename T> template<typename T>
@ -671,4 +676,8 @@ constexpr uint128 CityHash128(const char *s, size_t len) noexcept {
} }
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif // CITY_HASH_H_ #endif // CITY_HASH_H_

View File

@ -1,4 +1,9 @@
cmake_minimum_required(VERSION 3.10)
if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
# enable warnings
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunsafe-buffer-usage")
endif()
add_library( add_library(
OxClArgs OxClArgs

View File

@ -12,16 +12,17 @@
namespace ox { namespace ox {
ClArgs::ClArgs(int argc, const char **args) noexcept { ClArgs::ClArgs(int argc, const char **args) noexcept {
auto const argv = ox::SpanView{args, static_cast<size_t>(argc)};
for (auto i = 0u; i < static_cast<unsigned>(argc); ++i) { for (auto i = 0u; i < static_cast<unsigned>(argc); ++i) {
auto arg = String(args[i]); auto arg = String(argv[i]);
if (arg[0] == '-') { if (arg[0] == '-') {
while (arg[0] == '-' && arg.len()) { while (arg[0] == '-' && arg.len()) {
arg = arg.substr(1); arg = arg.substr(1);
} }
m_bools[arg] = true; m_bools[arg] = true;
// parse additional arguments // parse additional arguments
if (i < static_cast<unsigned>(argc) && args[i + 1]) { if (i < static_cast<unsigned>(argc) && argv[i + 1]) {
auto val = String(args[i + 1]); auto val = String(argv[i + 1]);
if (val.len() && val[i] != '-') { if (val.len() && val[i] != '-') {
if (val == "false") { if (val == "false") {
m_bools[arg] = false; m_bools[arg] = false;

View File

@ -395,7 +395,7 @@ Result<Buffer> writeMC(auto const&val, std::size_t buffReserveSz = 2 * units::KB
} }
Error writeMC(char *buff, std::size_t buffLen, auto const&val, std::size_t *sizeOut = nullptr) noexcept { Error writeMC(char *buff, std::size_t buffLen, auto const&val, std::size_t *sizeOut = nullptr) noexcept {
CharBuffWriter bw(buff, buffLen); CharBuffWriter bw{{buff, buffLen}};
oxReturnError(writeMC(bw, val)); oxReturnError(writeMC(bw, val));
if (sizeOut) { if (sizeOut) {
*sizeOut = bw.tellp(); *sizeOut = bw.tellp();

View File

@ -1,3 +1,9 @@
if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
# enable warnings
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunsafe-buffer-usage")
endif()
if(OX_USE_STDLIB AND OX_ENABLE_TRACEHOOK) if(OX_USE_STDLIB AND OX_ENABLE_TRACEHOOK)
add_library( add_library(
OxTraceHook SHARED OxTraceHook SHARED

View File

@ -8,6 +8,10 @@
#pragma once #pragma once
#include "def.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
template<typename It, typename T> template<typename It, typename T>
@ -40,4 +44,6 @@ constexpr OutIt copy_n(It in, Size cnt, OutIt out) {
return out; return out;
} }
} }
OX_CLANG_NOWARN_END

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include "bit.hpp" #include "bit.hpp"
#include "def.hpp"
#include "error.hpp" #include "error.hpp"
#include "initializerlist.hpp" #include "initializerlist.hpp"
#include "iterator.hpp" #include "iterator.hpp"
@ -17,6 +18,8 @@
#include "types.hpp" #include "types.hpp"
#include "utility.hpp" #include "utility.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
template<typename T, std::size_t ArraySize> template<typename T, std::size_t ArraySize>
@ -35,6 +38,10 @@ class Array {
public: public:
constexpr Array() noexcept = default; constexpr Array() noexcept = default;
template<typename ...Args>
constexpr Array(Args ...list) noexcept: m_items{std::move(list)...} {
}
constexpr Array(std::initializer_list<T> list) noexcept; constexpr Array(std::initializer_list<T> list) noexcept;
constexpr Array(const Array &other); constexpr Array(const Array &other);
@ -200,3 +207,5 @@ constexpr bool Array<T, ArraySize>::contains(const T &v) const {
} }
} }
OX_CLANG_NOWARN_END

View File

@ -9,9 +9,12 @@
#pragma once #pragma once
#include "bit.hpp" #include "bit.hpp"
#include "def.hpp"
#include "cstrops.hpp" #include "cstrops.hpp"
#include "iterator.hpp" #include "iterator.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox::detail { namespace ox::detail {
class BaseStringView { class BaseStringView {
@ -202,7 +205,7 @@ class BaseStringView {
} }
[[nodiscard]] [[nodiscard]]
constexpr auto operator[](std::size_t i) const noexcept { constexpr auto &operator[](std::size_t i) const noexcept {
return m_str[i]; return m_str[i];
} }
@ -215,3 +218,5 @@ class BaseStringView {
}; };
} }
OX_CLANG_NOWARN_END

View File

@ -14,6 +14,8 @@
#include "vector.hpp" #include "vector.hpp"
#include "writer.hpp" #include "writer.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
extern template class Vector<char>; extern template class Vector<char>;
@ -100,15 +102,11 @@ class CharBuffWriter {
char *m_buff = nullptr; char *m_buff = nullptr;
public: public:
template<std::size_t sz> explicit constexpr CharBuffWriter(ox::Span<char> buff) noexcept:
explicit constexpr CharBuffWriter(ox::Array<char, sz> &buff) noexcept:
m_cap(buff.size()), m_cap(buff.size()),
m_buff(buff.data()) { m_buff(buff.data()) {
} }
explicit constexpr CharBuffWriter(char *buff, std::size_t size) noexcept: m_cap(size), m_buff(buff) {
}
constexpr ox::Error seekp(std::size_t p) noexcept { constexpr ox::Error seekp(std::size_t p) noexcept {
m_it = p; m_it = p;
return {}; return {};
@ -237,3 +235,5 @@ extern template class WriterT<BufferWriter>;
extern template class WriterT<CharBuffWriter>; extern template class WriterT<CharBuffWriter>;
} }
OX_CLANG_NOWARN_END

View File

@ -8,9 +8,12 @@
#pragma once #pragma once
#include "def.hpp"
#include "types.hpp" #include "types.hpp"
#include "typetraits.hpp" #include "typetraits.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
template<typename T1, typename T2> template<typename T1, typename T2>
@ -113,3 +116,5 @@ constexpr int lastIndexOf(const auto &str, int character, std::size_t maxLen = 0
} }
} }
OX_CLANG_NOWARN_END

View File

@ -71,6 +71,17 @@ constexpr void oxAssert(const ox::Error&, const char*) noexcept {}
#define ox_alloca(size) __builtin_alloca(size) #define ox_alloca(size) __builtin_alloca(size)
#endif #endif
#define OX_PRAGMA(x) _Pragma(#x)
#ifdef __clang__
#define OX_CLANG_NOWARN_BEGIN(warnoption) \
OX_PRAGMA(clang diagnostic push) \
OX_PRAGMA(clang diagnostic ignored #warnoption)
#define OX_CLANG_NOWARN_END OX_PRAGMA(clang diagnostic pop)
#else
#define OX_CLANG_NOWARN_BEGIN(warnoption)
#define OX_CLANG_NOWARN_END
#endif
/** /**
* @return an ox::MallocaPtr of the given type pointing to the requested size memory allocation * @return an ox::MallocaPtr of the given type pointing to the requested size memory allocation
*/ */

View File

@ -25,6 +25,8 @@
#include "types.hpp" #include "types.hpp"
#include "typetraits.hpp" #include "typetraits.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
namespace detail { namespace detail {
@ -81,16 +83,16 @@ class FmtArg {
private: private:
static constexpr auto DataSz = 23; static constexpr auto DataSz = 23;
char dataStr[DataSz] = {}; ox::Array<char, DataSz> dataStr{};
template<typename T> template<typename T>
constexpr StringView sv(const T &v, char *dataStr) noexcept { constexpr StringView sv(const T &v, ox::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>) {
ox::CharBuffWriter w(dataStr, DataSz); ox::CharBuffWriter w{dataStr};
std::ignore = ox::writeItoa(v, w); std::ignore = ox::writeItoa(v, w);
return dataStr; return dataStr.data();
} else { } else {
return toStringView(v); return toStringView(v);
} }
@ -225,3 +227,5 @@ constexpr Result<T> join(auto const&d, auto const&list) {
} }
} }
OX_CLANG_NOWARN_END

View File

@ -8,8 +8,11 @@
#include "assert.hpp" #include "assert.hpp"
#include "bit.hpp" #include "bit.hpp"
#include "def.hpp"
#include "heapmgr.hpp" #include "heapmgr.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox::heapmgr { namespace ox::heapmgr {
static struct HeapSegment *volatile g_heapBegin = nullptr; static struct HeapSegment *volatile g_heapBegin = nullptr;
@ -142,3 +145,5 @@ void operator delete[](void *ptr, unsigned long int) noexcept {
} }
#endif #endif
OX_CLANG_NOWARN_END

View File

@ -10,6 +10,7 @@
#include "array.hpp" #include "array.hpp"
#include "concepts.hpp" #include "concepts.hpp"
#include "def.hpp"
#include "cstrops.hpp" #include "cstrops.hpp"
#include "memops.hpp" #include "memops.hpp"
#include "error.hpp" #include "error.hpp"
@ -175,7 +176,9 @@ constexpr Error IString<StrCap>::append(const char *str, std::size_t strLen) noe
strLen = cap() - currentLen; strLen = cap() - currentLen;
err = OxError(1, "Insufficient space for full string"); err = OxError(1, "Insufficient space for full string");
} }
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
ox::strncpy(m_buff.data() + currentLen, str, strLen); ox::strncpy(m_buff.data() + currentLen, str, strLen);
OX_CLANG_NOWARN_END
// 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;
m_size += strLen; m_size += strLen;
@ -263,7 +266,7 @@ constexpr auto itoa(Integer v) noexcept {
}(); }();
ox::IString<Cap> out; ox::IString<Cap> out;
std::ignore = out.resize(out.cap()); std::ignore = out.resize(out.cap());
ox::CharBuffWriter w(out.data(), out.cap()); ox::CharBuffWriter w{{out.data(), out.cap()}};
std::ignore = writeItoa(v, w); std::ignore = writeItoa(v, w);
std::ignore = out.resize(w.tellp()); std::ignore = out.resize(w.tellp());
return out; return out;

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include "def.hpp"
#include "error.hpp" #include "error.hpp"
#include "math.hpp" #include "math.hpp"
@ -39,6 +40,8 @@ struct contiguous_iterator_tag: public random_access_iterator_tag {
#include <iterator> #include <iterator>
#endif #endif
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
template<typename Category, typename T, typename DiffType = std::ptrdiff_t, template<typename Category, typename T, typename DiffType = std::ptrdiff_t,
@ -171,3 +174,5 @@ struct SpanIterator {
}; };
} }
OX_CLANG_NOWARN_END

View File

@ -10,6 +10,8 @@
#include "types.hpp" #include "types.hpp"
#include "memops.hpp" #include "memops.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
#ifndef OX_USE_STDLIB #ifndef OX_USE_STDLIB
#define ox_inhibit_loop_to_libcall __attribute__((__optimize__("-fno-tree-loop-distribute-patterns"))) #define ox_inhibit_loop_to_libcall __attribute__((__optimize__("-fno-tree-loop-distribute-patterns")))
@ -96,3 +98,5 @@ int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept {
} }
} }
OX_CLANG_NOWARN_END

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include "def.hpp"
#include "types.hpp" #include "types.hpp"
#include "typetraits.hpp" #include "typetraits.hpp"
@ -27,6 +28,8 @@ int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept;
} }
#endif #endif
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
template<typename T1, typename T2> template<typename T1, typename T2>
@ -56,3 +59,5 @@ void *memsetElements(T *ptr, T val, std::size_t elements) noexcept {
} }
} }
OX_CLANG_NOWARN_END

View File

@ -8,11 +8,7 @@
#pragma once #pragma once
#include "bit.hpp"
#include "initializerlist.hpp"
#include "iterator.hpp"
#include "memory.hpp" #include "memory.hpp"
#include "new.hpp"
#include "types.hpp" #include "types.hpp"
#include "utility.hpp" #include "utility.hpp"

View File

@ -9,9 +9,12 @@
#pragma once #pragma once
#include "bit.hpp" #include "bit.hpp"
#include "def.hpp"
#include "stddef.hpp" #include "stddef.hpp"
#include "types.hpp" #include "types.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
using RandomSeed = uint64_t[2]; using RandomSeed = uint64_t[2];
@ -51,3 +54,5 @@ constexpr uint64_t Random::gen() noexcept {
} }
} }
OX_CLANG_NOWARN_END

View File

@ -10,9 +10,12 @@
#include "array.hpp" #include "array.hpp"
#include "bit.hpp" #include "bit.hpp"
#include "def.hpp"
#include "iterator.hpp" #include "iterator.hpp"
#include "vector.hpp" #include "vector.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
template<typename T> template<typename T>
@ -231,12 +234,12 @@ class Span {
} }
constexpr T &operator[](std::size_t i) noexcept { constexpr T &operator[](std::size_t i) noexcept {
ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Span overflow"); ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Span access overflow");
return m_items[i]; return m_items[i];
} }
constexpr const T &operator[](std::size_t i) const noexcept { constexpr const T &operator[](std::size_t i) const noexcept {
ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Span overflow"); ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Span access overflow");
return m_items[i]; return m_items[i];
} }
@ -258,3 +261,5 @@ class Span {
}; };
} }
OX_CLANG_NOWARN_END

View File

@ -18,11 +18,14 @@
#endif #endif
#endif #endif
#include "def.hpp"
#include "defines.hpp" #include "defines.hpp"
#include "string.hpp" #include "string.hpp"
#include "trace.hpp" #include "trace.hpp"
#include "vector.hpp" #include "vector.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
#if defined(OX_USE_STDLIB) && __has_include(<unistd.h>) #if defined(OX_USE_STDLIB) && __has_include(<unistd.h>)
@ -100,3 +103,5 @@ void printStackTrace([[maybe_unused]]unsigned shave) noexcept {
} }
} }
OX_CLANG_NOWARN_END

View File

@ -13,6 +13,7 @@
#endif #endif
#include "algorithm.hpp" #include "algorithm.hpp"
#include "def.hpp"
#include "ignore.hpp" #include "ignore.hpp"
#include "memops.hpp" #include "memops.hpp"
#include "serialize.hpp" #include "serialize.hpp"
@ -22,6 +23,8 @@
#include "strops.hpp" #include "strops.hpp"
#include "vector.hpp" #include "vector.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
template<typename Integer> template<typename Integer>
@ -496,12 +499,12 @@ constexpr char &BasicString<SmallStringSize_v>::operator[](std::size_t i) noexce
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::substr(std::size_t pos) const noexcept { constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::substr(std::size_t pos) const noexcept {
return BasicString(m_buff.data() + pos, m_buff.size() - pos - 1); return BasicString(&m_buff[pos], m_buff.size() - pos - 1);
} }
template<std::size_t SmallStringSize_v> template<std::size_t SmallStringSize_v>
constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::substr(std::size_t begin, std::size_t end) const noexcept { constexpr BasicString<SmallStringSize_v> BasicString<SmallStringSize_v>::substr(std::size_t begin, std::size_t end) const noexcept {
const auto src = m_buff.data() + begin; const auto src = &m_buff[begin];
const auto size = end - begin; const auto size = end - begin;
BasicString<SmallStringSize_v> out(size); BasicString<SmallStringSize_v> out(size);
const auto buff = out.data(); const auto buff = out.data();
@ -568,3 +571,5 @@ struct MaybeView<ox::BasicString<sz>> {
}; };
} }
OX_CLANG_NOWARN_END

View File

@ -17,6 +17,8 @@
#include "maybeview.hpp" #include "maybeview.hpp"
#include "writer.hpp" #include "writer.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
template<std::size_t buffLen> template<std::size_t buffLen>
@ -116,3 +118,4 @@ constexpr ox::Result<int> atoi(ox::StringViewCR str) noexcept {
} }
OX_CLANG_NOWARN_END

View File

@ -6,6 +6,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
#include "def.hpp"
#include "strops.hpp" #include "strops.hpp"
static_assert(ox::strcmp("asdf", "hijk") < 0, "asdf < hijk"); static_assert(ox::strcmp("asdf", "hijk") < 0, "asdf < hijk");
@ -17,7 +18,9 @@ static_assert(ox::strcmp("", "") == 0, "\"\" == \"\"");
static_assert([] { static_assert([] {
auto testStr = "asdf"; auto testStr = "asdf";
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
return ox::strchr(testStr, 0, 4) == &testStr[4]; return ox::strchr(testStr, 0, 4) == &testStr[4];
OX_CLANG_NOWARN_END
}(), "ox::strchr 0"); }(), "ox::strchr 0");
static_assert([] { static_assert([] {

View File

@ -9,18 +9,21 @@
#pragma once #pragma once
#include "cstrops.hpp" #include "cstrops.hpp"
#include "def.hpp"
#include "error.hpp" #include "error.hpp"
#include "math.hpp" #include "math.hpp"
#include "stringview.hpp" #include "stringview.hpp"
#include "types.hpp" #include "types.hpp"
#include "vector.hpp" #include "vector.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
[[nodiscard]] [[nodiscard]]
constexpr ox::StringView substr(ox::StringView const&str, std::size_t pos) noexcept { constexpr ox::StringView substr(ox::StringView const&str, std::size_t pos) noexcept {
if (str.len() >= pos) { if (str.len() >= pos) {
return {str.data() + pos, str.len() - pos}; return {&str[pos], str.len() - pos};
} }
return {}; return {};
} }
@ -28,7 +31,7 @@ constexpr ox::StringView substr(ox::StringView const&str, std::size_t pos) noexc
[[nodiscard]] [[nodiscard]]
constexpr ox::StringView substr(ox::StringView const&str, std::size_t start, std::size_t end) noexcept { constexpr ox::StringView substr(ox::StringView const&str, std::size_t start, std::size_t end) noexcept {
if (str.len() >= start && end >= start) { if (str.len() >= start && end >= start) {
return {str.data() + start, end - start}; return {&str[start], end - start};
} }
return {}; return {};
} }
@ -113,3 +116,5 @@ constexpr ox::Result<std::size_t> lastIndexOf(ox::StringViewCR str, int characte
} }
} }
OX_CLANG_NOWARN_END

View File

@ -121,7 +121,9 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
auto a1 = static_cast<char*>(ox::heapmgr::malloc(5)); auto a1 = static_cast<char*>(ox::heapmgr::malloc(5));
auto a2 = static_cast<char*>(ox::heapmgr::malloc(5)); auto a2 = static_cast<char*>(ox::heapmgr::malloc(5));
oxAssert(a1 >= buff.front().unwrap() && a1 < buff.back().unwrap(), "malloc is broken"); oxAssert(a1 >= buff.front().unwrap() && a1 < buff.back().unwrap(), "malloc is broken");
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
oxAssert(a2 >= buff.front().unwrap() && a2 < buff.back().unwrap() && a2 > a1 + 5, "malloc is broken"); oxAssert(a2 >= buff.front().unwrap() && a2 < buff.back().unwrap() && a2 > a1 + 5, "malloc is broken");
OX_CLANG_NOWARN_END
ox::heapmgr::free(a1); ox::heapmgr::free(a1);
ox::heapmgr::free(a2); ox::heapmgr::free(a2);
return OxError(0); return OxError(0);
@ -474,7 +476,9 @@ int main(int argc, const char **args) {
if (argc < 2) { if (argc < 2) {
oxError("Must specify test to run"); oxError("Must specify test to run");
} }
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
auto const testName = args[1]; auto const testName = args[1];
OX_CLANG_NOWARN_END
auto const func = tests.find(testName); auto const func = tests.find(testName);
if (func != tests.end()) { if (func != tests.end()) {
oxAssert(func->second(), "Test returned Error"); oxAssert(func->second(), "Test returned Error");

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include "bit.hpp" #include "bit.hpp"
#include "def.hpp"
#include "ignore.hpp" #include "ignore.hpp"
#include "istring.hpp" #include "istring.hpp"
#include "buffer.hpp" #include "buffer.hpp"
@ -18,6 +19,8 @@
#include "stringview.hpp" #include "stringview.hpp"
#include "strops.hpp" #include "strops.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
using UUIDStr = ox::IString<36>; using UUIDStr = ox::IString<36>;
@ -187,7 +190,7 @@ class UUID {
constexpr UUIDStr toString() const noexcept { constexpr UUIDStr toString() const noexcept {
UUIDStr out; UUIDStr out;
std::ignore = out.resize(UUIDStr::cap()); std::ignore = out.resize(UUIDStr::cap());
ox::CharBuffWriter bw(out.data(), UUIDStr::cap()); ox::CharBuffWriter bw{{out.data(), UUIDStr::cap()}};
std::ignore = toString(bw); std::ignore = toString(bw);
out[UUIDStr::cap()] = 0; out[UUIDStr::cap()] = 0;
return out; return out;
@ -220,3 +223,5 @@ constexpr Error model(T *io, ox::CommonPtrWith<UUID> auto *obj) noexcept {
} }
} }
OX_CLANG_NOWARN_END

View File

@ -14,7 +14,7 @@ namespace ox {
static_assert([] { static_assert([] {
Vec2 v(1, 2); Vec2 v(1, 2);
return v.x == 1 && v.y == 2 && v[0] == 1 && v[1] == 2 && v.size() == 2; return v.x == 1 && v.y == 2 && v.size() == 2;
}()); }());
} }

View File

@ -34,112 +34,6 @@ class Vec2 {
float x = 0; float x = 0;
float y = 0; float y = 0;
template<typename RefType = value_type&, typename PtrType = value_type*, bool reverse = false>
struct iterator: public ox::Iterator<std::bidirectional_iterator_tag, value_type> {
private:
PtrType m_t = nullptr;
size_type m_offset = 0;
size_type m_max = 0;
public:
constexpr iterator() noexcept = default;
constexpr iterator(PtrType t, size_type offset, size_type max) noexcept {
m_t = t;
m_offset = offset;
m_max = max;
}
[[nodiscard]]
constexpr auto offset() const noexcept {
return m_offset;
}
constexpr iterator operator+(size_type s) const noexcept {
if constexpr(reverse) {
return iterator(m_t, ox::max<size_type>(m_offset - s, 0), m_max);
} else {
return iterator(m_t, ox::min<size_type>(m_offset + s, m_max), m_max);
}
}
constexpr typename ox::Iterator<std::bidirectional_iterator_tag, value_type>::difference_type
operator-(const iterator &other) const noexcept {
if constexpr(reverse) {
return m_offset + other.m_offset;
} else {
return m_offset - other.m_offset;
}
}
constexpr iterator operator-(size_type s) const noexcept {
if constexpr(reverse) {
return iterator(m_t, ox::min<size_type>(m_offset + s, m_max), m_max);
} else {
return iterator(m_t, ox::max<size_type>(m_offset - s, 0), m_max);
}
}
constexpr iterator &operator+=(size_type s) noexcept {
if constexpr(reverse) {
m_offset = ox::max<size_type>(m_offset - s, 0);
} else {
m_offset = ox::min(m_offset + s, m_max);
}
return *this;
}
constexpr iterator &operator-=(size_type s) noexcept {
if constexpr(reverse) {
m_offset = ox::min(m_offset + s, m_max);
} else {
m_offset = ox::max<size_type>(m_offset - s, 0);
}
return *this;
}
constexpr iterator &operator++() noexcept {
return operator+=(1);
}
constexpr iterator &operator--() noexcept {
return operator-=(1);
}
constexpr RefType operator*() const noexcept {
return m_t[m_offset];
}
constexpr RefType operator[](size_type s) const noexcept {
return m_t[s];
}
constexpr bool operator<(const iterator &other) const noexcept {
return m_offset < other.m_offset;
}
constexpr bool operator>(const iterator &other) const noexcept {
return m_offset > other.m_offset;
}
constexpr bool operator<=(const iterator &other) const noexcept {
return m_offset <= other.m_offset;
}
constexpr bool operator>=(const iterator &other) const noexcept {
return m_offset >= other.m_offset;
}
constexpr bool operator==(const iterator &other) const noexcept {
return m_t == other.m_t && m_offset == other.m_offset && m_max == other.m_max;
}
constexpr bool operator!=(const iterator &other) const noexcept {
return m_t != other.m_t || m_offset != other.m_offset || m_max != other.m_max;
}
};
constexpr Vec2() noexcept = default; constexpr Vec2() noexcept = default;
explicit constexpr Vec2(class Point const&pt) noexcept; explicit constexpr Vec2(class Point const&pt) noexcept;
@ -159,85 +53,8 @@ class Vec2 {
} }
#endif #endif
[[nodiscard]]
constexpr iterator<> begin() noexcept {
return {start(), 0, size()};
}
[[nodiscard]]
constexpr iterator<> end() noexcept {
return {start(), size(), size()};
}
[[nodiscard]]
constexpr iterator<const value_type&, const value_type*> begin() const noexcept {
return {start(), 0, size()};
}
[[nodiscard]]
constexpr iterator<const value_type&, const value_type*> end() const noexcept {
return {start(), size(), size()};
}
[[nodiscard]]
constexpr iterator<value_type&, value_type*, true> rbegin() noexcept {
return {start(), size() - 1, size()};
}
[[nodiscard]]
constexpr iterator<value_type&, value_type*, true> rend() noexcept {
return {start(), ox::MaxValue<size_type>, size()};
}
[[nodiscard]]
constexpr iterator<const value_type&, const value_type*, true> rbegin() const noexcept {
return {start(), size() - 1, size()};
}
[[nodiscard]]
constexpr iterator<const value_type&, const value_type*, true> rend() const noexcept {
return {start(), ox::MaxValue<size_type>, size()};
}
constexpr auto &operator[](std::size_t i) noexcept {
if (std::is_constant_evaluated()) {
switch (i) {
case 0:
return x;
case 1:
return y;
default:
oxAssert(false, "Read past end of Vec2");
return y;
}
} else {
return start()[i];
}
}
constexpr const auto &operator[](std::size_t i) const noexcept {
if (std::is_constant_evaluated()) {
switch (i) {
case 0:
return x;
case 1:
return y;
default:
oxAssert(false, "Read past end of Vec2");
return y;
}
} else {
return start()[i];
}
}
constexpr auto operator==(const Vec2 &v) const noexcept { constexpr auto operator==(const Vec2 &v) const noexcept {
for (auto i = 0u; i < v.size(); ++i) { return x == v.x && y == v.y;
if ((*this)[i] != v[i]) {
return false;
}
}
return true;
} }
constexpr auto operator!=(const Vec2 &v) const noexcept { constexpr auto operator!=(const Vec2 &v) const noexcept {
@ -292,17 +109,6 @@ class Vec2 {
y /= i; y /= i;
return *this; return *this;
} }
protected:
[[nodiscard]]
constexpr float *start() noexcept {
return&x;
}
[[nodiscard]]
constexpr const float *start() const noexcept {
return &x;
}
}; };
template<typename T> template<typename T>

View File

@ -20,6 +20,8 @@
#include "types.hpp" #include "types.hpp"
#include "utility.hpp" #include "utility.hpp"
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
namespace ox { namespace ox {
namespace detail { namespace detail {
@ -709,3 +711,5 @@ constexpr auto alignOf(const Vector<T>&) noexcept {
} }
} }
OX_CLANG_NOWARN_END