Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve naming convention to be more streamlined. #72

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/libunicode/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ template <typename T>
class out
{
public:
constexpr out(std::reference_wrapper<T> _ref) noexcept: ref_ { &_ref.value() } {}
constexpr explicit out(T& _ref) noexcept: ref_ { &_ref } {}
constexpr out(std::reference_wrapper<T> ref) noexcept: _ref { &ref.value() } {}
constexpr explicit out(T& ref) noexcept: _ref { &ref } {}
constexpr out(out const&) noexcept = default;
constexpr out(out&&) noexcept = default;
constexpr out& operator=(out const&) noexcept = default;
constexpr out& operator=(out&&) noexcept = default;

constexpr T& get() noexcept { return *ref_; }
constexpr T const& get() const noexcept { return *ref_; }
constexpr T& get() noexcept { return *_ref; }
constexpr T const& get() const noexcept { return *_ref; }

constexpr T& operator*() noexcept { return *ref_; }
constexpr T const& operator*() const noexcept { return *ref_; }
constexpr T& operator*() noexcept { return *_ref; }
constexpr T const& operator*() const noexcept { return *_ref; }

constexpr T* operator->() noexcept { return ref_; }
constexpr T const* operator->() const noexcept { return ref_; }
constexpr T* operator->() noexcept { return _ref; }
constexpr T const* operator->() const noexcept { return _ref; }

constexpr void assign(T _value) { *ref_ = std::move(_value); }
constexpr void assign(T value) { *_ref = std::move(value); }

private:
T* ref_;
T* _ref;
};

// dynamic array with a fixed capacity.
Expand All @@ -82,11 +82,11 @@ class fs_array
constexpr size_t size() const noexcept { return size_; }
constexpr bool empty() const noexcept { return size_ == 0; }

constexpr bool push_back(T _value) noexcept
constexpr bool push_back(T value) noexcept
{
if (size_ == N)
return false;
values_[size_++] = std::move(_value);
values_[size_++] = std::move(value);
return true;
}

Expand Down
30 changes: 15 additions & 15 deletions src/libunicode/ucd_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@
namespace unicode
{

struct Interval
struct Interval // NOLINT(readability-identifier-naming)
{
char32_t from;
char32_t to;
};

template <size_t N>
constexpr bool contains(std::array<Interval, N> const& _ranges, char32_t _codepoint) noexcept
constexpr bool contains(std::array<Interval, N> const& ranges, char32_t codepoint) noexcept
{
auto a = size_t { 0 };
auto b = static_cast<size_t>(_ranges.size()) - 1;
auto b = static_cast<size_t>(ranges.size()) - 1;
while (a < b)
{
auto const i = ((b + a) / 2);
auto const& I = _ranges[i];
if (I.to < _codepoint)
auto const& I = ranges[i];
if (I.to < codepoint)
a = i + 1;
else if (I.from > _codepoint)
else if (I.from > codepoint)
{
if (i == 0)
return false;
Expand All @@ -45,36 +45,36 @@ constexpr bool contains(std::array<Interval, N> const& _ranges, char32_t _codepo
else
return true;
}
return a == b && _ranges[a].from <= _codepoint && _codepoint <= _ranges[a].to;
return a == b && ranges[a].from <= codepoint && codepoint <= ranges[a].to;
}

template <typename T>
struct Prop
struct Prop // NOLINT(readability-identifier-naming)
{
Interval interval;
T property;
};

template <typename T, size_t N>
constexpr std::optional<T> search(std::array<Prop<T>, N> const& _ranges, char32_t _codepoint)
constexpr std::optional<T> search(std::array<Prop<T>, N> const& ranges, char32_t codepoint)
{
auto a = size_t { 0 };
auto b = static_cast<size_t>(_ranges.size()) - 1;
auto b = static_cast<size_t>(ranges.size()) - 1;

while (a < b)
{
auto const i = static_cast<size_t>((b + a) / 2);
auto const& I = _ranges[i];
if (I.interval.to < _codepoint)
auto const& I = ranges[i];
if (I.interval.to < codepoint)
a = i + 1;
else if (I.interval.from > _codepoint)
else if (I.interval.from > codepoint)
b = i - 1;
else
return I.property;
}

if (a == b && _ranges[a].interval.from <= _codepoint && _codepoint <= _ranges[a].interval.to)
return _ranges[a].property;
if (a == b && ranges[a].interval.from <= codepoint && codepoint <= ranges[a].interval.to)
return ranges[a].property;

return std::nullopt;
}
Expand Down
91 changes: 44 additions & 47 deletions src/libunicode/utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,68 +24,68 @@ namespace unicode

/// Converts an UTF-32 codepoint into a UTF-8 sequence.
///
/// @param _character UTF-32 character to encode to UTF-8
/// @param _result target memory location to start writing to (up to 4 chars)
/// @param character UTF-32 character to encode to UTF-8
/// @param result target memory location to start writing to (up to 4 chars)
///
/// @return number of bytes written to.
constexpr inline unsigned to_utf8(char32_t _character, uint8_t* _result)
constexpr inline unsigned to_utf8(char32_t character, uint8_t* result)
{
if (_character <= 0x7F)
if (character <= 0x7F)
{
_result[0] = static_cast<uint8_t>(_character & 0b0111'1111);
result[0] = static_cast<uint8_t>(character & 0b0111'1111);
return 1;
}
else if (_character <= 0x07FF)
else if (character <= 0x07FF)
{
_result[0] = static_cast<uint8_t>(((_character >> 6) & 0b0001'1111) | 0b1100'0000);
_result[1] = static_cast<uint8_t>(((_character >> 0) & 0b0011'1111) | 0b1000'0000);
result[0] = static_cast<uint8_t>(((character >> 6) & 0b0001'1111) | 0b1100'0000);
result[1] = static_cast<uint8_t>(((character >> 0) & 0b0011'1111) | 0b1000'0000);
return 2;
}
if (_character <= 0xFFFF)
if (character <= 0xFFFF)
{
_result[0] = static_cast<uint8_t>(((_character >> 12) & 0b0000'1111) | 0b1110'0000);
_result[1] = static_cast<uint8_t>(((_character >> 6) & 0b0011'1111) | 0b1000'0000);
_result[2] = static_cast<uint8_t>(((_character >> 0) & 0b0011'1111) | 0b1000'0000);
result[0] = static_cast<uint8_t>(((character >> 12) & 0b0000'1111) | 0b1110'0000);
result[1] = static_cast<uint8_t>(((character >> 6) & 0b0011'1111) | 0b1000'0000);
result[2] = static_cast<uint8_t>(((character >> 0) & 0b0011'1111) | 0b1000'0000);
return 3;
}
else
{
_result[0] = static_cast<uint8_t>(((_character >> 18) & 0b0000'0111) | 0b1111'0000);
_result[1] = static_cast<uint8_t>(((_character >> 12) & 0b0011'1111) | 0b1000'0000);
_result[2] = static_cast<uint8_t>(((_character >> 6) & 0b0011'1111) | 0b1000'0000);
_result[3] = static_cast<uint8_t>(((_character >> 0) & 0b0011'1111) | 0b1000'0000);
result[0] = static_cast<uint8_t>(((character >> 18) & 0b0000'0111) | 0b1111'0000);
result[1] = static_cast<uint8_t>(((character >> 12) & 0b0011'1111) | 0b1000'0000);
result[2] = static_cast<uint8_t>(((character >> 6) & 0b0011'1111) | 0b1000'0000);
result[3] = static_cast<uint8_t>(((character >> 0) & 0b0011'1111) | 0b1000'0000);
return 4;
}
}

/// Converts a UTF-32 string into an UTF-8 sring.
inline std::string to_utf8(char32_t const* _characters, size_t n)
inline std::string to_utf8(char32_t const* characters, size_t n)
{
std::string s;
s.reserve(n);
for (size_t i = 0; i < n; ++i)
{
uint8_t bytes[4];
unsigned const len = to_utf8(_characters[i], bytes);
unsigned const len = to_utf8(characters[i], bytes);
s.append((char const*) bytes, len);
}

return s;
}

inline std::string to_utf8(char32_t _character)
inline std::string to_utf8(char32_t character)
{
return to_utf8(&_character, 1);
return to_utf8(&character, 1);
}

inline std::string to_utf8(std::u32string const& _characters)
inline std::string to_utf8(std::u32string const& characters)
{
return to_utf8(_characters.data(), _characters.size());
return to_utf8(characters.data(), characters.size());
}

inline std::string to_utf8(std::u32string_view const& _characters)
inline std::string to_utf8(std::u32string_view const& characters)
{
return to_utf8(_characters.data(), _characters.size());
return to_utf8(characters.data(), characters.size());
}

struct utf8_decoder_state
Expand All @@ -95,25 +95,22 @@ struct utf8_decoder_state
unsigned currentLength = 0;
};

struct Invalid
{
};
struct Incomplete
{
};
struct Success
{
char32_t value;
};
// clang-format off
// NOLINTBEGIN(readability-identifier-naming)
struct Invalid { };
struct Incomplete { };
struct Success { char32_t value; };
// NOLINTEND(readability-identifier-naming)
// clang-format on

using ConvertResult = std::variant<Invalid, Incomplete, Success>;

/// Progressively decodes a UTF-8 codepoint.
ConvertResult from_utf8(utf8_decoder_state& state, uint8_t value) noexcept;

inline unsigned from_utf8i(utf8_decoder_state& _state, uint8_t _byte)
inline unsigned from_utf8i(utf8_decoder_state& state, uint8_t value)
{
auto const result = from_utf8(_state, _byte);
auto const result = from_utf8(state, value);

if (std::holds_alternative<Incomplete>(result))
return static_cast<unsigned>(-1);
Expand All @@ -124,43 +121,43 @@ inline unsigned from_utf8i(utf8_decoder_state& _state, uint8_t _byte)
return std::get<Success>(result).value;
}

inline ConvertResult from_utf8(uint8_t const* _bytes, size_t* _size)
inline ConvertResult from_utf8(uint8_t const* bytes, size_t* size)
{
auto state = utf8_decoder_state {};
auto result = ConvertResult {};

do
result = from_utf8(state, *_bytes++);
result = from_utf8(state, *bytes++);
while (std::holds_alternative<Incomplete>(result));

if (_size)
*_size = state.currentLength;
if (size)
*size = state.currentLength;

return result;
}

#if 0 // TODO(do that later) __cplusplus > 201703L // C++20 (char8_t)
inline ConvertResult from_utf8(char8_t const* _bytes, size_t* _size)
inline ConvertResult from_utf8(char8_t const* bytes, size_t* size)
{
return from_utf8((uint8_t const*)(_bytes), _size);
return from_utf8((uint8_t const*)(bytes), size);
}
#endif

inline ConvertResult from_utf8(char const* _bytes, size_t* _size)
inline ConvertResult from_utf8(char const* bytes, size_t* size)
{
return from_utf8((uint8_t const*) (_bytes), _size);
return from_utf8((uint8_t const*) (bytes), size);
}

template <typename T = char32_t>
inline std::basic_string<T> from_utf8(std::string_view _bytes)
inline std::basic_string<T> from_utf8(std::string_view bytes)
{
static_assert(sizeof(T) == 4);
std::basic_string<T> s;
size_t offset = 0;
while (offset < _bytes.size())
while (offset < bytes.size())
{
size_t i {};
ConvertResult const result = from_utf8(_bytes.data() + offset, &i);
ConvertResult const result = from_utf8(bytes.data() + offset, &i);
if (std::holds_alternative<Success>(result))
s += T(std::get<Success>(result).value);
offset += i;
Expand Down
Loading