Factoring out a triple repetition into found_in_list().

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5686 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
This commit is contained in:
Benno Schulenberg 2016-02-26 16:37:23 +00:00
parent c3b9d29502
commit 4d53694a27
2 changed files with 37 additions and 51 deletions

View File

@ -5,6 +5,7 @@
* src/color.c (color_update): Look for a default syntax only when * src/color.c (color_update): Look for a default syntax only when
all else failed -- forego the small, complicating optimization. all else failed -- forego the small, complicating optimization.
* src/color.c (color_update): Strip things bare to see the sameness. * src/color.c (color_update): Strip things bare to see the sameness.
* src/color.c (found_in_list): Factor out this triple repetition.
GNU nano 2.5.3 - 2016.02.25 GNU nano 2.5.3 - 2016.02.25

View File

@ -149,12 +149,36 @@ void nfreeregex(regex_t **r)
*r = NULL; *r = NULL;
} }
/* Try to match the given shibboleth string with one of the regexes in
* the list starting at head. Return TRUE upon success. */
bool found_in_list(regexlisttype *head, const char *shibboleth)
{
regexlisttype *item;
bool not_compiled;
for (item = head; item != NULL; item = item->next) {
not_compiled = (item->ext == NULL);
if (not_compiled) {
item->ext = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(item->ext, fixbounds(item->ext_regex), REG_EXTENDED);
}
if (regexec(item->ext, shibboleth, 0, NULL, 0) == 0)
return TRUE;
if (not_compiled)
nfreeregex(&item->ext);
}
return FALSE;
}
/* Update the color information based on the current filename. */ /* Update the color information based on the current filename. */
void color_update(void) void color_update(void)
{ {
syntaxtype *tmpsyntax; syntaxtype *tmpsyntax;
colortype *tmpcolor; colortype *tmpcolor;
regexlisttype *e;
assert(openfile != NULL); assert(openfile != NULL);
@ -209,22 +233,9 @@ void color_update(void)
for (tmpsyntax = syntaxes; tmpsyntax != NULL; for (tmpsyntax = syntaxes; tmpsyntax != NULL;
tmpsyntax = tmpsyntax->next) { tmpsyntax = tmpsyntax->next) {
for (e = tmpsyntax->extensions; e != NULL; e = e->next) { if (found_in_list(tmpsyntax->extensions, fullname)) {
bool not_compiled = (e->ext == NULL); openfile->syntax = tmpsyntax;
openfile->colorstrings = tmpsyntax->color;
if (not_compiled) {
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
}
if (regexec(e->ext, fullname, 0, NULL, 0) == 0) {
openfile->syntax = tmpsyntax;
openfile->colorstrings = tmpsyntax->color;
break;
}
if (not_compiled)
nfreeregex(&e->ext);
} }
} }
@ -239,22 +250,9 @@ void color_update(void)
for (tmpsyntax = syntaxes; tmpsyntax != NULL; for (tmpsyntax = syntaxes; tmpsyntax != NULL;
tmpsyntax = tmpsyntax->next) { tmpsyntax = tmpsyntax->next) {
for (e = tmpsyntax->headers; e != NULL; e = e->next) { if (found_in_list(tmpsyntax->headers, openfile->fileage->data)) {
bool not_compiled = (e->ext == NULL); openfile->syntax = tmpsyntax;
openfile->colorstrings = tmpsyntax->color;
if (not_compiled) {
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
}
if (regexec(e->ext, openfile->fileage->data, 0, NULL, 0) == 0) {
openfile->syntax = tmpsyntax;
openfile->colorstrings = tmpsyntax->color;
break;
}
if (not_compiled)
nfreeregex(&e->ext);
} }
} }
} }
@ -290,30 +288,17 @@ void color_update(void)
} }
/* Now try and find a syntax that matches the magicstring. */ /* Now try and find a syntax that matches the magicstring. */
for (tmpsyntax = syntaxes; tmpsyntax != NULL; if (magicstring != NULL) {
tmpsyntax = tmpsyntax->next) { for (tmpsyntax = syntaxes; tmpsyntax != NULL;
tmpsyntax = tmpsyntax->next) {
for (e = tmpsyntax->magics; e != NULL; e = e->next) { if (found_in_list(tmpsyntax->magics, magicstring)) {
bool not_compiled = (e->ext == NULL);
if (not_compiled) {
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
}
if (magicstring && regexec(e->ext, magicstring, 0, NULL, 0) == 0) {
openfile->syntax = tmpsyntax; openfile->syntax = tmpsyntax;
openfile->colorstrings = tmpsyntax->color; openfile->colorstrings = tmpsyntax->color;
break; break;
} }
if (not_compiled)
nfreeregex(&e->ext);
} }
if (openfile->syntax != NULL)
break;
} }
if (stat(openfile->filename, &fileinfo) == 0) if (stat(openfile->filename, &fileinfo) == 0)
magic_close(cookie); magic_close(cookie);
} }