C++11
Mark Kretschmann
kretschmann at kde.org
Thu May 9 17:24:51 UTC 2013
On Wed, May 8, 2013 at 3:51 PM, Edward Toroshchin
<edward.hades at gmail.com> wrote:
> On Wed, May 08, 2013 at 02:50:26PM +0200, Mark Kretschmann wrote:
>> I'd really like to start using some C++11 features in Amarok. It has been
>> argued that only few compilers support all of C++11's features at this
>> point, and that we therefore couldn't use C++11.
>
> I support going to C++11.
I'm sometimes doing little experiments with C++11 usage in Amarok,
just to see how much it could improve our code. When I was hacking on
this crash workaround for the Wikipedia applet, I found the code
particularly awkward to work with. Lots of redundancy.
With some Lambda usage, it becomes a good deal more readable and
pleasant to work with, and less error prone. This is how it could look
instead:
>From 7cca04eb25169e5befb3e5d6a1290795c3e651b8 Mon Sep 17 00:00:00 2001
From: Mark Kretschmann <kretschmann at kde.org>
Date: Thu, 9 May 2013 19:08:24 +0200
Subject: [PATCH] Replace redundant code with one Lambda function.
---
src/context/engines/wikipedia/WikipediaEngine.cpp | 68 ++++++-----------------
1 file changed, 17 insertions(+), 51 deletions(-)
diff --git a/src/context/engines/wikipedia/WikipediaEngine.cpp
b/src/context/engines/wikipedia/WikipediaEngine.cpp
index 0692b8d..e70c1a2 100644
--- a/src/context/engines/wikipedia/WikipediaEngine.cpp
+++ b/src/context/engines/wikipedia/WikipediaEngine.cpp
@@ -776,61 +776,27 @@ WikipediaEnginePrivate::wikiParse( QString &wiki )
wikiRef = wiki.midRef( wikiRef.position(), wiki.indexOf(
QLatin1String("<div class=\"printfooter\">"), wikiRef.position() ) -
wikiRef.position() );
wiki = wikiRef.toString();
- { // lets remove the warning box
- const QString wBox = QLatin1String("<table
class=\"metadata plainlinks ambox");
- const QString wBoxEnd = QLatin1String("</table>");
- const int wBoxEndSize = wBoxEnd.size();
- int matchIndex = 0;
- QStringMatcher mboxMatcher( wBox );
- while( (matchIndex = mboxMatcher.indexIn(wiki, matchIndex)) != -1 )
- {
- const int nToBoxEnd = wiki.indexOf( wBoxEnd, matchIndex )
- matchIndex;
- QStringRef wBoxRef = wiki.midRef( matchIndex, nToBoxEnd +
wBoxEndSize );
- wiki.remove( wBoxRef.toString() );
- }
- }
-
- { // remove protection policy (we don't do edits)
- const QString protec = QLatin1String("<div><a
href=\"/wiki/Wikipedia:Protection_policy") ;
- const QString protecEnd = QLatin1String("</a></div>") ;
- const int protecEndSize = protecEnd.size();
- int matchIndex = 0;
- QStringMatcher protecMatcher( protec );
- while( (matchIndex = protecMatcher.indexIn(wiki, matchIndex)) != -1 )
- {
- const int nToBoxEnd = wiki.indexOf( protecEnd, matchIndex
) - matchIndex;
- QStringRef pRef = wiki.midRef( matchIndex, nToBoxEnd +
protecEndSize );
- wiki.remove( pRef.toString() );
- }
- }
-
- { // lets also remove the "lock" image
- const QString topIcon = QLatin1String("<div class=\"metadata
topicon\" ");
- const QString topIconEnd = QLatin1String("</a></div>");
- const int topIconEndSize = topIconEnd.size();
- int matchIndex = 0;
- QStringMatcher topIconMatcher( topIcon );
- while( (matchIndex = topIconMatcher.indexIn(wiki, matchIndex)) != -1 )
- {
- const int nToBoxEnd = wiki.indexOf( topIconEnd,
matchIndex ) - matchIndex;
- QStringRef tRef = wiki.midRef( matchIndex, nToBoxEnd +
topIconEndSize );
- wiki.remove( tRef.toString() );
- }
- }
-
- { // remove <audio> tags (can lead to crashes in QtWebKit)
- const QString tag = QLatin1String("<audio");
- const QString tagEnd = QLatin1String("</audio>");
+ auto removeTag = [] ( QString& text, const QString& tagStart,
const QString& tagEnd )
+ {
const int tagEndSize = tagEnd.size();
int matchIndex = 0;
- QStringMatcher tagMatcher( tag );
- while( (matchIndex = tagMatcher.indexIn(wiki, matchIndex)) != -1 )
+ const QStringMatcher tagMatcher( tagStart );
+ while( ( matchIndex = tagMatcher.indexIn( text, matchIndex ) ) != -1 )
{
- const int nToTagEnd = wiki.indexOf( tagEnd, matchIndex )
- matchIndex;
- QStringRef tagRef = wiki.midRef( matchIndex, nToTagEnd +
tagEndSize );
- wiki.remove( tagRef.toString() );
+ const int nToTagEnd = text.indexOf( tagEnd, matchIndex )
- matchIndex;
+ const QStringRef tagRef = text.midRef( matchIndex,
nToTagEnd + tagEndSize );
+ text.remove( tagRef.toString() );
}
- }
+ };
+
+ // lets remove the warning box
+ removeTag( wiki, "<table class=\"metadata plainlinks ambox", "</table>" );
+ // remove protection policy (we don't do edits)
+ removeTag( wiki, "<div><a
href=\"/wiki/Wikipedia:Protection_policy", "</a></div>" );
+ // lets also remove the "lock" image
+ removeTag( wiki, "<div class=\"metadata topicon\" ", "</a></div>" );
+ // remove <audio> tags (can lead to crashes in QtWebKit)
+ removeTag( wiki, "<audio", "</audio>" );
// Adding back style and license information
wiki = QLatin1String("<div id=\"bodyContent\"") + wiki;
--
1.8.1.2
More information about the Amarok-devel
mailing list