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

List:       kde-core-devel
Subject:    Re: [PATCH] make ksnapshot increment the last number
From:       "=?UTF-8?Q?Ho=C3=A0ng_=C4=90=E1=BB=A9c_Hi=E1=BA=BFu?=" <hieu.d.hoang () gmail ! com>
Date:       2007-10-07 5:19:35
Message-ID: 4f12b0dd0710062219h4bdf046fxa02f1d544c3dacaa () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (text/plain)]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

  Hi,

 My previous patch has a problem, it only matches the last digit in
the filename, so "snapshot19.png" is followed by "snapshot110.png".
amrit on #qt suggested using "[^\\d](\\d+)" to match as far to the
left as possible. This regexp doesn't match when the only number is at
the start of the filename, so I used a second regexp to match that
case.

 Hi Aaron, lucky that you haven't committed the previous one. I am
applying for an svn account, so I might be able to commit this and be
responsible for it in the log. Wish me luck :-)

 Thanks again for any feedback.

  Hiếu
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHCGxUh4dScvxKz04RAr0jAJwLJ0q/4D83PVS1OkDZjSomAUFRoACghvRJ
ieiTppgtJ79xDTAC7/Egy7M=
=Cf5/
-----END PGP SIGNATURE-----

["ksnapshot-increment-last-number-fixed.patch" (text/x-diff)]

Index: ksnapshotobject.cpp
===================================================================
--- ksnapshotobject.cpp	(revision 722187)
+++ ksnapshotobject.cpp	(working copy)
@@ -54,17 +54,24 @@ void KSnapshotObject::autoincFilename()
     QString name= filename.fileName();
 
     // If the name contains a number then increment it
-    QRegExp numSearch("[0-9]+");
+    QRegExp numSearch("[^\\d](\\d+)"); // breaks when the only number is at the start
+    QRegExp numAtStart("(\\d+)"); // accounts for that breakage
+    QRegExp* usedRe = &numSearch;
 
     // Does it have a number?
-    int start = numSearch.indexIn(name);
+    int start = numSearch.lastIndexIn(name);
+    if (start == -1 ) {
+        start = numAtStart.indexIn(name);
+        usedRe = &numAtStart;
+    }
+
     if (start != -1) {
         // It has a number, increment it
-        int len = numSearch.matchedLength();
-        QString numAsStr= name.mid(start, len);
+        start = usedRe->pos(1); // we are only interested in the group
+        QString numAsStr = usedRe->capturedTexts()[1];
         QString number = QString::number(numAsStr.toInt() + 1);
-        number = number.rightJustified( len, '0');
-        name.replace(start, len, number );
+        number = number.rightJustified(numAsStr.length(), '0');
+        name.replace(start, number.length(), number);
     }
     else {
         // no number


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

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