Re: [hackers] [sbase][PATCH] cp: add -i flag

From: THIBAUT AUBIN <t.aubin20_AT_ejm.org>
Date: Sat, 19 Apr 2025 02:10:35 +0200

Thank you Roberto for your proposal,
I changed `isspace(3)` to `isblank(3)` as it is unlikely for a user to
write leading `\v`, `\f`, `\r`, and it prevents checking `\n` twice.
You are right `toupper(3)` is correct, however the direct comparison is the
fastest and most efficient option:

1) c == 'y' || c == 'Y'
Easily optimized and branch-predicted by the CPU, just a couple of
instructions (compare + jump). No memory access, no function call. e.g:

cmp c, 'y' ; compare c to 'y' (0x79)
je matched ; if equal, jump
cmp c, 'Y' ; compare c to 'Y' (0x59)
je matched ; if equal, jump


2) toupper(c) == 'Y'
This is usually involve a library call or locale table lookup (especially
in locale-aware systems) unless the compiler can inline it.
It often require checking if c is between 'a' and 'z', if so, subtracting
32 to make it uppercase. It is more instructions, more branches. e.g:

movzx eax, byte ptr [c] ; zero-extend c into eax
cmp al, 'a'
jb not_lower
cmp al, 'z'
ja not_lower
sub al, 0x20 ; convert to uppercase
not_lower:
cmp al, 'Y'
je matched


Finally, I took the liberty of exposing `xvprintf(3)` and moved the logic
in a new `confirm.c` file in order to be reused (e.g: `mv(1)`, `rm(1)`).

---8<
Received on Sat Apr 19 2025 - 02:10:35 CEST

This archive was generated by hypermail 2.3.0 : Sat Apr 19 2025 - 02:24:38 CEST