From: Willy Tarreau Date: Tue, 1 Dec 2026 06:59:26 +0000 (+0100) Subject: MINOR: tools: add memupper() / memlower() to convert a memory block X-Git-Url: https://git.kaiwu.me/http/%22../doc/static/%7B@url%7D?a=commitdiff_plain;h=a8e63163edb1fc6428da25ea29910ac86deaadfd;p=haproxy.git MINOR: tools: add memupper() / memlower() to convert a memory block These were taken from sample_conv_str2upper() andample_conv_str2lower() respectively. They're inline and operate on an opaque memory block cast to uchar. They're vectorizable when the compiler knows the length. The original functions were updated to call them, the code does not change. --- diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index 118f8539a..d0909081a 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -608,6 +608,48 @@ static forceinline void memcpy_opt_small(void *dst, const void *src, size_t len) memcpy(d, s, len); } +/* copies bytes from to , converting upper case characters to + * lower case ones. Nothing else is changed. It's expected to be used inlined + * inside other functions operating on short strings (handles one char at a + * time. It is designed this way to permit the compiler to vectorize it when + * it makes sense (easy via cmp/mask/sub since len is known). NUL chars are + * properly copied and none is appended. + */ +static forceinline void memlower(void *dst, const void *src, size_t len) +{ + const uchar *_src = src; + uchar *_dst = dst; + size_t i; + + for (i = 0; i < len; i++) { + if ((_src[i] >= 'A') && (_src[i] <= 'Z')) + _dst[i] = _src[i] + 'a' - 'A'; + else + _dst[i] = _src[i]; + } +} + +/* copies bytes from to , converting lower case characters to + * upper case ones. Nothing else is changed. It's expected to be used inlined + * inside other functions operating on short strings (handles one char at a + * time. It is designed this way to permit the compiler to vectorize it when + * it makes sense (easy via cmp/mask/sub since len is known). NUL chars are + * properly copied and none is appended. + */ +static forceinline void memupper(void *dst, const void *src, size_t len) +{ + const uchar *_src = src; + uchar *_dst = dst; + size_t i; + + for (i = 0; i < len; i++) { + if ((_src[i] >= 'a') && (_src[i] <= 'z')) + _dst[i] = _src[i] + 'A' - 'a'; + else + _dst[i] = _src[i]; + } +} + /* This function returns the first unused key greater than or equal to in * ID tree . Zero is returned if no place is found. */ diff --git a/src/sample.c b/src/sample.c index 96ace8f5d..31ff27203 100644 --- a/src/sample.c +++ b/src/sample.c @@ -2477,40 +2477,24 @@ static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private) { const char *src = smp->data.u.str.area; - char *dst; - int i; - /* may alias , we never write ahead of where we read */ + /* output sample may alias , we never write ahead of where we read */ if (!smp_prepare_out(smp)) return 0; - dst = smp->data.u.str.area; - for (i = 0; i < smp->data.u.str.data; i++) { - if ((src[i] >= 'A') && (src[i] <= 'Z')) - dst[i] = src[i] + 'a' - 'A'; - else - dst[i] = src[i]; - } + memlower(smp->data.u.str.area, src, smp->data.u.str.data); return 1; } static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private) { const char *src = smp->data.u.str.area; - char *dst; - int i; - /* may alias , we never write ahead of where we read */ + /* output sample may alias , we never write ahead of where we read */ if (!smp_prepare_out(smp)) return 0; - dst = smp->data.u.str.area; - for (i = 0; i < smp->data.u.str.data; i++) { - if ((src[i] >= 'a') && (src[i] <= 'z')) - dst[i] = src[i] + 'A' - 'a'; - else - dst[i] = src[i]; - } + memupper(smp->data.u.str.area, src, smp->data.u.str.data); return 1; }