Skip to content

Commit

Permalink
rg_utils: Optimize rg_extension by not scanning the string twice
Browse files Browse the repository at this point in the history
  • Loading branch information
ducalex committed Jul 28, 2024
1 parent c35dd9f commit a5ed8b8
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions components/retro-go/rg_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,20 @@ const char *rg_extension(const char *filename)
{
if (!filename)
return NULL;

const char *ptr = rg_basename(filename);
const char *ext = strrchr(ptr, '.');
if (!ext)
return ptr + strlen(ptr);
return ext + 1;
const char *ptr = filename + strlen(filename) - 1;
while (ptr > filename && *ptr != '/')
{
if (*ptr == '.')
return ptr + 1;
ptr--;
}
return NULL;
}

bool rg_extension_match(const char *filename, const char *extensions)
{
const char *ext = rg_extension(filename);
if (!ext || !*ext || !extensions || !*extensions)
if (!ext || !extensions)
return false;

const char *haystack = extensions;
Expand Down

0 comments on commit a5ed8b8

Please sign in to comment.