From: Jean-Marc Lasgouttes Date: Wed, 2 Oct 2024 08:14:26 +0000 (+0200) Subject: Avoid copying vector needlessly X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=69d8435a7f373994025ae23e796c6c1a73295404;p=lyx.git Avoid copying vector needlessly 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. --- diff --git a/src/BiblioInfo.cpp b/src/BiblioInfo.cpp index 9458984f96..15e5772068 100644 --- a/src/BiblioInfo.cpp +++ b/src/BiblioInfo.cpp @@ -1393,32 +1393,28 @@ docstring const BiblioInfo::getInfo(docstring const & key, } -docstring const BiblioInfo::getLabel(vector keys, +docstring const BiblioInfo::getLabel(vector 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 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::const_iterator key = keys.begin(); - vector::const_iterator ken = keys.end(); + auto key = keys.begin(); + auto const ken = keys.end(); vector 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) { diff --git a/src/BiblioInfo.h b/src/BiblioInfo.h index 7b7a80e058..c42d3e0318 100644 --- a/src/BiblioInfo.h +++ b/src/BiblioInfo.h @@ -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 keys, Buffer const & buf, + docstring const getLabel(std::vector 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?