From bace79a5715932f093d4c17db5d49af8e6594916 Mon Sep 17 00:00:00 2001
From: nicm <nicm>
Date: Thu, 14 Mar 2019 21:46:08 +0000
Subject: [PATCH] Remove some unnecessary temporary variables and be much less
 strict about spacing in style_parse.

---
 style.c | 65 ++++++++++++++++++++++++++-------------------------------
 1 file changed, 30 insertions(+), 35 deletions(-)

diff --git a/style.c b/style.c
index af4affc8..293c70f1 100644
--- a/style.c
+++ b/style.c
@@ -39,24 +39,24 @@ static struct style style_default = {
 int
 style_parse(struct style *sy, const struct grid_cell *base, const char *in)
 {
-	struct style	 saved;
-	const char	 delimiters[] = " ,";
-	char		 tmp[32];
-	int		 value, fg, bg, attr, flags;
-	size_t		 end;
+	struct style	saved;
+	const char	delimiters[] = " ,";
+	char		tmp[32];
+	int		value;
+	size_t		end;
 
 	if (*in == '\0')
 		return (0);
-	if (strchr(delimiters, in[strlen(in) - 1]) != NULL)
-		return (-1);
 	style_copy(&saved, sy);
 
-	fg = sy->gc.fg;
-	bg = sy->gc.bg;
-	attr = sy->gc.attr;
-	flags = sy->gc.flags;
-
 	do {
+		while (*in != '\0' && strchr(delimiters, *in) != NULL) {
+			in++;
+			end--;
+		}
+		if (*in == '\0')
+			break;
+
 		end = strcspn(in, delimiters);
 		if (end > (sizeof tmp) - 1)
 			goto error;
@@ -64,45 +64,40 @@ style_parse(struct style *sy, const struct grid_cell *base, const char *in)
 		tmp[end] = '\0';
 
 		if (strcasecmp(tmp, "default") == 0) {
-			fg = base->fg;
-			bg = base->bg;
-			attr = base->attr;
-			flags = base->flags;
+			sy->gc.fg = base->fg;
+			sy->gc.bg = base->bg;
+			sy->gc.attr = base->attr;
+			sy->gc.flags = base->flags;
 		} else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
 			if ((value = colour_fromstring(tmp + 3)) == -1)
 				goto error;
 			if (*in == 'f' || *in == 'F') {
 				if (value != 8)
-					fg = value;
+					sy->gc.fg = value;
 				else
-					fg = base->fg;
+					sy->gc.fg = base->fg;
 			} else if (*in == 'b' || *in == 'B') {
 				if (value != 8)
-					bg = value;
+					sy->gc.bg = value;
 				else
-					bg = base->bg;
+					sy->gc.bg = base->bg;
 			} else
 				goto error;
 		} else if (strcasecmp(tmp, "none") == 0)
-			attr = 0;
+			sy->gc.attr = 0;
 		else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {
 			if ((value = attributes_fromstring(tmp + 2)) == -1)
 				goto error;
-			attr &= ~value;
+			sy->gc.attr &= ~value;
 		} else {
 			if ((value = attributes_fromstring(tmp)) == -1)
 				goto error;
-			attr |= value;
+			sy->gc.attr |= value;
 		}
 
 		in += end + strspn(in + end, delimiters);
 	} while (*in != '\0');
 
-	sy->gc.fg = fg;
-	sy->gc.bg = bg;
-	sy->gc.attr = attr;
-	sy->gc.flags = flags;
-
 	return (0);
 
 error:
@@ -122,18 +117,18 @@ style_tostring(struct style *sy)
 	*s = '\0';
 
 	if (gc->fg != 8) {
-		off += xsnprintf(s + off, sizeof s - off, "%sfg=%s",
-		    comma, colour_tostring(gc->fg));
+		off += xsnprintf(s + off, sizeof s - off, "%sfg=%s", comma,
+		    colour_tostring(gc->fg));
 		comma = ",";
 	}
 	if (gc->bg != 8) {
-		off += xsnprintf(s + off, sizeof s - off, "%sbg=%s",
-		    comma, colour_tostring(gc->bg));
+		off += xsnprintf(s + off, sizeof s - off, "%sbg=%s", comma,
+		    colour_tostring(gc->bg));
 		comma = ",";
 	}
 	if (gc->attr != 0 && gc->attr != GRID_ATTR_CHARSET) {
-		xsnprintf(s + off, sizeof s - off, "%s%s",
-		    comma, attributes_tostring(gc->attr));
+		xsnprintf(s + off, sizeof s - off, "%s%s", comma,
+		    attributes_tostring(gc->attr));
 		comma = ",";
 	}
 
@@ -174,7 +169,7 @@ style_apply_update(struct grid_cell *gc, struct options *oo, const char *name)
 void
 style_set(struct style *sy, const struct grid_cell *gc)
 {
-	memset(sy, 0, sizeof *sy);
+	memcpy(sy, &style_default, sizeof *sy);
 	memcpy(&sy->gc, gc, sizeof sy->gc);
 }