[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: KURL Patch
From:       Frerich Raabe <raabe () kde ! org>
Date:       2001-10-13 20:23:20
[Download RAW message or body]

On Saturday 13 October 2001 21:09, Carsten Pfeiffer wrote:
> On Samstag, 13. Oktober 2001 20:28 Frerich Raabe wrote:
> > Hi,
> >
> > I attached a patch for kurl.* which adds a method 'queryValue()', making
> > it possible to do stuff like
> >
> > KURL u = "http://www.foo.bar/yoyodyne.php?q1=a&q2=b";
> > cout << u.queryItem("q1") << endl; // prints 'a'
>
> If you have ?foobar=1?foo=2
> then queryItem("foo") will return 1 instead of 2. And ?foo= should return
> "" instead of QString::null, I think.

Agreed. I merged your patch with my current version which renames queryValue() 
to queryItem() (since we're not returning the value of the query but rather 
the value of one of the query's items), saved a few calls to _item.length() 
and item.length() and made it a const method. I also adjusted kurltest to 
cover the issues you pointed out. How about this one?

- Frerich

["kurl.diff" (text/x-diff)]

Index: kurl.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kurl.cpp,v
retrieving revision 1.183
diff -u -3 -p -r1.183 kurl.cpp
--- kurl.cpp	2001/10/13 04:19:05	1.183
+++ kurl.cpp	2001/10/13 20:19:02
@@ -1660,3 +1660,30 @@ bool urlcmp( const QString& _url1, const
 
   return true;
 }
+
+QString KURL::queryItem( const QString& _item ) const
+{
+  if ( m_strQuery_encoded.length() <= 1 )
+    return QString::null;
+
+  QStringList items = QStringList::split( '&', m_strQuery_encoded );
+  int _len = _item.length();
+  QString item;
+  for ( QStringList::ConstIterator it = items.begin(); it != items.end(); ++it )
+  {
+    item = (*it);
+    if ( item.startsWith( _item ) ) 
+    {
+      int len = item.length();
+      if ( len > _len && item.at( _len ) == '=' )
+      {
+        if ( len > ++_len )
+          return item.mid( _len );
+        else // empty value
+          return QString::fromLatin1("");
+      }
+    }
+  }
+
+  return QString::null;
+}

["kurltest.diff" (text/x-diff)]

Index: kurltest.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/tests/kurltest.cpp,v
retrieving revision 1.46
diff -u -3 -p -r1.46 kurltest.cpp
--- kurltest.cpp	2001/10/11 11:07:57	1.46
+++ kurltest.cpp	2001/10/13 20:19:23
@@ -441,6 +441,13 @@ int main(int argc, char *argv[])
   check("setEncodedPathAndQuery test#1", tobi1.query(), "?another&query");
   check("setEncodedPathAndQuery test#2", tobi1.path(), "another/path");
 
+  KURL theKow = "http://www.google.de/search?q=frerich&hlx=xx&hl=de&empty=&lr=lang_de";
+  check("queryItem (first item)", theKow.queryItem("q"), "frerich");
+  check("queryItem (middle item)", theKow.queryItem("hl"), "de");
+  check("queryItem (last item)", theKow.queryItem("lr"), "lang_de");
+  check("queryItem (invalid item)", theKow.queryItem("InterstellarCounselor"), QString::null);
+  check("queryItem (empty item)", theKow.queryItem("empty"), "");
+
   printf("\nTest OK !\n");
 }
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic