[master] 0c3ec1692 Simplify std.tolower() and std.toupper() with WS_VSB

Poul-Henning Kamp phk at FreeBSD.org
Mon Feb 10 12:29:07 UTC 2020


commit 0c3ec169282427e629f211dc17c4afd1df2bb9da
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Feb 10 11:40:22 2020 +0000

    Simplify std.tolower() and std.toupper() with WS_VSB

diff --git a/lib/libvmod_std/vmod_std.c b/lib/libvmod_std/vmod_std.c
index 51a9b7e16..1fe02fdcf 100644
--- a/lib/libvmod_std/vmod_std.c
+++ b/lib/libvmod_std/vmod_std.c
@@ -46,6 +46,7 @@
 #include "vrnd.h"
 #include "vtcp.h"
 #include "vsa.h"
+#include "vsb.h"
 #include "vtim.h"
 #include "vcl.h"
 
@@ -66,56 +67,34 @@ vmod_set_ip_tos(VRT_CTX, VCL_INT tos)
 	    IPPROTO_IP, IP_TOS, &itos, sizeof(itos)));
 }
 
-static const char *
-vmod_updown(VRT_CTX, int up, VCL_STRANDS s)
-{
-	unsigned u;
-	char *b, *e;
-	const char *p;
-	int i;
-
-	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
-	u = WS_ReserveAll(ctx->ws);
-	e = b = ctx->ws->f;
-	e += u;
-	for (i = 0; i < s->n && b < e; i++) {
-		p = s->p[i];
-		while (p != NULL && *p != '\0' && b < e) {
-			if (up)
-				*b++ = (char)toupper(*p++);
-			else
-				*b++ = (char)tolower(*p++);
-		}
-	}
-	if (b < e)
-		*b = '\0';
-	b++;
-	if (b > e) {
-		WS_MarkOverflow(ctx->ws);
-		WS_Release(ctx->ws, 0);
-		return (NULL);
-	} else {
-		e = b;
-		b = ctx->ws->f;
-		WS_Release(ctx->ws, e - b);
-		return (b);
-	}
-}
-
 VCL_STRING v_matchproto_(td_std_toupper)
 vmod_toupper(VRT_CTX, VCL_STRANDS s)
 {
+	const char *p;
+	struct vsb vsb[1];
+	int i;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
-	return (vmod_updown(ctx, 1, s));
+	WS_VSB_new(vsb, ctx->ws);
+	for (i = 0; i < s->n; i++)
+		for (p = s->p[i]; p != NULL && *p != '\0'; p++)
+			VSB_putc(vsb, (char)toupper(*p));
+	return (WS_VSB_finish(vsb, ctx->ws, NULL));
 }
 
 VCL_STRING v_matchproto_(td_std_tolower)
 vmod_tolower(VRT_CTX, VCL_STRANDS s)
 {
+	const char *p;
+	struct vsb vsb[1];
+	int i;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
-	return (vmod_updown(ctx, 0, s));
+	WS_VSB_new(vsb, ctx->ws);
+	for (i = 0; i < s->n; i++)
+		for (p = s->p[i]; p != NULL && *p != '\0'; p++)
+			VSB_putc(vsb, (char)tolower(*p));
+	return (WS_VSB_finish(vsb, ctx->ws, NULL));
 }
 
 VCL_REAL v_matchproto_(td_std_random)


More information about the varnish-commit mailing list