nano

nano with my custom patches
git clone git://bsandro.tech/nano
Log | Files | Refs | README | LICENSE

commit d50e8feed937cf4516c293a5260920f1e3d5e357
parent 8e104117a44ca91b74f618a25a53f9223cae22f4
Author: Benno Schulenberg <bensberg@telfort.nl>
Date:   Tue,  6 May 2025 15:57:42 +0200

tweaks: avoid using toupper() and tolower() where easily possible

Those functions can misbehave for some characters in certain locales.

Diffstat:
Msrc/rcfile.c | 9+++++----
Msrc/winio.c | 8++++----
2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/rcfile.c b/src/rcfile.c @@ -756,10 +756,11 @@ void parse_binding(char *ptr, bool dobind) keycopy = copy_of(keyptr); /* Uppercase either the second or the first character of the key name. */ - if (keycopy[0] == '^') - keycopy[1] = toupper((unsigned char)keycopy[1]); - else - keycopy[0] = toupper((unsigned char)keycopy[0]); + if (keycopy[0] == '^') { + if ('a' <= keycopy[1] && keycopy[1] <= 'z') + keycopy[1] &= 0x5F; + } else if ('a' <= keycopy[0] && keycopy[0] <= 'z') + keycopy[0] &= 0x5F; /* Verify that the key name is not too short. */ if (keycopy[1] == '\0' || (keycopy[0] == 'M' && keycopy[2] == '\0')) { diff --git a/src/winio.c b/src/winio.c @@ -1046,8 +1046,8 @@ int parse_kbinput(WINDOW *frame) meta_key = TRUE; } else if (waiting_codes == 0 || nextcodes[0] == ESC_CODE || (keycode != 'O' && keycode != '[')) { - if (!shifted_metas) - keycode = tolower(keycode); + if ('A' <= keycode && keycode <= 'Z' && !shifted_metas) + keycode |= 0x20; meta_key = TRUE; } else keycode = parse_escape_sequence(keycode); @@ -1105,8 +1105,8 @@ int parse_kbinput(WINDOW *frame) /* If the first escape arrived alone but not the second, then it * is a Meta keystroke; otherwise, it is an "Esc Esc control". */ if (first_escape_was_alone && !last_escape_was_alone) { - if (!shifted_metas) - keycode = tolower(keycode); + if ('A' <= keycode && keycode <= 'Z' && !shifted_metas) + keycode |= 0x20; meta_key = TRUE; } else keycode = convert_to_control(keycode);