[ox/std] Add all_of and any_of range functions, cleanup

This commit is contained in:
Gary Talent 2023-03-11 16:46:17 -06:00
parent 488f73f60f
commit b58431c09a
9 changed files with 87 additions and 30 deletions

View File

@ -97,6 +97,7 @@ install(
new.hpp
optional.hpp
random.hpp
ranges.hpp
serialize.hpp
std.hpp
stddef.hpp

View File

@ -27,13 +27,8 @@ void *ox_inhibit_loop_to_libcall memset(void *ptr, int val, std::size_t size) {
return ox_memset(ptr, val, size);
}
}
#undef ox_inhibit_loop_to_libcall
#endif
int ox_memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept {
int ox_inhibit_loop_to_libcall memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept {
int retval = 0;
auto block1 = reinterpret_cast<const uint8_t*>(ptr1);
auto block2 = reinterpret_cast<const uint8_t*>(ptr2);
@ -48,3 +43,13 @@ int ox_memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept {
}
return retval;
}
}
#undef ox_inhibit_loop_to_libcall
#endif
int ox_memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept {
return memcmp(ptr1, ptr2, size);
}

View File

@ -23,6 +23,8 @@ void *memmove(void *dest, const void *src, std::size_t size);
void *memset(void *ptr, int val, std::size_t size);
int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept;
}
#endif
@ -68,10 +70,22 @@ constexpr void *ox_memset(void *ptr, int val, std::size_t size) noexcept {
namespace ox {
constexpr void *memmove(void *dest, const void *src, std::size_t size) {
return ox_memmove(dest, src, size);
}
constexpr void *memset(void *ptr, int val, std::size_t size) noexcept {
return ox_memset(ptr, val, size);
}
constexpr void *memcpy(void *dest, const void *src, std::size_t size) noexcept {
return ox_memcpy(dest, src, size);
}
inline int memcmp(const void *ptr1, const void *ptr2, std::size_t size) noexcept {
return ox_memcmp(ptr1, ptr2, size);
}
template<typename T>
void *memsetElements(T *ptr, T val, std::size_t elements) noexcept {
return memset(ptr, val, elements * sizeof(T));

View File

@ -8,6 +8,7 @@
#pragma once
#include "bit.hpp"
#include "stddef.hpp"
#include "types.hpp"

35
deps/ox/src/ox/std/ranges.hpp vendored Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright 2015 - 2023 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
namespace ox {
[[nodiscard]]
constexpr bool all_of(auto begin, auto end, auto pred) noexcept {
while (begin != end) {
if (!pred(*begin)) {
return false;
}
++begin;
}
return true;
}
[[nodiscard]]
constexpr bool any_of(auto begin, auto end, auto pred) noexcept {
while (begin != end) {
if (pred(*begin)) {
return true;
}
++begin;
}
return false;
}
}

View File

@ -352,3 +352,19 @@ constexpr auto toStdStringView(CRStringView sv) noexcept {
#endif
}
constexpr ox::Result<int> ox_atoi(ox::CRStringView str) noexcept {
int total = 0;
int multiplier = 1;
for (auto i = static_cast<int64_t>(str.len()) - 1; i != -1; --i) {
auto s = static_cast<uint64_t>(i);
if (str[s] >= '0' && str[s] <= '9') {
total += (str[s] - '0') * multiplier;
multiplier *= 10;
} else {
return OxError(1);
}
}
return total;
}

View File

@ -125,20 +125,6 @@ constexpr int ox_lastIndexOf(const auto &str, int character, std::size_t maxLen
return retval;
}
constexpr ox::Result<int> ox_atoi(const char *str) noexcept {
int total = 0;
int multiplier = 1;
for (auto i = static_cast<int64_t>(ox_strlen(str)) - 1; i != -1; i--) {
if (str[i] >= '0' && str[i] <= '9') {
total += (str[i] - '0') * multiplier;
multiplier *= 10;
} else {
return OxError(1);
}
}
return total;
}
template<typename Integer, typename T>
constexpr T ox_itoa(Integer v, T str) noexcept {
if (v) {

View File

@ -35,6 +35,6 @@ Result<UUID> UUID::generate() noexcept {
}
static_assert(UUID{}.isNull());
static_assert(!UUID::fromString("34AF4809-043D-4348-B720-2B454E5678C7").value.isNull());
static_assert(!UUID::fromString("34af4809-043d-4348-b720-2b454e5678c7").value.isNull());
}

View File

@ -11,6 +11,7 @@
#include "array.hpp"
#include "bstring.hpp"
#include "random.hpp"
#include "ranges.hpp"
#include "stringview.hpp"
#include "strops.hpp"
#include "trace.hpp"
@ -56,8 +57,8 @@ constexpr ox::Result<uint8_t> fromHex(ox::CRStringView v) noexcept {
return OxError(2);
}
uint8_t out = 0;
out += valMap[static_cast<unsigned>(v[0])] * 16u;
out += valMap[static_cast<unsigned>(v[1])];
out += valMap[static_cast<unsigned char>(v[0])] * 16u;
out += valMap[static_cast<unsigned char>(v[1])];
return out;
}
@ -95,7 +96,7 @@ class UUID {
protected:
static bool s_seeded;
static Random s_rand;
ox::Array<uint8_t, 16> m_value;
ox::Array<uint8_t, 16> m_value{};
public:
static void seedGenerator(const RandomSeed &seed) noexcept;
@ -110,12 +111,10 @@ class UUID {
[[nodiscard]]
constexpr auto isNull() const noexcept {
if (std::is_constant_evaluated()) {
for (auto v : m_value) {
if (v) {
return false;
}
if (ox::all_of(m_value.begin(), m_value.end(), [](auto v) { return v == 0; })) {
return true;
}
return true;
return false;
} else {
constexpr uint64_t zero = 0;
return ox::memcmp(&zero, m_value.data() + 0, 8) == 0
@ -125,7 +124,7 @@ class UUID {
static constexpr ox::Result<ox::UUID> fromString(ox::CRStringView s) noexcept {
if (s.len() < 36) {
return OxError(1, "Insufficient data contain complete UUID");
return OxError(1, "Insufficient data to contain a complete UUID");
}
UUID out;
auto valueI = 0u;