1 // Copyright (C) 2004 Shintaro Matsuoka <shin@shoegazed.org>
2 // Copyright (C) 2006 Martin Aumueller <aumuell@reserv.at>
3 // See COPYING file for licensing information
5 #ifndef AMAROK_QSTRINGX_H
6 #define AMAROK_QSTRINGX_H
11 #include <QStringList>
17 class QStringx
: public QString
21 QStringx( QChar ch
) : QString( ch
) {}
22 QStringx( const QString
& s
) : QString( s
) {}
23 QStringx( const QByteArray
& ba
) : QString( ba
) {}
24 QStringx( const QChar
* unicode
, uint length
) : QString( unicode
, length
) {}
25 QStringx( const char* str
) : QString( str
) {}
26 virtual ~QStringx() {}
28 // the numbers following % obviously are not taken into account
29 QString
args( const QStringList
& args
) const
31 const QStringList text
= (*this).split( QRegExp( "%\\d+" ), QString::KeepEmptyParts
);
33 QList
<QString
>::ConstIterator itrText
= text
.begin();
34 QList
<QString
>::ConstIterator itrArgs
= args
.begin();
35 QString merged
= (*itrText
);
37 while ( itrText
!= text
.end() && itrArgs
!= args
.end() )
39 merged
+= (*itrArgs
) + (*itrText
);
44 Q_ASSERT( itrText
== text
.end() && itrArgs
== args
.end() );
49 // %something gets replaced by the value corresponding to key "something" in args
50 QString
namedArgs( const QMap
<QString
, QString
> args
, bool opt
=false ) const
52 QRegExp
rxArg( "%[a-zA-Z0-9]+" );
56 for( int pos
= rxArg
.indexIn( *this );
58 pos
= rxArg
.indexIn( *this, start
) )
60 int len
= rxArg
.matchedLength();
61 QString p
= rxArg
.capturedTexts()[0].mid(1, len
-1);
63 result
+= mid( start
, pos
-start
);
64 if( !args
[p
].isEmpty() )
71 result
+= mid( start
);
76 // %something gets replaced by the value corresponding to key "something" in args,
77 // however, if key "something" is not available,
78 // then replace everything within surrounding { } by an empty string
79 QString
namedOptArgs( const QMap
<QString
, QString
> args
) const
81 QRegExp
rxOptArg( "\\{.*%[a-zA-Z0-9_]+.*\\}" );
82 rxOptArg
.setMinimal( true );
86 for( int pos
= rxOptArg
.indexIn( *this );
88 pos
= rxOptArg
.indexIn( *this, start
) )
90 int len
= rxOptArg
.matchedLength();
91 QStringx opt
= rxOptArg
.capturedTexts()[0].mid(1, len
-2);
93 result
+= QStringx(mid( start
, pos
-start
)).namedArgs( args
);
94 result
+= opt
.namedArgs( args
, true );
98 result
+= QStringx( mid( start
) ).namedArgs( args
);
104 } // namespace Amarok
106 #endif // AMAROK_QSTRINGX_H