]> git.lyx.org Git - lyx.git/commitdiff
Avoid copying vector needlessly
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 2 Oct 2024 08:14:26 +0000 (10:14 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 3 Oct 2024 08:18:24 +0000 (10:18 +0200)
The getLabel method will not consider more than 10 citation keys.
Instead of removing elements from the keys vector, this commit adapts
the for loop to skip unwanted elements.

This allows to pass the keys vector by const reference.

Spotted by Coverity scan.

src/BiblioInfo.cpp
src/BiblioInfo.h

index 9458984f960ed895b05b5d3192cf90ca07cc11ef..15e5772068e86edff10e6a4d63adcb9f2fe95286 100644 (file)
@@ -1393,32 +1393,28 @@ docstring const BiblioInfo::getInfo(docstring const & key,
 }
 
 
-docstring const BiblioInfo::getLabel(vector<docstring> keys,
+docstring const BiblioInfo::getLabel(vector<docstring> const & keys,
        Buffer const & buf, string const & style, CiteItem const & ci) const
 {
        size_t max_size = ci.max_size;
        // shorter makes no sense
        LASSERT(max_size >= 16, max_size = 16);
 
-       // we can't display more than 10 of these, anyway
-       // but since we truncate in the middle,
-       // we need to split into two halfs.
-       bool const too_many_keys = keys.size() > 10;
-       vector<docstring> lkeys;
-       if (too_many_keys) {
-               lkeys.insert(lkeys.end(), keys.end() - 5, keys.end());
-               keys.resize(5);
-               keys.insert(keys.end(), lkeys.begin(), lkeys.end());
-       }
-
        CiteEngineType const engine_type = buf.params().citeEngineType();
        DocumentClass const & dc = buf.params().documentClass();
        docstring const & format = from_utf8(dc.getCiteFormat(engine_type, style, false, "cite"));
        docstring ret = format;
-       vector<docstring>::const_iterator key = keys.begin();
-       vector<docstring>::const_iterator ken = keys.end();
+       auto key = keys.begin();
+       auto const ken = keys.end();
        vector<docstring> handled_keys;
        for (int i = 0; key != ken; ++key, ++i) {
+               // we can't display more than 10 keys anyway, so keep 5 from
+               // the start and 5 from the end.
+               if (i == 5 && keys.size() > 10) {
+                       i = keys.size() - 5;
+                       key = ken - 5;
+               }
+
                handled_keys.push_back(*key);
                int n = 0;
                for (auto const & k : handled_keys) {
index 7b7a80e0581110adef983cb853a08739df714466..c42d3e03188263e8137ed894e9c2a8d667b883a3 100644 (file)
@@ -241,7 +241,7 @@ public:
                                bool const for_xhtml = false) const;
        /// \return formatted BibTeX data for citation labels.
        /// Citation labels can have more than one key.
-       docstring const getLabel(std::vector<docstring> keys, Buffer const & buf,
+       docstring const getLabel(std::vector<docstring> const & keys, Buffer const & buf,
                                 std::string const & style, CiteItem const & ci) const;
        /// Is this a reference from a bibtex database
        /// or from a bibliography environment?