based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 32754b4b4d6c0686ad66e1ff868f0d32ef2f33ee
parent febff86821e4e3d9e26be13f75ccaa0be051ba3e
author Dimitrije Dobrota < mail@dimitrijedobrota.com >
date Wed, 4 Jun 2025 11:18:30 +0200

Consistency Improvements

Diffstat:
M include/based/char/character.hpp | + -
M include/based/char/is/alnum.hpp | ++ -
M include/based/char/is/alpha.hpp | ++ -
M include/based/char/is/alpha_lower.hpp | +++ -
M include/based/char/is/alpha_upper.hpp | +++ -
M include/based/char/is/digit.hpp | +++ -
M include/based/char/is/xdigit.hpp | +++ -
M include/based/char/mapper.hpp | ++++++++++++++++++++++ -----------------------------
M include/based/container/vector.hpp | ++++++ ------
M include/based/types/literals.hpp | ++ --
M test/source/char/mapper_test.cpp | +++ -

11 files changed, 50 insertions(+), 45 deletions(-)


diff --git a/ include/based/char/character.hpp b/ include/based/char/character.hpp

@@ -21,7 +21,7 @@ public:

using strong_type::strong_type;
using strong_type::operator=;

explicit constexpr character(char chr)
constexpr character(char chr) // NOLINT(*explicit*)
: strong_type(cast(chr))
{
}

diff --git a/ include/based/char/is/alnum.hpp b/ include/based/char/is/alnum.hpp

@@ -1,12 +1,13 @@

#pragma once

#include "based/char/character.hpp"
#include "based/char/is/alpha.hpp"
#include "based/char/is/digit.hpp"

namespace based
{

constexpr bool is_alnum(char chr)
constexpr bool is_alnum(character chr)
{
return is_alpha(chr) || is_digit(chr);
}

diff --git a/ include/based/char/is/alpha.hpp b/ include/based/char/is/alpha.hpp

@@ -1,12 +1,13 @@

#pragma once

#include "based/char/character.hpp"
#include "based/char/is/alpha_lower.hpp"
#include "based/char/is/alpha_upper.hpp"

namespace based
{

constexpr bool is_alpha(char chr)
constexpr bool is_alpha(character chr)
{
return is_alpha_lower(chr) || is_alpha_upper(chr);
}

diff --git a/ include/based/char/is/alpha_lower.hpp b/ include/based/char/is/alpha_lower.hpp

@@ -1,9 +1,11 @@

#pragma once

#include "based/char/character.hpp"

namespace based
{

constexpr bool is_alpha_lower(char chr)
constexpr bool is_alpha_lower(character chr)
{
return chr >= 'a' && chr <= 'z';
}

diff --git a/ include/based/char/is/alpha_upper.hpp b/ include/based/char/is/alpha_upper.hpp

@@ -1,9 +1,11 @@

#pragma once

#include "based/char/character.hpp"

namespace based
{

constexpr bool is_alpha_upper(char chr)
constexpr bool is_alpha_upper(character chr)
{
return chr >= 'A' && chr <= 'Z';
}

diff --git a/ include/based/char/is/digit.hpp b/ include/based/char/is/digit.hpp

@@ -1,9 +1,11 @@

#pragma once

#include "based/char/character.hpp"

namespace based
{

constexpr bool is_digit(char chr)
constexpr bool is_digit(character chr)
{
return chr >= '0' && chr <= '9';
}

diff --git a/ include/based/char/is/xdigit.hpp b/ include/based/char/is/xdigit.hpp

@@ -1,9 +1,11 @@

#pragma once

#include "based/char/character.hpp"

namespace based
{

constexpr bool is_xdigit(char chr)
constexpr bool is_xdigit(character chr)
{
return (chr >= 'a' && chr <= 'f') || (chr >= 'A' && chr <= 'F')
|| (chr >= '0' && chr <= '9');

diff --git a/ include/based/char/mapper.hpp b/ include/based/char/mapper.hpp

@@ -1,29 +1,30 @@

#pragma once

#include <array>

#include "based/char/character.hpp"
#include "based/concepts/procedure/predicate.hpp"
#include "based/types/types.hpp"
#include "based/container/array.hpp"
#include "based/types/limits.hpp"
#include "based/types/literals.hpp"

namespace based
{

template<Predicate<char> Predicate>
template<Predicate<character> Predicate>
class mapper
{
static constexpr based::size_t size = 128;
using mapped_type = bu8;
static constexpr auto size = limits<u8>::max;
using mapped_type = u8;

static constexpr Predicate m_predicate = {};

using direct_t = std::array<mapped_type, size>;
using direct_t = array<mapped_type, u8, size>;
static constexpr direct_t direct = []
{
direct_t res = {};

mapped_type count = 0;
for (std::size_t idx = 0; idx < size; idx++) {
if (m_predicate(static_cast<char>(idx))) {
mapped_type count = 0_u8;
for (auto idx = 0_u8; idx < size; idx++) {
if (m_predicate(character::basic_cast(idx))) {
res[idx] = count++;
}
}

@@ -31,25 +32,25 @@ class mapper

return res;
}();

static constexpr const std::size_t count = []
static constexpr const u8 count = []
{
mapped_type count = 0;
for (std::size_t idx = 0; idx < size; idx++) {
if (m_predicate(static_cast<char>(idx))) {
mapped_type count = 0_u8;
for (auto idx = 0_u8; idx < size; idx++) {
if (m_predicate(character::basic_cast(idx))) {
count++;
}
}
return count;
}();

using reverse_t = std::array<char, count>;
using reverse_t = array<character, u8, count>;
static constexpr reverse_t reverse = []
{
reverse_t res = {};

mapped_type count = 0;
for (std::size_t idx = 0; idx < size; idx++) {
const auto chr = static_cast<char>(idx);
mapped_type count = 0_u8;
for (auto idx = 0_u8; idx < size; idx++) {
const auto chr = character::basic_cast(idx);
if (m_predicate(chr)) {
res[count++] = chr;
}

@@ -59,17 +60,9 @@ class mapper

}();

public:
static constexpr bool predicate(char chr) { return m_predicate(chr); }
static constexpr char map(mapped_type value) { return reverse[value]; }
static constexpr mapped_type map(char chr)
{
return direct[static_cast<based::size_t>(chr)];
}
};

struct test
{
constexpr bool operator()(char chr) const { return chr >= 'a' && chr <= 'z'; }
static constexpr bool predicate(character chr) { return m_predicate(chr); }
static constexpr character map(mapped_type value) { return reverse[value]; }
static constexpr mapped_type map(character chr) { return direct[chr.ord()]; }
};

} // namespace based

diff --git a/ include/based/container/vector.hpp b/ include/based/container/vector.hpp

@@ -6,18 +6,18 @@ namespace based

{

template<class T, class U, class Allocator = std::allocator<T>>
class vector : public vector<T, Allocator>
class vector : public std::vector<T, Allocator>
{
using base = vector<T, Allocator>;
using base = std::vector<T, Allocator>;

static constexpr auto cast(U value)
static constexpr auto cast(U pos)
{
return static_cast<base::size_type>(value);
return static_cast<base::size_type>(pos.value);
}

static constexpr auto cast(base::size_type value)
static constexpr auto cast(base::size_type pos)
{
return static_cast<U>(value);
return static_cast<U>(pos);
}

public:

diff --git a/ include/based/types/literals.hpp b/ include/based/types/literals.hpp

@@ -34,7 +34,7 @@ constexpr auto make_signed_itnernal()

{
const signed long long radix = 10;

static_assert(is_digit(c), "invalid digit");
static_assert(is_digit(character(c)), "invalid digit");
static_assert(v <= (limits<i64>::max.value - (c - '0')) / radix, "overflow");

return make_signed_itnernal<(radix * v) + (c - '0'), cs...>();

@@ -111,7 +111,7 @@ constexpr auto make_unsigned_internal()

{
const unsigned long long radix = 10;

static_assert(is_digit(c), "invalid digit");
static_assert(is_digit(character(c)), "invalid digit");
static_assert(v <= (limits<u64>::max.value - (c - '0')) / radix, "overflow");

return make_unsigned_internal<(radix * v) + c - '0', cs...>();

diff --git a/ test/source/char/mapper_test.cpp b/ test/source/char/mapper_test.cpp

@@ -4,9 +4,11 @@


#include <catch2/catch_test_macros.hpp>

#include "based/char/character.hpp"

struct test
{
constexpr bool operator()(char chr) const { return chr >= '\0'; }
constexpr bool operator()(based::character chr) const { return chr >= '\0'; }
};

template class based::mapper<test>;