commit e0da1d250c9be3488c7ab8ef3c7857451ba87130
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Mon Sep 21 12:58:32 2026 +0200

    Minor performance tweaks suggested by cppcheck
    
    Three kind of changes here:
    
        - pass const parameters by reference
        - use resize() or pop_back() instead of substr() in things like
            s = s.substr(0, n) ==> s.resize(n)
            s = s.substr(0, s.size() - 1) ==> s.pop_back()
        - in constructors, initalize members in initialzation list.
---
 src/BiblioInfo.cpp                    |  7 +++----
 src/Buffer.cpp                        |  2 +-
 src/BufferParams.cpp                  |  2 +-
 src/BufferView.cpp                    |  2 +-
 src/Format.cpp                        |  5 ++---
 src/Language.cpp                      |  2 +-
 src/Language.h                        |  2 +-
 src/ListingLanguage.cpp               |  2 +-
 src/ListingLanguage.h                 |  2 +-
 src/TexRow.cpp                        |  2 +-
 src/TexRow.h                          |  2 +-
 src/TextClass.cpp                     |  2 +-
 src/frontends/qt/ButtonController.cpp |  2 +-
 src/frontends/qt/ButtonController.h   |  2 +-
 src/frontends/qt/ColorsCombo.cpp      |  6 +++---
 src/frontends/qt/GuiPainter.cpp       | 10 +++-------
 src/frontends/qt/GuiView.cpp          |  2 +-
 src/frontends/qt/GuiView.h            |  2 +-
 src/frontends/qt/Menus.cpp            |  4 ++--
 src/insets/InsetCitation.cpp          | 24 +++++++++---------------
 src/insets/InsetGraphics.cpp          |  2 +-
 src/insets/InsetInclude.cpp           |  2 +-
 src/insets/InsetIndex.cpp             |  2 +-
 src/insets/InsetSpecialChar.cpp       |  2 +-
 src/lyxfind.cpp                       |  4 ++--
 src/mathed/InsetMathHull.cpp          |  2 +-
 src/mathed/MathParser.cpp             |  2 +-
 27 files changed, 44 insertions(+), 56 deletions(-)

diff --git a/src/BiblioInfo.cpp b/src/BiblioInfo.cpp
index 5976d41054..e01e43559b 100644
--- a/src/BiblioInfo.cpp
+++ b/src/BiblioInfo.cpp
@@ -1639,11 +1639,10 @@ CitationStyle citationStyleFromString(string const & 
command,
                cmd[0] = lowercase(cmd[0]);
        }
 
-       size_t const n = command.size() - 1;
-       if (command[n] == '*') {
+       if (command.back() == '*') {
                cs.hasStarredVersion = true;
-               if (suffixIs(cmd, '*'))
-                       cmd = cmd.substr(0, cmd.size() - 1);
+               if (cmd.back() == '*')
+                       cmd.pop_back();
        }
 
        cs.name = cmd;
diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index 5b339b4837..a580014118 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -2879,7 +2879,7 @@ bool Buffer::getStatus(FuncRequest const & cmd, 
FuncStatus & flag) const
                        params().getDefaultOutputFormat() : to_utf8(arg);
                size_t pos = format.find(' ');
                if (pos != string::npos)
-                       format = format.substr(0, pos);
+                       format.resize(pos);
                enable = params().isExportable(format, false);
                if (!enable)
                        flag.message(bformat(
diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp
index 8579c520df..5f05338cbe 100644
--- a/src/BufferParams.cpp
+++ b/src/BufferParams.cpp
@@ -2285,7 +2285,7 @@ namespace{
 /// This function checks whether the catcode of @
 /// is `other' (12) rather than `letter' (11)
 /// It also returns the context of the change
-bool isAtOther(docstring const passage, docstring & context)
+bool isAtOther(docstring const & passage, docstring & context)
 {
        // we assume `letter' catcode initially
        bool cc = false;
diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index b7c8ad52c4..e185b1a626 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -2488,7 +2488,7 @@ void BufferView::dispatch(FuncRequest const & cmd, 
DispatchResult & dr)
                                size_t const i = insname.rfind(':');
                                if (i == string::npos)
                                        break;
-                               insname = insname.substr(0, i);
+                               insname.resize(i);
                        }
                        // if we did not delete the inset, skip it
                        if (!bvcur.nextInset() || bvcur.nextInset() == ins)
diff --git a/src/Format.cpp b/src/Format.cpp
index e55242f650..3f8cf0867e 100644
--- a/src/Format.cpp
+++ b/src/Format.cpp
@@ -75,10 +75,9 @@ bool operator<(Format const & a, Format const & b)
 Format::Format(string const & n, string const & e, docstring const & p,
               string const & s, string const & v, string const & ed,
               string const & m, int flags)
-       : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
-         editor_(ed), mime_(m), flags_(flags)
+       : name_(n), extension_list_(getVectorFromString(e, ",")), 
prettyname_(p),
+         shortcut_(s), viewer_(v), editor_(ed), mime_(m), flags_(flags)
 {
-       extension_list_ = getVectorFromString(e, ",");
        LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
 }
 
diff --git a/src/Language.cpp b/src/Language.cpp
index 6571774a49..985e566335 100644
--- a/src/Language.cpp
+++ b/src/Language.cpp
@@ -343,7 +343,7 @@ bool Language::readLanguage(Lexer & lex)
 }
 
 
-bool Language::read(Lexer & lex, std::string const lang)
+bool Language::read(Lexer & lex, std::string const & lang)
 {
        lang_ = lang;
        encoding_ = nullptr;
diff --git a/src/Language.h b/src/Language.h
index c04bad8b89..88a7d6db75 100644
--- a/src/Language.h
+++ b/src/Language.h
@@ -126,7 +126,7 @@ public:
        /// Is this language supported by a specific package?
        bool supportedBy(std::string const & package) const;
        ///
-       bool read(support::Lexer & lex, std::string const lang);
+       bool read(support::Lexer & lex, std::string const & lang);
        ///
        bool readLanguage(support::Lexer & lex);
        ///
diff --git a/src/ListingLanguage.cpp b/src/ListingLanguage.cpp
index 62cf0eb813..96f65cf2f4 100644
--- a/src/ListingLanguage.cpp
+++ b/src/ListingLanguage.cpp
@@ -149,7 +149,7 @@ void ListingLanguage::readDialect(Lexer & lex)
 }
 
 
-bool ListingLanguage::read(Lexer & lex, std::string const lang)
+bool ListingLanguage::read(Lexer & lex, std::string const & lang)
 {
        lang_ = lang;
 
diff --git a/src/ListingLanguage.h b/src/ListingLanguage.h
index b67d9591fd..4b290efe8f 100644
--- a/src/ListingLanguage.h
+++ b/src/ListingLanguage.h
@@ -47,7 +47,7 @@ public:
        ///
        bool isSupported(std::string const & package) const;
        ///
-       bool read(support::Lexer & lex, std::string const lang);
+       bool read(support::Lexer & lex, std::string const & lang);
        ///
        bool readListingLanguage(support::Lexer & lex);
        ///
diff --git a/src/TexRow.cpp b/src/TexRow.cpp
index 63be1938f7..8d87ac55c6 100644
--- a/src/TexRow.cpp
+++ b/src/TexRow.cpp
@@ -388,7 +388,7 @@ TexRow::RowEntry 
TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
 
 
 //static
-bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2)
+bool TexRow::sameParOrInsetMath(RowEntry const & entry1, RowEntry const & 
entry2)
 {
        if (entry1.type != entry2.type)
                return false;
diff --git a/src/TexRow.h b/src/TexRow.h
index bb9ac825f3..833259e8ce 100644
--- a/src/TexRow.h
+++ b/src/TexRow.h
@@ -212,7 +212,7 @@ public:
 
 private:
        /// true iff same paragraph or math inset or begin_document
-       static bool sameParOrInsetMath(RowEntry entry1, RowEntry entry2);
+       static bool sameParOrInsetMath(RowEntry const & entry1, RowEntry const 
& entry2);
        /// computes the distance in pos or cell index
        /// assumes it is the sameParOrInsetMath
        static pos_type comparePos(RowEntry const & entry1, RowEntry const & 
entry2);
diff --git a/src/TextClass.cpp b/src/TextClass.cpp
index bb7bf8d754..3a872c1711 100644
--- a/src/TextClass.cpp
+++ b/src/TextClass.cpp
@@ -2125,7 +2125,7 @@ InsetLayout const & DocumentClass::insetLayout(docstring 
const & name) const
                size_t i = n.find(':');
                if (i == string::npos)
                        break;
-               n = n.substr(0, i);
+               n.resize(i);
        }
        // Layout "name" not found.
        return InsetLayout::undefined();
diff --git a/src/frontends/qt/ButtonController.cpp 
b/src/frontends/qt/ButtonController.cpp
index 5c649e19df..1124c5200f 100644
--- a/src/frontends/qt/ButtonController.cpp
+++ b/src/frontends/qt/ButtonController.cpp
@@ -284,7 +284,7 @@ void ButtonController::addCheckedLineEdit(QLineEdit * 
input, QWidget * target, i
 }
 
 
-void ButtonController::addCheckedLineEditPanel(QLineEdit * input, QWidget * 
target, QString const panel)
+void ButtonController::addCheckedLineEditPanel(QLineEdit * input, QWidget * 
target, QString const & panel)
 {
        d->checked_widgets_.append(CheckedLineEdit(input, target, -1, panel));
 }
diff --git a/src/frontends/qt/ButtonController.h 
b/src/frontends/qt/ButtonController.h
index fa694e6f33..ec1cb9c293 100644
--- a/src/frontends/qt/ButtonController.h
+++ b/src/frontends/qt/ButtonController.h
@@ -112,7 +112,7 @@ public:
        /** Add a widget to the list of all widgets whose validity should
         *  be checked explicitly when the buttons are refreshed.
         */
-       void addCheckedLineEditPanel(QLineEdit * input, QWidget * target, 
QString const panel);
+       void addCheckedLineEditPanel(QLineEdit * input, QWidget * target, 
QString const & panel);
 
 private:
        /// noncopyable
diff --git a/src/frontends/qt/ColorsCombo.cpp b/src/frontends/qt/ColorsCombo.cpp
index f322f06e2f..72f5194775 100644
--- a/src/frontends/qt/ColorsCombo.cpp
+++ b/src/frontends/qt/ColorsCombo.cpp
@@ -104,7 +104,7 @@ struct ColorsCombo::Private
        ///
        void countCategories();
        ///
-       void toggleRows(QString const data, bool const toggle);
+       void toggleRows(QString const & data, bool const toggle);
        ///
        ColorsCombo * p;
 
@@ -203,7 +203,7 @@ void ColItemDelegate::drawDisplay(QPainter * painter, 
QStyleOptionViewItem const
        fmt.setMargin(0);
        if (cc_->left_margin_ != -1)
                fmt.setLeftMargin(cc_->left_margin_);
-               
+
        doc.rootFrame()->setFrameFormat(fmt);
 
        painter->translate(opt.rect.x() + 5,
@@ -331,7 +331,7 @@ void ColorsCombo::Private::setFilter(QString const & s)
 }
 
 
-void ColorsCombo::Private::toggleRows(QString const data, bool const toggle)
+void ColorsCombo::Private::toggleRows(QString const & data, bool const toggle)
 {
        QList<QStandardItem *> cis = model_->findItems(data, Qt::MatchExactly, 
1);
        if (cis.empty())
diff --git a/src/frontends/qt/GuiPainter.cpp b/src/frontends/qt/GuiPainter.cpp
index 600bcfa20f..27e46ff6fe 100644
--- a/src/frontends/qt/GuiPainter.cpp
+++ b/src/frontends/qt/GuiPainter.cpp
@@ -41,13 +41,9 @@ namespace frontend {
 const int Painter::thin_line = 1;
 
 GuiPainter::GuiPainter(QPaintDevice * device, double pixel_ratio, bool 
devel_mode)
-       : QPainter(device), Painter(pixel_ratio, devel_mode)
-{
-       // set cache correctly
-       current_color_ = pen().color();
-       current_ls_ = pen().style() == Qt::DotLine ? line_onoffdash : 
line_solid;
-       current_lw_ = pen().width();
-}
+       : QPainter(device), Painter(pixel_ratio, devel_mode), 
current_color_(pen().color()),
+         current_ls_(pen().style() == Qt::DotLine ? line_onoffdash : 
line_solid),
+         current_lw_(pen().width()) {}
 
 
 GuiPainter::~GuiPainter()
diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp
index 40a5744947..4f15510200 100644
--- a/src/frontends/qt/GuiView.cpp
+++ b/src/frontends/qt/GuiView.cpp
@@ -1219,7 +1219,7 @@ TocModels & GuiView::tocModels()
 
 
 void GuiView::addColorItem(QString const & item, QString const & guiname,
-                          QString const & category, QString const color,
+                          QString const & category, QString const & color,
                           bool const custom, int const r) const
 {
        QList<QStandardItem *> row;
diff --git a/src/frontends/qt/GuiView.h b/src/frontends/qt/GuiView.h
index a82447d831..33fa273318 100644
--- a/src/frontends/qt/GuiView.h
+++ b/src/frontends/qt/GuiView.h
@@ -197,7 +197,7 @@ public:
        ///
        void addColorItem(QString const & item, QString const & guiname,
                          QString const & category = QString(),
-                         QString const color = QString(),
+                         QString const & color = QString(),
                          bool custom = false, int row = -1) const;
        ///
        QStandardItemModel * viewColorsModel();
diff --git a/src/frontends/qt/Menus.cpp b/src/frontends/qt/Menus.cpp
index 8e62a0bc75..e8f4cc0902 100644
--- a/src/frontends/qt/Menus.cpp
+++ b/src/frontends/qt/Menus.cpp
@@ -372,7 +372,7 @@ public:
        /// Check whether the shortcut of \p mi are unique and valid, and 
report if not
        void checkShortcutUnique(MenuItem const & mi) const;
        /// Return true if a \p sc is a unique shortcut
-       bool checkShortcut(QString const sc) const;
+       bool checkShortcut(QString const & sc) const;
        /// Try to find a unique shortcut from a string of alternatives
        QString getBestShortcut(MenuItem const & mi) const;
        ///
@@ -892,7 +892,7 @@ void MenuDefinition::checkShortcutUnique(MenuItem const & 
mi) const
 }
 
 
-bool MenuDefinition::checkShortcut(QString const shortcut) const
+bool MenuDefinition::checkShortcut(QString const & shortcut) const
 {
        if (shortcut.isEmpty())
                return true;
diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp
index fc65cce3db..81cf4de90e 100644
--- a/src/insets/InsetCitation.cpp
+++ b/src/insets/InsetCitation.cpp
@@ -108,25 +108,20 @@ CitationStyle 
InsetCitation::getCitationStyle(BufferParams const & bp, string co
        cs.hasStarredVersion = false;
 
        string normalized_input = input;
-       string::size_type const n = input.size() - 1;
        if (isUpperCase(input[0]))
                normalized_input[0] = lowercase(input[0]);
-       if (input[n] == '*')
-               normalized_input = normalized_input.substr(0, n);
+       if (input.back() == '*')
+               normalized_input.pop_back();
 
        string const alias = bp.getCiteAlias(normalized_input);
        if (!alias.empty())
                normalized_input = alias;
 
-       vector<CitationStyle>::const_iterator it  = valid_styles.begin();
-       vector<CitationStyle>::const_iterator end = valid_styles.end();
-       for (; it != end; ++it) {
-               CitationStyle this_cs = *it;
+       for (auto const & this_cs : valid_styles)
                if (this_cs.name == normalized_input) {
-                       cs = *it;
+                       cs = this_cs;
                        break;
                }
-       }
 
        return cs;
 }
@@ -362,11 +357,10 @@ CitationStyle asValidLatexCommand(BufferParams const & 
bp, string const & input,
        cs.hasStarredVersion = false;
 
        string normalized_input = input;
-       string::size_type const n = input.size() - 1;
        if (isUpperCase(input[0]))
                normalized_input[0] = lowercase(input[0]);
-       if (input[n] == '*')
-               normalized_input = normalized_input.substr(0, n);
+       if (input.back() == '*')
+               normalized_input.pop_back();
 
        string const alias = bp.getCiteAlias(normalized_input);
        if (!alias.empty())
@@ -391,7 +385,7 @@ CitationStyle asValidLatexCommand(BufferParams const & bp, 
string const & input,
        }
 
        cs.forceUpperCase &= input[0] == uppercase(input[0]);
-       cs.hasStarredVersion &= input[n] == '*';
+       cs.hasStarredVersion &= input.back() == '*';
 
        return cs;
 }
@@ -477,9 +471,9 @@ docstring InsetCitation::complexLabel(bool for_xhtml) const
        bool const uppercase = isUpperCase(cite_type[0]);
        if (uppercase)
                cite_type[0] = lowercase(cite_type[0]);
-       bool const starred = (cite_type[cite_type.size() - 1] == '*');
+       bool const starred = cite_type.back() == '*';
        if (starred)
-               cite_type = cite_type.substr(0, cite_type.size() - 1);
+               cite_type.pop_back();
 
        // handle alias
        string const alias = buf.masterParams().getCiteAlias(cite_type);
diff --git a/src/insets/InsetGraphics.cpp b/src/insets/InsetGraphics.cpp
index c087533905..311739cc37 100644
--- a/src/insets/InsetGraphics.cpp
+++ b/src/insets/InsetGraphics.cpp
@@ -441,7 +441,7 @@ string InsetGraphics::createLatexOptions(bool const ps) 
const
        string opts = options.str();
        // delete last ','
        if (suffixIs(opts, ','))
-               opts = opts.substr(0, opts.size() - 1);
+               opts.pop_back();
 
        return opts;
 }
diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp
index 2defd467bd..ed57134cd6 100644
--- a/src/insets/InsetInclude.cpp
+++ b/src/insets/InsetInclude.cpp
@@ -178,7 +178,7 @@ docstring stripOuterBraces(docstring & str)
        if (prefixIs(str, from_ascii("{")))
                str = str.substr(1, docstring::npos);
        if (suffixIs(str, from_ascii("}")))
-               str = str.substr(0, str.size() - 1);
+               str.pop_back();
        return str;
 }
 
diff --git a/src/insets/InsetIndex.cpp b/src/insets/InsetIndex.cpp
index 4ccd0058eb..d8997c310e 100644
--- a/src/insets/InsetIndex.cpp
+++ b/src/insets/InsetIndex.cpp
@@ -928,7 +928,7 @@ std::vector<docstring> 
InsetIndex::getSeeAlsoesAsText(OutputParams const & runpa
 namespace {
 
 bool hasInsetWithCode(const InsetIndex * const inset_index, const InsetCode 
code,
-                                         const 
std::set<InsetIndexMacroParams::Type> types = {})
+                                         std::set<InsetIndexMacroParams::Type> 
const & types = {})
 {
        Paragraph const & par = inset_index->paragraphs().front();
        InsetList::const_iterator it = par.insetList().begin();
diff --git a/src/insets/InsetSpecialChar.cpp b/src/insets/InsetSpecialChar.cpp
index 41a5049f50..530eb8044d 100644
--- a/src/insets/InsetSpecialChar.cpp
+++ b/src/insets/InsetSpecialChar.cpp
@@ -85,7 +85,7 @@ void drawChar(PainterInfo & pi, int & x, int const y, 
char_type ch)
 }
 
 
-void drawLogo(PainterInfo & pi, int & x, int const y, string const kind)
+void drawLogo(PainterInfo & pi, int & x, int const y, string const & kind)
 {
        FontInfo const & font = pi.base.font;
        int const em = theFontMetrics(font).em();
diff --git a/src/lyxfind.cpp b/src/lyxfind.cpp
index 485b81e4d2..a7481f4570 100644
--- a/src/lyxfind.cpp
+++ b/src/lyxfind.cpp
@@ -1644,7 +1644,7 @@ void Intervall::addIntervall(int low, int upper)
        }
 }
 
-static void buildaccent(string n, string param, string const & values)
+static void buildaccent(string const & n, string const & param, string const & 
values)
 {
        stringstream s(n);
        string name;
@@ -4041,7 +4041,7 @@ MatchResult MatchStringAdv::findAux(DocIterator const & 
cur, int len, MatchStrin
                while ((lng > 1) && ((str[lng -1] == '}') || (str[lng -1] == 
'\n')))
                        lng--;
                if (lng != str.size()) {
-                       str = str.substr(0, lng);
+                       str.resize(lng);
                }
                // Replace occurences of '~' to ' '
                static std::regex specialChars { R"(~)" };
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index 2ca494230f..25fdcd8682 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -383,7 +383,7 @@ void InsetMathHull::addToToc(DocIterator const & pit, bool 
output_active,
                                wi << cell(index(row, c));
                                d = ods.str();
                                if (d.size() > max_length) {
-                                       d = d.substr(0, max_length - 1);
+                                       d.resize(max_length - 1);
                                        break;
                                }
                        }
diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp
index 865f0ad361..383615cb93 100644
--- a/src/mathed/MathParser.cpp
+++ b/src/mathed/MathParser.cpp
@@ -609,7 +609,7 @@ void Parser::tokenize(istream & is)
        while (is.get(c)) {
                s += c;
                if (s.size() >= 10 && s.substr(s.size() - 10) == "\\end_inset") 
{
-                       s = s.substr(0, s.size() - 10);
+                       s.resize(s.size() - 10);
                        break;
                }
        }
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to