From bbc85e7bb9cac835823782d0fd2e8f6b3aa77946 Mon Sep 17 00:00:00 2001 From: Tobias Wallner Date: Tue, 10 Oct 2023 14:03:56 +0200 Subject: [PATCH 1/4] no need to have a switch statement or branches, that code can be easily made branchless. Let the compiler optimize the boolean logic like crazy, because it can --- cpp-terminal/platforms/conversion.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/cpp-terminal/platforms/conversion.cpp b/cpp-terminal/platforms/conversion.cpp index 5e61b121..1666da2e 100644 --- a/cpp-terminal/platforms/conversion.cpp +++ b/cpp-terminal/platforms/conversion.cpp @@ -105,19 +105,15 @@ std::string utf32_to_utf8(const std::u32string& s) bool is_valid_utf8_code_unit(const std::string& s) { - static const constexpr int b1OOOOOOO{128}; - static const constexpr int b11OOOOOO{192}; - static const constexpr int b111OOOOO{224}; - static const constexpr int b1111OOOO{240}; - static const constexpr int b11111OOO{248}; - switch(s.size()) - { - case 1: return ((s[0] & b1OOOOOOO) == 0) ? true : false; - case 2: return ((s[0] & b111OOOOO) == b11OOOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) ? true : false; - case 3: return ((s[0] & b1111OOOO) == b111OOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO) ? true : false; - case 4: return ((s[0] & b11111OOO) == b1111OOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO) && ((s[3] & b11OOOOOO) == b1OOOOOOO) ? true : false; - default: return false; - } + const constexpr int b1OOOOOOO{128}; + const constexpr int b11OOOOOO{192}; + const constexpr int b111OOOOO{224}; + const constexpr int b1111OOOO{240}; + const constexpr int b11111OOO{248}; + return ((s[0] & b1OOOOOOO) == 0) + || ((s[0] & b111OOOOO) == b11OOOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) + || ((s[0] & b1111OOOO) == b111OOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO) + || ((s[0] & b11111OOO) == b1111OOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO) && ((s[3] & b11OOOOOO) == b1OOOOOOO); } } // namespace Private From 617e3d50de68cff312d4bea97121d15e0bb2c09a Mon Sep 17 00:00:00 2001 From: Tobias Wallner Date: Tue, 10 Oct 2023 14:18:25 +0200 Subject: [PATCH 2/4] added brackets to make the || and && priority clearer --- cpp-terminal/platforms/conversion.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cpp-terminal/platforms/conversion.cpp b/cpp-terminal/platforms/conversion.cpp index 1666da2e..3ad9da1b 100644 --- a/cpp-terminal/platforms/conversion.cpp +++ b/cpp-terminal/platforms/conversion.cpp @@ -105,15 +105,15 @@ std::string utf32_to_utf8(const std::u32string& s) bool is_valid_utf8_code_unit(const std::string& s) { - const constexpr int b1OOOOOOO{128}; - const constexpr int b11OOOOOO{192}; - const constexpr int b111OOOOO{224}; - const constexpr int b1111OOOO{240}; - const constexpr int b11111OOO{248}; + static constexpr const int b1OOOOOOO{128}; + static constexpr const int b11OOOOOO{192}; + static constexpr const int b111OOOOO{224}; + static constexpr const int b1111OOOO{240}; + static constexpr const int b11111OOO{248}; return ((s[0] & b1OOOOOOO) == 0) - || ((s[0] & b111OOOOO) == b11OOOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) - || ((s[0] & b1111OOOO) == b111OOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO) - || ((s[0] & b11111OOO) == b1111OOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO) && ((s[3] & b11OOOOOO) == b1OOOOOOO); + || (((s[0] & b111OOOOO) == b11OOOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO)) + || (((s[0] & b1111OOOO) == b111OOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO)) + || (((s[0] & b11111OOO) == b1111OOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO) && ((s[3] & b11OOOOOO) == b1OOOOOOO)); } } // namespace Private From 4df59ed567b0c5db7fcc7bda2204bbd23b6c2b00 Mon Sep 17 00:00:00 2001 From: Tobias Wallner Date: Tue, 10 Oct 2023 14:19:01 +0200 Subject: [PATCH 3/4] explicitly casted DWORD to int32_t to not have implicit size conversions --- cpp-terminal/platforms/input.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cpp-terminal/platforms/input.cpp b/cpp-terminal/platforms/input.cpp index a0b30ec3..a177494c 100644 --- a/cpp-terminal/platforms/input.cpp +++ b/cpp-terminal/platforms/input.cpp @@ -273,22 +273,22 @@ void Term::Private::Input::read_raw() { case 0: { - setButton(buttons, old_state.dwButtonState, state, 0); + setButton(buttons, static_cast(old_state.dwButtonState), state, 0); break; } case MOUSE_MOVED: { - setButton(buttons, old_state.dwButtonState, state, MOUSE_MOVED); + setButton(buttons, static_cast(old_state.dwButtonState), state, MOUSE_MOVED); break; } case DOUBLE_CLICK: { - setButton(buttons, old_state.dwButtonState, state, DOUBLE_CLICK); + setButton(buttons, static_cast(old_state.dwButtonState), state, DOUBLE_CLICK); break; } case MOUSE_WHEELED: { - setButton(buttons, old_state.dwButtonState, state, MOUSE_WHEELED); + setButton(buttons, static_cast(old_state.dwButtonState), state, MOUSE_WHEELED); if(state > 0) buttons[static_cast(Term::Button::Type::Wheel)] = Button(Term::Button::Type::Wheel, Term::Button::Action::RolledUp); else buttons[static_cast(Term::Button::Type::Wheel)] = Button(Term::Button::Type::Wheel, Term::Button::Action::RolledDown); @@ -296,7 +296,7 @@ void Term::Private::Input::read_raw() } case MOUSE_HWHEELED: { - setButton(buttons, old_state.dwButtonState, state, MOUSE_HWHEELED); + setButton(buttons, static_cast(old_state.dwButtonState), state, MOUSE_HWHEELED); if(state > 0) buttons[static_cast(Term::Button::Type::Wheel)] = Button(Term::Button::Type::Wheel, Term::Button::Action::ToRight); else buttons[static_cast(Term::Button::Type::Wheel)] = Button(Term::Button::Type::Wheel, Term::Button::Action::ToLeft); From 0980d433eea2005691be27b54f8dffea5a77c672 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 12:22:33 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- cpp-terminal/platforms/conversion.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cpp-terminal/platforms/conversion.cpp b/cpp-terminal/platforms/conversion.cpp index 3ad9da1b..56d1182d 100644 --- a/cpp-terminal/platforms/conversion.cpp +++ b/cpp-terminal/platforms/conversion.cpp @@ -110,10 +110,7 @@ bool is_valid_utf8_code_unit(const std::string& s) static constexpr const int b111OOOOO{224}; static constexpr const int b1111OOOO{240}; static constexpr const int b11111OOO{248}; - return ((s[0] & b1OOOOOOO) == 0) - || (((s[0] & b111OOOOO) == b11OOOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO)) - || (((s[0] & b1111OOOO) == b111OOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO)) - || (((s[0] & b11111OOO) == b1111OOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO) && ((s[3] & b11OOOOOO) == b1OOOOOOO)); + return ((s[0] & b1OOOOOOO) == 0) || (((s[0] & b111OOOOO) == b11OOOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO)) || (((s[0] & b1111OOOO) == b111OOOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO)) || (((s[0] & b11111OOO) == b1111OOOO) && ((s[1] & b11OOOOOO) == b1OOOOOOO) && ((s[2] & b11OOOOOO) == b1OOOOOOO) && ((s[3] & b11OOOOOO) == b1OOOOOOO)); } } // namespace Private