Permalink
Browse files

Make startsWith/endsWith more efficient.

Krazy suggests that explicitly casting to QLatin1String (or using single
character constants instead of string constants) can give a performance
boost since it avoids the (relatively expensive) implicit conversion.
  • Loading branch information...
atomopawn authored and mattr- committed Aug 6, 2011
1 parent 96392d1 commit aef0c801061ba6f8c13576a962d160ba4634c112
@@ -82,7 +82,7 @@ bool BasketThumbCreator::create(const QString &path, int /*width*/, int /*height
file.close();
return true;
}
- } else if (key.endsWith("*")) {
+ } else if (key.endsWith('*')) {
// We do not know what it is, but we should read the embedded-file in order to discard it:
bool ok;
ulong size = value.toULong(&ok);
View
@@ -387,7 +387,7 @@ void Archive::open(const QString &path)
stream.seek(file.pos());
}
- } else if (key.endsWith("*")) {
+ } else if (key.endsWith('*')) {
// We do not know what it is, but we should read the embedded-file in order to discard it:
bool ok;
qint64 size = value.toULong(&ok);
View
@@ -356,7 +356,7 @@ void Backup::setFolderAndRestart(const QString &folder, const QString &message)
KMessageBox::information(
0,
"<qt>" + message.arg(
- (folder.endsWith("/") ? folder.left(folder.length() - 1) : folder),
+ (folder.endsWith('/') ? folder.left(folder.length() - 1) : folder),
KGlobal::mainComponent().aboutData()->programName()),
i18n("Restart")
);
View
@@ -1184,8 +1184,8 @@ BasketView::BasketView(QWidget *parent, const QString &folderName)
// global one)
m_action->setShortcutConfigurable(false);
- if (!m_folderName.endsWith("/"))
- m_folderName += "/";
+ if (!m_folderName.endsWith('/'))
+ m_folderName += '/';
setFocusPolicy(Qt::StrongFocus);
setDragAutoScroll(true);
@@ -2171,7 +2171,7 @@ void BasketView::contentsMouseReleaseEvent(QMouseEvent *event)
} else if (link == "basket-internal-import") {
KMenu *menu = Global::bnpView->popupMenu("fileimport");
menu->exec(event->globalPos());
- } else if (link.startsWith("basket://")) {
+ } else if (link.startsWith(QLatin1String("basket://"))) {
emit crossReference(link);
} else {
KRun *run = new KRun(KUrl(link), window()); // open the URL.
@@ -4120,7 +4120,7 @@ void BasketView::noteOpen(Note *note)
// Finally do the opening job:
QString customCommand = note->content()->customOpenCommand();
- if (url.url().startsWith("basket://")) {
+ if (url.url().startsWith(QLatin1String("basket://"))) {
emit crossReference(url.url());
} else if (customCommand.isEmpty()) {
KRun *run = new KRun(url, window());
@@ -5090,7 +5090,7 @@ bool BasketView::isFileEncrypted()
if (file.open(QIODevice::ReadOnly)) {
// Should be ASCII anyways
QString line = file.readLine(32);
- if (line.startsWith("-----BEGIN PGP MESSAGE-----"))
+ if (line.startsWith(QLatin1String("-----BEGIN PGP MESSAGE-----")))
return true;
}
return false;
View
@@ -2143,8 +2143,8 @@ BasketView* BNPView::basketForFolderName(const QString &folderName)
*/
QString name = folderName;
- if (!name.endsWith("/"))
- name += "/";
+ if (!name.endsWith('/'))
+ name += '/';
QTreeWidgetItemIterator it(m_tree);
while (*it) {
View
@@ -132,14 +132,14 @@ void FormatImporter::importBaskets()
// Move the folder to the new repository (normal basket) or copy the folder (mirorred folder):
QString folderName = *it;
- if (folderName.startsWith("/")) { // It was a folder mirror:
+ if (folderName.startsWith('/')) { // It was a folder mirror:
KMessageBox::information(0, i18n("<p>Folder mirroring is not possible anymore (see <a href='http://basket.kde.org/'>basket.kde.org</a> for more information).</p>"
"<p>The folder <b>%1</b> has been copied for the basket needs. You can either delete this folder or delete the basket, or use both. But remember that "
"modifying one will not modify the other anymore as they are now separate entities.</p>", folderName), i18n("Folder Mirror Import"),
"", KMessageBox::AllowLink);
// Also modify folderName to be only the folder name and not the full path anymore:
QString newFolderName = folderName;
- if (newFolderName.endsWith("/"))
+ if (newFolderName.endsWith('/'))
newFolderName = newFolderName.left(newFolderName.length() - 1);
newFolderName = newFolderName.mid(newFolderName.lastIndexOf('/') + 1);
newFolderName = Tools::fileNameForNewFile(newFolderName, Global::basketsFolder());
View
@@ -378,10 +378,10 @@ void KGpgMe::setPassphraseCb()
if (m_useGnuPGAgent) {
if (agent_info.indexOf(':'))
agent = true;
- if (agent_info.startsWith("disable:"))
+ if (agent_info.startsWith(QLatin1String("disable:")))
setenv("GPG_AGENT_INFO", agent_info.mid(8).toAscii(), 1);
} else {
- if (!agent_info.startsWith("disable:"))
+ if (!agent_info.startsWith(QLatin1String("disable:")))
setenv("GPG_AGENT_INFO", "disable:" + agent_info.toAscii(), 1);
}
if (agent)
View
@@ -733,7 +733,7 @@ QString LikeBackDialog::introductionText()
languagesMessage = "";
}
} else {
- if (!KGlobal::locale()->language().startsWith("en"))
+ if (!KGlobal::locale()->language().startsWith(QLatin1String("en")))
languagesMessage = i18n("Please write in English.");
}
View
@@ -742,7 +742,7 @@ Note::Zone Note::zoneAt(const QPoint &pos, bool toAdd)
QString Note::linkAt(const QPoint &pos)
{
QString link = m_content->linkAt(pos - QPoint(contentX(), NOTE_MARGIN));
- if (link.isEmpty() || link.startsWith("basket://"))
+ if (link.isEmpty() || link.startsWith(QLatin1String("basket://")))
return link;
else
return NoteFactory::filteredURL(KUrl(link)).prettyUrl();//KURIFilter::self()->filteredURI(link);
View
@@ -2013,7 +2013,7 @@ void CrossReferenceContent::exportToHTML(HTMLExporter *exporter, int /*indent*/)
QString url = m_url.url();
QString title;
- if(url.startsWith("basket://"))
+ if(url.startsWith(QLatin1String("basket://")))
url = url.mid(9, url.length() -9);
if(url.endsWith('/'))
url = url.left(url.length() - 1);
View
@@ -189,21 +189,21 @@ QStringList NoteFactory::textToURLList(const QString &text)
}
/* Search for an url and create an URL note */
- if ((ltext.startsWith("/") && ltext[1] != '/' && ltext[1] != '*') || // Take files but not C/C++/... comments !
- ltext.startsWith("file:") ||
- ltext.startsWith("http://") ||
- ltext.startsWith("https://") ||
- ltext.startsWith("www.") ||
- ltext.startsWith("ftp.") ||
- ltext.startsWith("ftp://") ||
- ltext.startsWith("mailto:")) {
+ if ((ltext.startsWith('/') && ltext[1] != '/' && ltext[1] != '*') || // Take files but not C/C++/... comments !
+ ltext.startsWith(QLatin1String("file:")) ||
+ ltext.startsWith(QLatin1String("http://")) ||
+ ltext.startsWith(QLatin1String("https://")) ||
+ ltext.startsWith(QLatin1String("www.")) ||
+ ltext.startsWith(QLatin1String("ftp.")) ||
+ ltext.startsWith(QLatin1String("ftp://")) ||
+ ltext.startsWith(QLatin1String("mailto:"))) {
// First, correct the text to use the good format for the url
- if (ltext.startsWith("/"))
+ if (ltext.startsWith('/'))
(*it).insert(0, "file:");
- if (ltext.startsWith("www."))
+ if (ltext.startsWith(QLatin1String("www.")))
(*it).insert(0, "http://");
- if (ltext.startsWith("ftp."))
+ if (ltext.startsWith(QLatin1String("ftp.")))
(*it).insert(0, "ftp://");
// And create the Url note (or launcher if URL point a .desktop file)
@@ -311,7 +311,7 @@ Note* NoteFactory::createNoteLinkOrLauncher(const KUrl &url, BasketView *parent)
// "Invalid entry (missing '=') at /my/file.ogg:11984"
// "Invalid entry (missing ']') at /my/file.ogg:11984"...
KService::Ptr service;
- if (url.fileName().endsWith(".desktop"))
+ if (url.fileName().endsWith(QLatin1String(".desktop")))
service = new KService(url.path());
// If link point to a .desktop file then add a launcher, otherwise it's a link
@@ -678,13 +678,13 @@ Note* NoteFactory::decodeContent(QDataStream &stream, NoteType::Id type, BasketV
bool NoteFactory::maybeText(const KUrl &url)
{
QString path = url.url().toLower();
- return path.endsWith(".txt");
+ return path.endsWith(QLatin1String(".txt"));
}
bool NoteFactory::maybeHtml(const KUrl &url)
{
QString path = url.url().toLower();
- return path.endsWith(".html") || path.endsWith(".htm");
+ return path.endsWith(QLatin1String(".html")) || path.endsWith(QLatin1String(".htm"));
}
bool NoteFactory::maybeImageOrAnimation(const KUrl &url)
@@ -714,19 +714,19 @@ bool NoteFactory::maybeImageOrAnimation(const KUrl &url)
bool NoteFactory::maybeAnimation(const KUrl &url)
{
QString path = url.url().toLower();
- return path.endsWith(".mng") || path.endsWith(".gif");
+ return path.endsWith(QLatin1String(".mng")) || path.endsWith(QLatin1String(".gif"));
}
bool NoteFactory::maybeSound(const KUrl &url)
{
QString path = url.url().toLower();
- return path.endsWith(".mp3") || path.endsWith(".ogg");
+ return path.endsWith(QLatin1String(".mp3")) || path.endsWith(QLatin1String(".ogg"));
}
bool NoteFactory::maybeLauncher(const KUrl &url)
{
QString path = url.url().toLower();
- return path.endsWith(".desktop");
+ return path.endsWith(QLatin1String(".desktop"));
}
////////////// NEW:
@@ -837,12 +837,12 @@ NoteType::Id NoteFactory::typeForURL(const KUrl &url, BasketView */*parent*/)
*Global::debugWindow << "typeForURL: " + url.prettyUrl() + " ; MIME type = " + mimeType;
if (mimeType == "application/x-desktop") return NoteType::Launcher;
- else if (viewText && mimeType.startsWith("text/plain")) return NoteType::Text;
- else if (viewHTML && mimeType.startsWith("text/html")) return NoteType::Html;
+ else if (viewText && mimeType.startsWith(QLatin1String("text/plain"))) return NoteType::Text;
+ else if (viewHTML && mimeType.startsWith(QLatin1String("text/html"))) return NoteType::Html;
else if (viewImage && mimeType == "movie/x-mng") return NoteType::Animation;
else if (viewImage && mimeType == "image/gif") return NoteType::Animation;
- else if (viewImage && mimeType.startsWith("image/")) return NoteType::Image;
- else if (viewSound && mimeType.startsWith("audio/")) return NoteType::Sound;
+ else if (viewImage && mimeType.startsWith(QLatin1String("image/"))) return NoteType::Image;
+ else if (viewSound && mimeType.startsWith(QLatin1String("audio/"))) return NoteType::Sound;
else return NoteType::File;
}
@@ -915,41 +915,41 @@ QString NoteFactory::titleForURL(const KUrl &url)
QString title = url.prettyUrl();
QString home = "file:" + QDir::homePath() + "/";
- if (title.startsWith("mailto:"))
+ if (title.startsWith(QLatin1String("mailto:")))
return title.remove(0, 7);
if (title.startsWith(home))
title = "~/" + title.remove(0, home.length());
- if (title.startsWith("file://"))
+ if (title.startsWith(QLatin1String("file://")))
title = title.remove(0, 7); // 7 == QString("file://").length() - 1
- else if (title.startsWith("file:"))
+ else if (title.startsWith(QLatin1String("file:")))
title = title.remove(0, 5); // 5 == QString("file:").length() - 1
- else if (title.startsWith("http://www."))
+ else if (title.startsWith(QLatin1String("http://www.")))
title = title.remove(0, 11); // 11 == QString("http://www.").length() - 1
- else if (title.startsWith("http://"))
+ else if (title.startsWith(QLatin1String("http://")))
title = title.remove(0, 7); // 7 == QString("http://").length() - 1
if (! url.isLocalFile()) {
- if (title.endsWith("/index.html") && title.length() > 11)
+ if (title.endsWith(QLatin1String("/index.html")) && title.length() > 11)
title.truncate(title.length() - 11); // 11 == QString("/index.html").length()
- else if (title.endsWith("/index.htm") && title.length() > 10)
+ else if (title.endsWith(QLatin1String("/index.htm")) && title.length() > 10)
title.truncate(title.length() - 10); // 10 == QString("/index.htm").length()
- else if (title.endsWith("/index.xhtml") && title.length() > 12)
+ else if (title.endsWith(QLatin1String("/index.xhtml")) && title.length() > 12)
title.truncate(title.length() - 12); // 12 == QString("/index.xhtml").length()
- else if (title.endsWith("/index.php") && title.length() > 10)
+ else if (title.endsWith(QLatin1String("/index.php")) && title.length() > 10)
title.truncate(title.length() - 10); // 10 == QString("/index.php").length()
- else if (title.endsWith("/index.asp") && title.length() > 10)
+ else if (title.endsWith(QLatin1String("/index.asp")) && title.length() > 10)
title.truncate(title.length() - 10); // 10 == QString("/index.asp").length()
- else if (title.endsWith("/index.php3") && title.length() > 11)
+ else if (title.endsWith(QLatin1String("/index.php3")) && title.length() > 11)
title.truncate(title.length() - 11); // 11 == QString("/index.php3").length()
- else if (title.endsWith("/index.php4") && title.length() > 11)
+ else if (title.endsWith(QLatin1String("/index.php4")) && title.length() > 11)
title.truncate(title.length() - 11); // 11 == QString("/index.php4").length()
- else if (title.endsWith("/index.php5") && title.length() > 11)
+ else if (title.endsWith(QLatin1String("/index.php5")) && title.length() > 11)
title.truncate(title.length() - 11); // 11 == QString("/index.php5").length()
}
- if (title.length() > 2 && title.endsWith("/")) // length > 2 because "/" and "~/" shouldn't be transformed to "" and "~"
+ if (title.length() > 2 && title.endsWith('/')) // length > 2 because "/" and "~/" shouldn't be transformed to "" and "~"
title.truncate(title.length() - 1); // eg. transform "www.kde.org/" to "www.kde.org"
return title;
@@ -1038,7 +1038,7 @@ Note* NoteFactory::importKMenuLauncher(BasketView *parent)
// * locateLocal() return a local file even if it is a system wide one (local one doesn't exists)
// * desktopEntryPath() returns the full path for system wide ressources, but relative path if in home
QString serviceUrl = dialog->service()->entryPath();
- if (! serviceUrl.startsWith("/"))
+ if (! serviceUrl.startsWith('/'))
serviceUrl = dialog->service()->locateLocal(); //locateLocal("xdgdata-apps", serviceUrl);
return createNoteLauncher(serviceUrl, parent);
}
View
@@ -327,7 +327,7 @@ void SoftwareImporters::importKJots()
finishImport(basket);
// IT IS A NOTEBOOK XML FILE, AT THE VERION 0.7.0 and later:
- } else if ((*it).endsWith(".book") /*&& !buf.isNull() && (buf.left(2) == "<!" / *<!DOCTYPE...* / || buf.left(2) == "<?" / *<?xml...* /)*/) {
+ } else if ((*it).endsWith(QLatin1String(".book")) /*&& !buf.isNull() && (buf.left(2) == "<!" / *<!DOCTYPE...* / || buf.left(2) == "<?" / *<?xml...* /)*/) {
QDomDocument *doc = XMLWork::openFile("KJots", dirPath + *it);
if (doc == 0)
@@ -362,7 +362,7 @@ void SoftwareImporters::importKNotes()
QStringList list = dir.entryList();
for (QStringList::Iterator it = list.begin(); it != list.end(); ++it) { // For each file
- if (!(*it).endsWith(".ics")) // Don't process *.ics~ and otehr files
+ if (!(*it).endsWith(QLatin1String(".ics"))) // Don't process *.ics~ and otehr files
continue;
QFile file(dirPath + *it);
if (file.open(QIODevice::ReadOnly)) {
@@ -386,14 +386,14 @@ void SoftwareImporters::importKNotes()
if (!buf.isNull() && buf == "BEGIN:VJOURNAL") {
inVJournal = true;
- } else if (inVJournal && buf.startsWith("SUMMARY:")) {
+ } else if (inVJournal && buf.startsWith(QLatin1String("SUMMARY:"))) {
title = buf.mid(8, buf.length());
- } else if (inVJournal && buf.startsWith("DESCRIPTION:")) {
+ } else if (inVJournal && buf.startsWith(QLatin1String("DESCRIPTION:"))) {
body = buf.mid(12, buf.length());
inDescription = true;
- } else if (inDescription && buf.startsWith(" ")) {
+ } else if (inDescription && buf.startsWith(QLatin1String(" "))) {
body += buf.mid(1, buf.length());
- } else if (buf.startsWith("X-KDE-KNotes-RichText:")) {
+ } else if (buf.startsWith(QLatin1String("X-KDE-KNotes-RichText:"))) {
isRichText = XMLWork::trueOrFalse(buf.mid(22, buf.length() - 22).trimmed(), "false");
} else if (buf == "END:VJOURNAL") {
insertTitledNote(basket, fromICS(title), fromICS(body), (isRichText ? Qt::RichText : Qt::PlainText));
@@ -483,7 +483,7 @@ void SoftwareImporters::importTomboy()
QStringList list = dir.entryList();
for (QStringList::Iterator it = list.begin(); it != list.end(); ++it) { // For each file
- if (!(*it).endsWith(".note"))
+ if (!(*it).endsWith(QLatin1String(".note")))
continue;
QDomDocument *doc = XMLWork::openFile("note", dirPath + *it);
if (doc == 0)
@@ -654,8 +654,8 @@ void SoftwareImporters::importKnowIt()
while (1) {
line = stream.readLine();
- if (line.startsWith("\\NewEntry") ||
- line.startsWith("\\CurrentEntry") || stream.atEnd()) {
+ if (line.startsWith(QLatin1String("\\NewEntry")) ||
+ line.startsWith(QLatin1String("\\CurrentEntry")) || stream.atEnd()) {
while (level + 1 < baskets.size() - baskets.count(0))
baskets.pop();
if (level + 1 > baskets.size() - baskets.count(0))
@@ -701,9 +701,9 @@ void SoftwareImporters::importKnowIt()
text = "";
links.clear();
descriptions.clear();
- } else if (line.startsWith("\\Link")) {
+ } else if (line.startsWith(QLatin1String("\\Link"))) {
links.append(line.mid(6));
- } else if (line.startsWith("\\Descr")) {
+ } else if (line.startsWith(QLatin1String("\\Descr"))) {
while (descriptions.count() < links.count() - 1)
descriptions.append("");
descriptions.append(line.mid(7));
View
@@ -404,7 +404,7 @@ Tag* Tag::tagSimilarTo(Tag *tagToTest)
for (State::List::iterator it2 = (*it)->states().begin(); it2 != (*it)->states().end(); ++it2, ++itTest) {
State *state = *it2;
State *stateToTest = *itTest;
- if (state->id().startsWith("tag_state_") || stateToTest->id().startsWith("tag_state_")) {
+ if (state->id().startsWith(QLatin1String("tag_state_")) || stateToTest->id().startsWith(QLatin1String("tag_state_"))) {
defaultTag = false;
}
if (state->name() != stateToTest->name()) {
Oops, something went wrong.

0 comments on commit aef0c80

Please sign in to comment.