From f60fc93d35c9238b2c5ef31d25615ce335ca7940 Mon Sep 17 00:00:00 2001 From: Christian Parpart Date: Fri, 14 Apr 2023 23:17:49 +0200 Subject: [PATCH] Improve naming convention to be more streamlined. Signed-off-by: Christian Parpart --- src/libunicode/support.h | 24 ++++----- src/libunicode/ucd_private.h | 30 +++++------ src/libunicode/utf8.h | 91 ++++++++++++++++----------------- src/libunicode/word_segmenter.h | 52 +++++++++---------- 4 files changed, 97 insertions(+), 100 deletions(-) diff --git a/src/libunicode/support.h b/src/libunicode/support.h index 1c68f82..aef6926 100644 --- a/src/libunicode/support.h +++ b/src/libunicode/support.h @@ -37,26 +37,26 @@ template class out { public: - constexpr out(std::reference_wrapper _ref) noexcept: ref_ { &_ref.value() } {} - constexpr explicit out(T& _ref) noexcept: ref_ { &_ref } {} + constexpr out(std::reference_wrapper 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. @@ -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; } diff --git a/src/libunicode/ucd_private.h b/src/libunicode/ucd_private.h index 90efe40..de23e71 100644 --- a/src/libunicode/ucd_private.h +++ b/src/libunicode/ucd_private.h @@ -19,24 +19,24 @@ namespace unicode { -struct Interval +struct Interval // NOLINT(readability-identifier-naming) { char32_t from; char32_t to; }; template -constexpr bool contains(std::array const& _ranges, char32_t _codepoint) noexcept +constexpr bool contains(std::array const& ranges, char32_t codepoint) noexcept { auto a = size_t { 0 }; - auto b = static_cast(_ranges.size()) - 1; + auto b = static_cast(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; @@ -45,36 +45,36 @@ constexpr bool contains(std::array 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 -struct Prop +struct Prop // NOLINT(readability-identifier-naming) { Interval interval; T property; }; template -constexpr std::optional search(std::array, N> const& _ranges, char32_t _codepoint) +constexpr std::optional search(std::array, N> const& ranges, char32_t codepoint) { auto a = size_t { 0 }; - auto b = static_cast(_ranges.size()) - 1; + auto b = static_cast(ranges.size()) - 1; while (a < b) { auto const i = static_cast((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; } diff --git a/src/libunicode/utf8.h b/src/libunicode/utf8.h index a49aa5b..46c02a5 100644 --- a/src/libunicode/utf8.h +++ b/src/libunicode/utf8.h @@ -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(_character & 0b0111'1111); + result[0] = static_cast(character & 0b0111'1111); return 1; } - else if (_character <= 0x07FF) + else if (character <= 0x07FF) { - _result[0] = static_cast(((_character >> 6) & 0b0001'1111) | 0b1100'0000); - _result[1] = static_cast(((_character >> 0) & 0b0011'1111) | 0b1000'0000); + result[0] = static_cast(((character >> 6) & 0b0001'1111) | 0b1100'0000); + result[1] = static_cast(((character >> 0) & 0b0011'1111) | 0b1000'0000); return 2; } - if (_character <= 0xFFFF) + if (character <= 0xFFFF) { - _result[0] = static_cast(((_character >> 12) & 0b0000'1111) | 0b1110'0000); - _result[1] = static_cast(((_character >> 6) & 0b0011'1111) | 0b1000'0000); - _result[2] = static_cast(((_character >> 0) & 0b0011'1111) | 0b1000'0000); + result[0] = static_cast(((character >> 12) & 0b0000'1111) | 0b1110'0000); + result[1] = static_cast(((character >> 6) & 0b0011'1111) | 0b1000'0000); + result[2] = static_cast(((character >> 0) & 0b0011'1111) | 0b1000'0000); return 3; } else { - _result[0] = static_cast(((_character >> 18) & 0b0000'0111) | 0b1111'0000); - _result[1] = static_cast(((_character >> 12) & 0b0011'1111) | 0b1000'0000); - _result[2] = static_cast(((_character >> 6) & 0b0011'1111) | 0b1000'0000); - _result[3] = static_cast(((_character >> 0) & 0b0011'1111) | 0b1000'0000); + result[0] = static_cast(((character >> 18) & 0b0000'0111) | 0b1111'0000); + result[1] = static_cast(((character >> 12) & 0b0011'1111) | 0b1000'0000); + result[2] = static_cast(((character >> 6) & 0b0011'1111) | 0b1000'0000); + result[3] = static_cast(((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 @@ -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; /// 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(result)) return static_cast(-1); @@ -124,43 +121,43 @@ inline unsigned from_utf8i(utf8_decoder_state& _state, uint8_t _byte) return std::get(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(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 -inline std::basic_string from_utf8(std::string_view _bytes) +inline std::basic_string from_utf8(std::string_view bytes) { static_assert(sizeof(T) == 4); std::basic_string 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(result)) s += T(std::get(result).value); offset += i; diff --git a/src/libunicode/word_segmenter.h b/src/libunicode/word_segmenter.h index b2fc7ba..eb24049 100644 --- a/src/libunicode/word_segmenter.h +++ b/src/libunicode/word_segmenter.h @@ -25,64 +25,64 @@ class word_segmenter using iterator = char_type const*; using view_type = std::basic_string_view; - constexpr word_segmenter(std::basic_string_view const& _str): - word_segmenter(_str.data(), _str.data() + _str.size()) + constexpr word_segmenter(std::basic_string_view const& str): + word_segmenter(str.data(), str.data() + str.size()) { } constexpr word_segmenter(): word_segmenter({}, {}) {} constexpr bool empty() const noexcept { return size() == 0; } - constexpr std::size_t size() const noexcept { return static_cast(right_ - left_); } - constexpr view_type operator*() const noexcept { return view_type(left_, size()); } + constexpr std::size_t size() const noexcept { return static_cast(_right - _left); } + constexpr view_type operator*() const noexcept { return view_type(_left, size()); } constexpr word_segmenter& operator++() noexcept { - left_ = right_; - while (right_ != end_) + _left = _right; + while (_right != _end) { - switch (state_) + switch (_state) { case State::NoWord: - if (!isDelimiter(*right_)) + if (!isDelimiter(*_right)) { - state_ = State::Word; + _state = State::Word; return *this; } break; case State::Word: - if (isDelimiter(*right_)) + if (isDelimiter(*_right)) { - state_ = State::NoWord; + _state = State::NoWord; return *this; } break; } - ++right_; + ++_right; } return *this; } - constexpr bool operator==(word_segmenter const& _rhs) const noexcept + constexpr bool operator==(word_segmenter const& rhs) const noexcept { - return left_ == _rhs.left_ && right_ == _rhs.right_; + return _left == rhs._left && _right == rhs._right; } - constexpr bool operator!=(word_segmenter const& _rhs) const noexcept { return !(*this == _rhs); } + constexpr bool operator!=(word_segmenter const& rhs) const noexcept { return !(*this == rhs); } private: - constexpr word_segmenter(iterator _begin, iterator _end): - left_ { _begin }, - right_ { _begin }, - state_ { _begin != _end ? (isDelimiter(*right_) ? State::NoWord : State::Word) : State::NoWord }, - end_ { _end } + constexpr word_segmenter(iterator begin, iterator end): + _left { begin }, + _right { begin }, + _state { begin != end ? (isDelimiter(*_right) ? State::NoWord : State::Word) : State::NoWord }, + _end { end } { ++*this; } - constexpr bool isDelimiter(char_type _char) const noexcept + constexpr bool isDelimiter(char_type character) const noexcept { - switch (_char) + switch (character) { case ' ': case '\r': @@ -100,10 +100,10 @@ class word_segmenter NoWord }; - iterator left_; - iterator right_; - State state_; - iterator end_; + iterator _left; + iterator _right; + State _state; + iterator _end; }; } // namespace unicode