[master] 1a43a0e1f vte: Add support for left-justified fields

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Mon Aug 28 13:10:06 UTC 2023


commit 1a43a0e1f67c412fe5cc9a4e01b603774f2e36d0
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Tue Aug 22 10:25:00 2023 +0200

    vte: Add support for left-justified fields

diff --git a/include/vte.h b/include/vte.h
index 43279bc9a..fcd13fb2b 100644
--- a/include/vte.h
+++ b/include/vte.h
@@ -30,9 +30,11 @@
  *
  * Align and print fields in a line-based output. Fields are delimited
  * with a horizontal tab HT and lines starting with a space SP are kept
- * verbatim. Lines are delimited with a single new line LF character.
+ * verbatim. Lines are delimited with a single new line LF character. A
+ * field starting with a vertical tab VT is justified to the left.
  *
- * Using non-ASCII or non-printable ASCII character is undefined behavior.
+ * Using non-ASCII or non-printable ASCII characters besides delimiters
+ * and modifiers for fields and lines is undefined behavior.
  */
 
 struct vte;
diff --git a/lib/libvarnish/vte.c b/lib/libvarnish/vte.c
index 44ac71afa..3ebf57086 100644
--- a/lib/libvarnish/vte.c
+++ b/lib/libvarnish/vte.c
@@ -116,6 +116,8 @@ vte_update(struct vte *vte)
 			vte->f_off = -1;
 			continue;
 		}
+		if (vte->f_off >= 0 && vte->f_sz == 0 && *p == '\v')
+			p++;
 		if (*p == '\t' || *p == '\n') {
 			fno = vte->f_off;
 			if (fno >= 0 && vte->f_sz > vte->f_maxsz[fno])
@@ -240,8 +242,8 @@ VTE_finish(struct vte *vte)
 int
 VTE_format(const struct vte *vte, VTE_format_f *func, void *priv)
 {
-	int fno, fsz, nsp;
-	const char *p, *q;
+	int fno, fsz, nsp, just_left;
+	const char *p, *q, *sep;
 
 	CHECK_OBJ_NOTNULL(vte, VTE_MAGIC);
 	AN(func);
@@ -255,22 +257,42 @@ VTE_format(const struct vte *vte, VTE_format_f *func, void *priv)
 	q = p;
 
 	fno = 0;
+	sep = "";
+	just_left = 0;
 	while (*p != 0) {
-		if (fno == 0 && *p == ' ')
+		if (*p == '\v') {
+			if (p - 1 > q) { /* exclude previous separator */
+				VTE_FORMAT(func, priv, "%.*s%s",
+				    (int)(p - 1 - q), q, sep);
+			}
+			q = ++p;
+			just_left = 1;
+		}
+		if (!just_left && fno == 0 && *p == ' ')
 			fsz = strcspn(p, "\n");
 		else
 			fsz = strcspn(p, "\t\n");
 		p += fsz;
 		if (*p == '\t') {
 			assert(vte->f_maxsz[fno] + nsp > fsz);
-			VTE_FORMAT(func, priv, "%.*s%*s",
-			    (int)(p - q), q,
-			    vte->f_maxsz[fno] + nsp - fsz, "");
+			if (just_left) {
+				VTE_FORMAT(func, priv, "%*s%.*s%*s",
+				    vte->f_maxsz[fno] - fsz, "",
+				    (int)(p - q), q,
+				    nsp, "");
+				just_left = 0;
+			} else {
+				VTE_FORMAT(func, priv, "%.*s%*s",
+				    (int)(p - q), q,
+				    vte->f_maxsz[fno] + nsp - fsz, "");
+			}
 			fno++;
 			q = ++p;
+			sep = "";
 		} else if (*p == '\n') {
 			fno = 0;
 			p++;
+			sep = "\n";
 		}
 	}
 


More information about the varnish-commit mailing list