From 98612f549202c491c9b97df54e77d413a8e35e8d Mon Sep 17 00:00:00 2001
From: Přemysl Eric Janouch
Date: Fri, 9 Jul 2021 04:36:43 +0200
Subject: Improve C-char parsing
I was hoping that a superoptimiser would help me find a miraculous
branchless equation to cover it, but in the end the branching
doesn't hurt at all in our case.
It's more readable than `(((char >> 2) - 0x38) & 0x60) ^ char`
or `(char ^ 0x40) & (((char >> 1) ^ 0x20) | 0x5f)`.
---
sdn.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/sdn.cpp b/sdn.cpp
index 4636ff3..3565bcb 100644
--- a/sdn.cpp
+++ b/sdn.cpp
@@ -398,9 +398,9 @@ fun decode_attrs (const vector &attrs) -> chtype {
// --- Application -------------------------------------------------------------
-enum { ALT = 1 << 24, SYM = 1 << 25 }; // Outside the range of Unicode
+enum { ALT = 1 << 24, SYM = 1 << 25 }; // Outside the range of Unicode
#define KEY(name) (SYM | KEY_ ## name)
-#define CTRL(char) ((char - 64) & 0x7f) // 60..7f aren't translated correctly
+#define CTRL(char) ((char) == '?' ? 0x7f : (char) & 0x1f)
#define ACTIONS(XX) XX(NONE) XX(HELP) XX(QUIT) XX(QUIT_NO_CHDIR) \
XX(CHOOSE) XX(CHOOSE_FULL) XX(VIEW) XX(EDIT) XX(SORT_LEFT) XX(SORT_RIGHT) \
@@ -1571,11 +1571,11 @@ fun parse_key (const string &key_name) -> wint_t {
return c | g.name_to_key.at (p);
} else if (!strncmp (p, "C-", 2)) {
p += 2;
- if (*p < '?' || *p > 'z') {
+ if (*p < '?' || *p > '~') {
cerr << "bindings: invalid combination: " << key_name << endl;
return WEOF;
}
- c |= CTRL (toupper (*p));
+ c |= CTRL (*p);
p += 1;
} else {
wchar_t w; mbstate_t mb {};
--
cgit v1.2.3-70-g09d2