From 04262f09a49a9ecd6731350a87ce1197c0de01c2 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Thu, 10 Mar 2016 20:06:01 +0000
Subject: [PATCH] Turning the linked list of syntaxes upside-down, so that the
 last-defined one comes first, so that a search can stop at the first match
 instead of always having to run through the entire list.

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5716 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog    |  4 ++++
 src/color.c  |  5 +++++
 src/rcfile.c | 29 +++++++++--------------------
 3 files changed, 18 insertions(+), 20 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 8555cd18..0657998e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,10 @@
 	formatter command into a single routine.
 	* src/rcfile.c (parse_header_exp, parse_magic_exp, grab_and_store):
 	Elide the first two functions, and reshuffle parameters in the last.
+	* src/rcfile.c (parse_syntax, parse_rcfile), src/color.c
+	(color_update): Turn the linked list of syntaxes upside-down, so that
+	the last-defined one comes first, so that searching can stop at the
+	first match instead of always having to run through the entire list.
 
 2016-03-09  Benno Schulenberg  <bensberg@justemail.net>
 	* src/rcfile.c (parse_syntax): Produce an adequate error message
diff --git a/src/color.c b/src/color.c
index 3026c2d6..bf91872c 100644
--- a/src/color.c
+++ b/src/color.c
@@ -186,6 +186,7 @@ void color_update(void)
 	    if (strcmp(sint->name, syntaxstr) == 0) {
 		openfile->syntax = sint;
 		openfile->colorstrings = sint->color;
+		break;
 	    }
 	}
 
@@ -216,6 +217,7 @@ void color_update(void)
 	    if (found_in_list(sint->extensions, fullname)) {
 		openfile->syntax = sint;
 		openfile->colorstrings = sint->color;
+		break;
 	    }
 	}
 
@@ -231,6 +233,7 @@ void color_update(void)
 		if (found_in_list(sint->headers, openfile->fileage->data)) {
 		    openfile->syntax = sint;
 		    openfile->colorstrings = sint->color;
+		    break;
 		}
 	    }
 	}
@@ -271,6 +274,7 @@ void color_update(void)
 		    if (found_in_list(sint->magics, magicstring)) {
 			openfile->syntax = sint;
 			openfile->colorstrings = sint->color;
+			break;
 		    }
 		}
 	    }
@@ -287,6 +291,7 @@ void color_update(void)
 	    if (strcmp(sint->name, "default") == 0) {
 		openfile->syntax = sint;
 		openfile->colorstrings = sint->color;
+		break;
 	    }
 	}
     }
diff --git a/src/rcfile.c b/src/rcfile.c
index cf7aea50..cf017fd2 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -124,7 +124,7 @@ static bool opensyntax = FALSE;
 	/* Whether we're allowed to add to the last syntax.  When a file ends,
 	 * or when a new syntax command is seen, this bool becomes FALSE. */
 static syntaxtype *endsyntax = NULL;
-	/* The end of the list of syntaxes. */
+	/* The syntax that is currently being parsed. */
 static colortype *endcolor = NULL;
 	/* The end of the color list for the current syntax. */
 #endif
@@ -298,17 +298,8 @@ void parse_syntax(char *ptr)
 	return;
     }
 
-    if (syntaxes == NULL) {
-	syntaxes = (syntaxtype *)nmalloc(sizeof(syntaxtype));
-	endsyntax = syntaxes;
-    } else {
-	endsyntax->next = (syntaxtype *)nmalloc(sizeof(syntaxtype));
-	endsyntax = endsyntax->next;
-#ifdef DEBUG
-	fprintf(stderr, "Adding new syntax after first one\n");
-#endif
-    }
-
+    /* Initialize a new syntax struct. */
+    endsyntax = (syntaxtype *)nmalloc(sizeof(syntaxtype));
     endsyntax->name = mallocstrcpy(NULL, nameptr);
     endsyntax->extensions = NULL;
     endsyntax->headers = NULL;
@@ -318,7 +309,10 @@ void parse_syntax(char *ptr)
     endsyntax->color = NULL;
     endcolor = NULL;
     endsyntax->nmultis = 0;
-    endsyntax->next = NULL;
+
+    /* Hook the new syntax in at the top of the list. */
+    endsyntax->next = syntaxes;
+    syntaxes = endsyntax;
 
     opensyntax = TRUE;
 
@@ -928,9 +922,6 @@ void parse_rcfile(FILE *rcstream
     char *buf = NULL;
     ssize_t len;
     size_t n = 0;
-#ifndef DISABLE_COLOR
-    syntaxtype *end_syn_save = NULL;
-#endif
 
     while ((len = getline(&buf, &n, rcstream)) > 0) {
 	char *ptr, *keyword, *option;
@@ -973,7 +964,6 @@ void parse_rcfile(FILE *rcstream
 		opensyntax = FALSE;
 		continue;
 	    } else {
-		end_syn_save = endsyntax;
 		endsyntax = sint;
 		opensyntax = TRUE;
 		keyword = ptr;
@@ -1047,9 +1037,8 @@ void parse_rcfile(FILE *rcstream
 #ifndef DISABLE_COLOR
 	/* If we temporarily reset endsyntax to allow extending,
 	 * restore the value here. */
-	if (end_syn_save != NULL) {
-	    endsyntax = end_syn_save;
-	    end_syn_save = NULL;
+	if (endsyntax != syntaxes) {
+	    endsyntax = syntaxes;
 	    opensyntax = FALSE;
 	}
 #endif
-- 
GitLab