commit ae5c45e446df498d237a8026eab0b8a1aa741b01
parent 5295c08439ba181594f050a2a6c01a39345c023b
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Thu, 12 Jun 2025 22:07:24 +0200
mhd_str: optimised caseless comparisons and case transformations
Diffstat:
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/src/mhd2/mhd_str.c b/src/mhd2/mhd_str.c
@@ -157,7 +157,7 @@ isasciialnum (char c)
MHD_static_inline_ MHD_FN_CONST_ char
toasciilower (char c)
{
- return isasciiupper (c) ? (c - 'A' + 'a') : c;
+ return isasciiupper (c) ? (char) (0x20u | (unsigned char) c) : c;
}
@@ -173,7 +173,7 @@ toasciilower (char c)
MHD_static_inline_ MHD_FN_CONST_ char
toasciiupper (char c)
{
- return isasciilower (c) ? (c - 'a' + 'A') : c;
+ return isasciilower (c) ? (char) ((~0x20u) & (unsigned char) c) : c;
}
@@ -809,12 +809,14 @@ uint8totwoxdigits (uint8_t v)
* @return boolean 'true' if chars are caseless equal, false otherwise
*/
MHD_static_inline_ MHD_FN_CONST_ bool
-charsequalcaseless (const char c1, const char c2)
+charsequalcaseless (char c1, char c2)
{
- return ( (c1 == c2) ||
- (((c1 - 'A' + 'a') == c2) ?
- isasciiupper (c1) :
- ((c1 == (c2 - 'A' + 'a')) && isasciiupper (c2))) );
+ if (c1 == c2)
+ return true;
+ /* Fold case on both sides */
+ c1 = ((char) (~0x20u & (unsigned char) c1));
+ c2 = ((char) (~0x20u & (unsigned char) c2));
+ return (c1 == c2) && isasciiupper (c1);
}
@@ -962,11 +964,10 @@ static const char map_value_to_xdigit[16] =
* @param c2 the second char to compare
* @return boolean 'true' if chars are caseless equal, false otherwise
*/
-# define charsequalcaseless(c1, c2) \
+#define charsequalcaseless(c1, c2) \
( ((c1) == (c2)) || \
- ((((c1) - 'A' + 'a') == (c2)) ? \
- isasciiupper (c1) : \
- (((c1) == ((c2) - 'A' + 'a')) && isasciiupper (c2))) )
+ (((0x20u | (unsigned char) c1) == (0x20u | (unsigned char) c2)) && \
+ toasciilower (((char) (0x20u | (unsigned char) c2)))) )
#endif /* !HAVE_INLINE_FUNCS */