Re: Mimetype optimization
Jakub Stachowski <[email protected]> Wed, 23 Apr 2008 19:48:18 +0200
| Newsgroups | gmane.comp.kde.devel.optimize |
|---|---|
| Message-ID | <[email protected]> |
--Boundary-00=_jZ3DIrVEtF5WCEr
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Dnia wtorek, 22 kwietnia 2008, Olivier Goffart napisa=C5=82:
> Le mardi 22 avril 2008, Jakub Stachowski a =C3=A9crit=C2=A0:
> > Index: kmimetypefactory.cpp
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > --- kmimetypefactory.cpp (wersja 799070)
> > +++ kmimetypefactory.cpp (kopia robocza)
> > @@ -190,8 +190,10 @@
> > int len =3D filename.length();
> >
> > // Patterns like "*~", "*.extension"
> > - if (pattern[0] =3D=3D '*' && len + 1 >=3D pattern_len &&
> > pattern.indexOf('[') =3D=3D -1) + if (pattern[0] =3D=3D '*' &&
> > pattern.indexOf('[') =3D=3D -1)
> > {
> > + if ( len + 1 < pattern_len ) return false;
> > +
>
> What about patern like *foo*bar*
> this will not match "foobar" anymore.
> I think you should search for '*' in the pattern
Right.=20
>
> (oh, and is there some escaping in regexp? such as "*foo\?\?" )
Right. Current parser is somehow limited - it does not detect ? or '\'. The=
re=20
are no pattern with these chars yet, but they may be added.
Attached is a patch to fix these problems.
I added some static QChar() vars (conversion char* -> QChar took ~10% of=20
matchFileName). Is it a problem?
--Boundary-00=_jZ3DIrVEtF5WCEr
Content-Type: text/x-diff;
charset="utf-8";
name="mimematchfix.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="mimematchfix.patch"
Index: kmimetypefactory.cpp
===================================================================
--- kmimetypefactory.cpp (wersja 799923)
+++ kmimetypefactory.cpp (kopia robocza)
@@ -182,15 +182,24 @@
return mimeList;
}
+inline static bool specialChars( const QString& pattern )
+{
+ static QChar bracket('[');
+ static QChar question('?');
+ static QChar slash('\\');
+ return ( pattern.contains(bracket) || pattern.contains(question) || pattern.contains(slash) );
+}
+
static bool matchFileName( const QString &filename, const QString &pattern )
{
int pattern_len = pattern.length();
if (!pattern_len)
return false;
int len = filename.length();
-
+
+ static QChar asterisk('*');
// Patterns like "*~", "*.extension"
- if (pattern[0] == '*' && pattern.indexOf('[') == -1)
+ if (pattern[0] == asterisk && !specialChars(pattern) && pattern.indexOf(asterisk, 1) == -1)
{
if ( len + 1 < pattern_len ) return false;
@@ -203,11 +212,11 @@
}
// Patterns like "README*" (well this is currently the only one like that...)
- if (pattern[pattern_len - 1] == '*') {
- if ( len + 1 < pattern_len ) return false;
- if (pattern[0] == '*')
+ if (pattern[pattern_len - 1] == asterisk && !specialChars(pattern) && pattern.lastIndexOf(asterisk, -2) < 1 ) {
+ if (pattern[0] == asterisk)
return filename.indexOf(pattern.mid(1, pattern_len - 2)) != -1;
+ if ( len + 1 < pattern_len ) return false;
const QChar *c1 = pattern.unicode();
const QChar *c2 = filename.unicode();
int cnt = 1;
@@ -217,7 +226,7 @@
}
// Names without any wildcards like "README"
- if (pattern.indexOf('[') == -1 && pattern.indexOf('*') == -1 && pattern.indexOf('?'))
+ if (!pattern.contains(asterisk) && !specialChars(pattern))
return (pattern == filename);
// Other patterns, like "[Mm]akefile": use slow but correct method
--Boundary-00=_jZ3DIrVEtF5WCEr
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Kde-optimize mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kde-optimize
--Boundary-00=_jZ3DIrVEtF5WCEr--