Use the glob matching for filename

Regex is expensive to compare filenames. we already have the glob matching
and it works enough in this case.

Prior to this change, renaming FcConfigGlobMatch() to FcStrGlobMatch() and moving to fcstr.c
This commit is contained in:
Akira TAGOH 2013-05-08 11:57:49 +09:00
parent 03216ccf4c
commit f6244d2cf2
4 changed files with 51 additions and 49 deletions

View File

@ -2210,50 +2210,6 @@ FcConfigGlobAdd (FcConfig *config,
return FcStrSetAdd (set, glob); return FcStrSetAdd (set, glob);
} }
static FcBool
FcConfigGlobMatch (const FcChar8 *glob,
const FcChar8 *string)
{
FcChar8 c;
while ((c = *glob++))
{
switch (c) {
case '*':
/* short circuit common case */
if (!*glob)
return FcTrue;
/* short circuit another common case */
if (strchr ((char *) glob, '*') == 0)
{
size_t l1, l2;
l1 = strlen ((char *) string);
l2 = strlen ((char *) glob);
if (l1 < l2)
return FcFalse;
string += (l1 - l2);
}
while (*string)
{
if (FcConfigGlobMatch (glob, string))
return FcTrue;
string++;
}
return FcFalse;
case '?':
if (*string++ == '\0')
return FcFalse;
break;
default:
if (*string++ != c)
return FcFalse;
break;
}
}
return *string == '\0';
}
static FcBool static FcBool
FcConfigGlobsMatch (const FcStrSet *globs, FcConfigGlobsMatch (const FcStrSet *globs,
const FcChar8 *string) const FcChar8 *string)
@ -2261,7 +2217,7 @@ FcConfigGlobsMatch (const FcStrSet *globs,
int i; int i;
for (i = 0; i < globs->num; i++) for (i = 0; i < globs->num; i++)
if (FcConfigGlobMatch (globs->strs[i], string)) if (FcStrGlobMatch (globs->strs[i], string))
return FcTrue; return FcTrue;
return FcFalse; return FcFalse;
} }

View File

@ -1089,6 +1089,10 @@ FcStrContainsWord (const FcChar8 *s1, const FcChar8 *s2);
FcPrivate int FcPrivate int
FcStrMatchIgnoreCaseAndDelims (const FcChar8 *s1, const FcChar8 *s2, const FcChar8 *delims); FcStrMatchIgnoreCaseAndDelims (const FcChar8 *s1, const FcChar8 *s2, const FcChar8 *delims);
FcPrivate FcBool
FcStrGlobMatch (const FcChar8 *glob,
const FcChar8 *string);
FcPrivate FcBool FcPrivate FcBool
FcStrUsesHome (const FcChar8 *s); FcStrUsesHome (const FcChar8 *s);

View File

@ -196,12 +196,10 @@ FcCompareFilename (FcValue *v1, FcValue *v2)
return 0.0; return 0.0;
else if (FcStrCmpIgnoreCase (s1, s2) == 0) else if (FcStrCmpIgnoreCase (s1, s2) == 0)
return 1.0; return 1.0;
else if (FcStrRegexCmp (s2, s1)) else if (FcStrGlobMatch (s1, s2))
return 2.0; return 2.0;
else if (FcStrRegexCmpIgnoreCase (s2, s1))
return 3.0;
else else
return 4.0; return 3.0;
} }
#define PRI_NULL(n) \ #define PRI_NULL(n) \

View File

@ -459,6 +459,50 @@ FcStrMatchIgnoreCaseAndDelims (const FcChar8 *s1, const FcChar8 *s2, const FcCha
return w1.src - s1 - 1; return w1.src - s1 - 1;
} }
FcBool
FcStrGlobMatch (const FcChar8 *glob,
const FcChar8 *string)
{
FcChar8 c;
while ((c = *glob++))
{
switch (c) {
case '*':
/* short circuit common case */
if (!*glob)
return FcTrue;
/* short circuit another common case */
if (strchr ((char *) glob, '*') == 0)
{
size_t l1, l2;
l1 = strlen ((char *) string);
l2 = strlen ((char *) glob);
if (l1 < l2)
return FcFalse;
string += (l1 - l2);
}
while (*string)
{
if (FcStrGlobMatch (glob, string))
return FcTrue;
string++;
}
return FcFalse;
case '?':
if (*string++ == '\0')
return FcFalse;
break;
default:
if (*string++ != c)
return FcFalse;
break;
}
}
return *string == '\0';
}
const FcChar8 * const FcChar8 *
FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2) FcStrStrIgnoreCase (const FcChar8 *s1, const FcChar8 *s2)
{ {