]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: tools: add memupper() / memlower() to convert a memory block
authorWilly Tarreau <w@1wt.eu>
Tue, 1 Dec 2026 06:59:26 +0000 (07:59 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 1 Dec 2026 07:17:13 +0000 (08:17 +0100)
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.

include/haproxy/tools.h
src/sample.c

index 118f8539a48eb969c50d55b61a0b9d69e676f25e..d0909081afb9b9f40f83fb2cdd1784b556cfe956 100644 (file)
@@ -608,6 +608,48 @@ static forceinline void memcpy_opt_small(void *dst, const void *src, size_t len)
                memcpy(d, s, len);
 }
 
+/* copies <len> bytes from <src> to <dst>, 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 <len> bytes from <src> to <dst>, 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 <key> in
  * ID tree <root>. Zero is returned if no place is found.
  */
index 96ace8f5db0e377d3c72c84d08cbb0e3acb73aa8..31ff27203f3de1db259c3ca5d7bd76bbc4642b91 100644 (file)
@@ -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;
 
-       /* <dst> may alias <src>, we never write ahead of where we read */
+       /* output sample may alias <src>, 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;
 
-       /* <dst> may alias <src>, we never write ahead of where we read */
+       /* output sample may alias <src>, 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;
 }