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

List:       kfm-devel
Subject:    Re: PATCH: KonqRun::askSave and URLs containing '%'
From:       Maarten ter Huurne <mth () stack ! nl>
Date:       2001-08-29 19:45:12
[Download RAW message or body]

On Saturday 25 August 2001 16:04, Carsten Pfeiffer wrote:
> On Sat, Aug 25, 2001 at 04:30:33PM +0000, Maarten ter Huurne wrote:
> > Where should this functionality be located? As a function, as a static
> > method or as an instance method? And in case of a method, in which class?
> > Should it be part of KDE or should we try to get TrollTech to put it in
> > Qt? Since it is a more general replacement for QString::arg, my opinion
> > is that it belongs in QString.
>
> Until QString offers this, we could put it into KStringHandler (kdecore).

Attached patches add an "arg" method to KStringHandler, in the way David 
suggested (using default arguments). They are diffs from the KDE 2.2 code.

I think this is a convenient, safe and efficient way to substitute multiple 
arguments. I verified the code by reviewing and running a few tests, but 
please double check it just in case.

Bye,
		Maarten

["kstringhandler.h.diff" (text/x-diff)]

--- kdelibs-2.2/kdecore/kstringhandler.h	Sun Jan 28 23:38:54 2001
+++ kstringhandler.h	Wed Aug 29 21:33:10 2001
@@ -234,5 +234,32 @@
      */
     static QStringList perlSplit
       (const QRegExp & sep, const QString & s, uint max = 0);
+
+    /**
+     * Substitutes QString arguments into a QString.
+     * The positions to substitute at are indicated by a percent followed
+     * by a single digit.
+     *
+     * Example:
+     * arg("%3 does the %1 %2!", "hurting", "stop", "When")
+     *  == "When does the hurting stop!"
+     *
+     * This method is similar to QString::arg, with the following differences:
+     * - multiple arguments can be substituted at once
+     * - %n is always substituted by argument n, while QString::arg substitutes
+     *   the lowest %n in the string
+     * - only arguments of type QString are accepted
+     *
+     * @param s is the string to substitute in.
+     * @param arg<n> is the argument to substitute for "%n".
+     *   Null strings are not substituted: the "%n" will remain in the result.
+     * @return The string s after substitution.
+     */
+    static QString arg(const QString &s, const QString &arg1,
+        const QString &arg2 = QString::null, const QString &arg3 = QString::null,
+        const QString &arg4 = QString::null, const QString &arg5 = QString::null,
+        const QString &arg6 = QString::null, const QString &arg7 = QString::null,
+        const QString &arg8 = QString::null, const QString &arg9 = QString::null);
+
 };
 #endif

["kstringhandler.cpp.diff" (text/x-diff)]

--- kdelibs-2.2/kdecore/kstringhandler.cpp	Thu Jun 21 14:06:48 2001
+++ kstringhandler.cpp	Wed Aug 29 21:49:33 2001
@@ -507,3 +507,34 @@
   return l;
 }
 
+QString KStringHandler::arg(const QString &s,
+    const QString &arg1, const QString &arg2, const QString &arg3,
+    const QString &arg4, const QString &arg5, const QString &arg6,
+    const QString &arg7, const QString &arg8, const QString &arg9)
+{
+    int i = 0, j;
+    QString ret;
+    while ((j = s.find('%', i)) != -1) {
+        ret += s.mid(i, j - i);
+        i = j + 1; // skip '%'
+        const QString *argn = &QString::null;
+        switch(s[i].digitValue()) { // -1 if no digit
+            case 1: argn = &arg1; break;
+            case 2: argn = &arg2; break;
+            case 3: argn = &arg3; break;
+            case 4: argn = &arg4; break;
+            case 5: argn = &arg5; break;
+            case 6: argn = &arg6; break;
+            case 7: argn = &arg7; break;
+            case 8: argn = &arg8; break;
+            case 9: argn = &arg9; break;
+        }
+        if (!argn->isNull()) {
+            ret += *argn;
+            i++;
+        }
+        else ret += '%';
+    }
+    ret += s.mid(i);
+    return ret;
+}


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

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