File 348d7cbf-kderuntime-nepomuk-47branch-regexp.diff of Package kdebase4-runtime

commit 348d7cbf37e3e0cc638a8c6844f66efee131f6cf
Author: Sebastian Trueg <trueg@kde.org>
Date:   Thu Oct 20 21:31:52 2011 +0200

    Only convert file URLs in REGEX filters that appear at the beginning.

diff --git a/nepomuk/services/storage/removablemediamodel.cpp b/nepomuk/services/storage/removablemediamodel.cpp
index ebd83f1..836668e 100644
--- a/nepomuk/services/storage/removablemediamodel.cpp
+++ b/nepomuk/services/storage/removablemediamodel.cpp
@@ -270,6 +270,7 @@ QString Nepomuk::RemovableMediaModel::convertFileUrls(const QString &query) cons
 
     // is 0, 1, or 3 - nothing else
     int quoteCnt = 0;
+    int literalStartPos = 0;
     bool inRegEx = false;
     bool inRes = false;
     QChar quote;
@@ -293,6 +294,7 @@ QString Nepomuk::RemovableMediaModel::convertFileUrls(const QString &query) cons
                    newQuery.append(c);
                    newQuery.append(c);
                }
+               literalStartPos = i+1;
                continue;
            }
            else if(c == quote) {
@@ -379,12 +381,16 @@ QString Nepomuk::RemovableMediaModel::convertFileUrls(const QString &query) cons
         //
         else if(inRegEx && quoteCnt && c == 'f') {
             // peek forward to see if its a file URL
-            if(i+5 < query.length() &&
-                    query[i+1] == 'i' &&
-                    query[i+2] == 'l' &&
-                    query[i+3] == 'e' &&
-                    query[i+4] == ':' &&
-                    query[i+5] == '/') {
+            // file URLs in regexps only make sense if they appear at the beginning or following a '^'
+            if((i == literalStartPos ||
+                (query[literalStartPos] == '^' &&
+                 i == literalStartPos+1)) &&
+               i+5 < query.length() &&
+               query[i+1] == 'i' &&
+               query[i+2] == 'l' &&
+               query[i+3] == 'e' &&
+               query[i+4] == ':' &&
+               query[i+5] == '/') {
                 // find end of regex
                 QString quoteEnd = quote;
                 if(quoteCnt == 3) {
diff --git a/nepomuk/services/storage/test/removablemediamodeltest.cpp b/nepomuk/services/storage/test/removablemediamodeltest.cpp
index 3a4abee..c47c26d 100644
--- a/nepomuk/services/storage/test/removablemediamodeltest.cpp
+++ b/nepomuk/services/storage/test/removablemediamodeltest.cpp
@@ -164,6 +164,11 @@ void RemovableMediaModelTest::testConvertFileUrlsInQuery_data()
     QTest::newRow("queryWithConvertableRegex2")
             << QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER(REGEX(?u, '^file:///media/nfs/')) . }")
             << QString::fromLatin1("select ?r where { ?r nie:url ?u . FILTER(REGEX(?u, '^nfs://thehost/solid-path/')) . }");
+
+    QTest::newRow("queryWithNotReallyAFileUrl")
+            << QString::fromLatin1("select ?r where { ?r ?p ?u . FILTER(REGEX(?u, 'ffile:///media/nfs/')) . }")
+            << QString::fromLatin1("select ?r where { ?r ?p ?u . FILTER(REGEX(?u, 'ffile:///media/nfs/')) . }");
+    // TODO: add queries that should NOT be converted
 }
 
 void RemovableMediaModelTest::testConvertFileUrlsInQuery()
openSUSE Build Service is sponsored by