From: Richard Kimberly Heck Date: Mon, 16 Mar 2020 05:08:03 +0000 (-0400) Subject: Use ranges and fix nullptr warnings. X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=2ed021e1bb74a4c459921986c61865a86fcb4800;p=features.git Use ranges and fix nullptr warnings. --- diff --git a/src/Text.cpp b/src/Text.cpp index dc296384b2..1087330482 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -190,10 +190,8 @@ Text::Text(InsetText * owner, bool use_default_layout) Text::Text(InsetText * owner, Text const & text) : owner_(owner), pars_(text.pars_) { - ParagraphList::iterator const end = pars_.end(); - ParagraphList::iterator it = pars_.begin(); - for (; it != end; ++it) - it->setInsetOwner(owner); + for (auto & p : pars_) + p.setInsetOwner(owner); } @@ -870,10 +868,9 @@ void Text::insertStringAsLines(Cursor & cur, docstring const & str, // insert the string, don't insert doublespace bool space_inserted = true; - for (docstring::const_iterator cit = str.begin(); - cit != str.end(); ++cit) { + for (auto const & ch : str) { Paragraph & par = pars_[pit]; - if (*cit == '\n') { + if (ch == '\n') { if (inset().allowMultiPar() && (!par.empty() || par.allowEmpty())) { lyx::breakParagraph(*this, pit, pos, par.layout().isEnvironment()); @@ -884,28 +881,28 @@ void Text::insertStringAsLines(Cursor & cur, docstring const & str, continue; } // do not insert consecutive spaces if !free_spacing - } else if ((*cit == ' ' || *cit == '\t') && + } else if ((ch == ' ' || ch == '\t') && space_inserted && !par.isFreeSpacing()) { continue; - } else if (*cit == '\t') { + } else if (ch == '\t') { if (!par.isFreeSpacing()) { // tabs are like spaces here par.insertChar(pos, ' ', font, bparams.track_changes); ++pos; space_inserted = true; } else { - par.insertChar(pos, *cit, font, bparams.track_changes); + par.insertChar(pos, ch, font, bparams.track_changes); ++pos; space_inserted = true; } - } else if (!isPrintable(*cit)) { + } else if (!isPrintable(ch)) { // Ignore unprintables continue; } else { // just insert the character - par.insertChar(pos, *cit, font, bparams.track_changes); + par.insertChar(pos, ch, font, bparams.track_changes); ++pos; - space_inserted = (*cit == ' '); + space_inserted = (ch == ' '); } } setCursor(cur, pit, pos); @@ -1820,10 +1817,8 @@ bool Text::dissolveInset(Cursor & cur) // ERT paragraphs have the Language latex_language. // This is invalid outside of ERT, so we need to // change it to the buffer language. - ParagraphList::iterator it = plist.begin(); - ParagraphList::iterator it_end = plist.end(); - for (; it != it_end; ++it) - it->changeLanguage(b.params(), latex_language, b.language()); + for (auto & p : plist) + p.changeLanguage(b.params(), latex_language, b.language()); /* If the inset is the only thing in paragraph and the layout * is not plain, then the layout of the first paragraph of diff --git a/src/Text2.cpp b/src/Text2.cpp index 07839a748d..6adbb51553 100644 --- a/src/Text2.cpp +++ b/src/Text2.cpp @@ -603,7 +603,7 @@ bool Text::checkAndActivateInsetVisual(Cursor & cur, bool movingForward, bool mo if (cur.pos() == cur.lastpos()) return false; Paragraph & par = cur.paragraph(); - Inset * inset = par.isInset(cur.pos()) ? par.getInset(cur.pos()) : 0; + Inset * inset = par.isInset(cur.pos()) ? par.getInset(cur.pos()) : nullptr; if (!inset || !inset->editable()) return false; if (cur.selection() && cur.realAnchor().find(inset) == -1) diff --git a/src/Text3.cpp b/src/Text3.cpp index 4a4755637a..2f6afd5be5 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -535,16 +535,14 @@ static void outline(OutlineOp mode, Cursor & cur, Text * text) continue; DocumentClass const & tc = buf.params().documentClass(); - DocumentClass::const_iterator lit = tc.begin(); - DocumentClass::const_iterator len = tc.end(); int const newtoclevel = (mode == OutlineIn ? toclevel + 1 : toclevel - 1); LabelType const oldlabeltype = start->layout().labeltype; - for (; lit != len; ++lit) { - if (lit->toclevel == newtoclevel && - lit->labeltype == oldlabeltype) { - start->setLayout(*lit); + for (auto const & lay : tc) { + if (lay.toclevel == newtoclevel && + lay.labeltype == oldlabeltype) { + start->setLayout(lay); break; } } @@ -2079,10 +2077,8 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd) // Insert auto-insert arguments bool autoargs = false, inautoarg = false; Layout::LaTeXArgMap args = cur.inset().getLayout().args(); - Layout::LaTeXArgMap::const_iterator lait = args.begin(); - Layout::LaTeXArgMap::const_iterator const laend = args.end(); - for (; lait != laend; ++lait) { - Layout::latexarg arg = (*lait).second; + for (auto const & argt : args) { + Layout::latexarg arg = argt.second; if (!inautoarg && arg.insertonnewline && cur.pos() > 0) { FuncRequest cmd2(LFUN_PARAGRAPH_BREAK); lyx::dispatch(cmd2); @@ -2100,7 +2096,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd) lyx::dispatch(cmd2); } } - FuncRequest cmd2(LFUN_ARGUMENT_INSERT, (*lait).first); + FuncRequest cmd2(LFUN_ARGUMENT_INSERT, argt.first); lyx::dispatch(cmd2); autoargs = true; inautoarg = true; @@ -3426,11 +3422,11 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd, case LFUN_SPELLING_ADD: case LFUN_SPELLING_IGNORE: case LFUN_SPELLING_REMOVE: - enable = theSpellChecker() != NULL; + enable = theSpellChecker() != nullptr; if (enable && !cmd.getArg(1).empty()) { // validate explicitly given language Language const * const lang = const_cast(languages.getLanguage(cmd.getArg(1))); - enable &= lang != NULL; + enable &= lang != nullptr; } break; @@ -3690,12 +3686,8 @@ std::vector Text::getFreeFonts() const { vector ffList; - FontStack::const_iterator cit = freeFonts.begin(); - FontStack::const_iterator end = freeFonts.end(); - for (; cit != end; ++cit) - // we do not use cit-> here because gcc 2.9x does not - // like it (JMarc) - ffList.push_back((*cit).first); + for (auto const & f : freeFonts) + ffList.push_back(f.first); return ffList; } diff --git a/src/TextClass.cpp b/src/TextClass.cpp index 4ac03ab31a..d992c5ccd2 100644 --- a/src/TextClass.cpp +++ b/src/TextClass.cpp @@ -806,17 +806,15 @@ TextClass::ReturnValues TextClass::read(Lexer & lexrc, ReadType rt) if (lexrc.next()) { vector const dbs = getVectorFromString(rtrim(lexrc.getString()), "|"); - vector::const_iterator it = dbs.begin(); - vector::const_iterator end = dbs.end(); - for (; it != end; ++it) { - if (!contains(*it, ':')) { + for (auto const & dbase : dbs) { + if (!contains(dbase, ':')) { vector const enginetypes = getVectorFromString(opt_enginetype_, "|"); - for (string const &s: enginetypes) - cite_default_biblio_style_[s] = *it; + for (string const & s: enginetypes) + cite_default_biblio_style_[s] = dbase; } else { string eng; - string const db = split(*it, eng, ':'); + string const db = split(dbase, eng, ':'); cite_default_biblio_style_[eng] = db; } } @@ -932,10 +930,8 @@ TextClass::ReturnValues TextClass::read(Lexer & lexrc, ReadType rt) min_toclevel_ = Layout::NOT_IN_TOC; max_toclevel_ = Layout::NOT_IN_TOC; - const_iterator lit = begin(); - const_iterator len = end(); - for (; lit != len; ++lit) { - int const toclevel = lit->toclevel; + for (auto const & lay : *this) { + int const toclevel = lay.toclevel; if (toclevel != Layout::NOT_IN_TOC) { if (min_toclevel_ == Layout::NOT_IN_TOC) min_toclevel_ = toclevel; @@ -1200,7 +1196,7 @@ bool TextClass::readCiteEngine(Lexer & lexrc, ReadType rt, bool const add) cs.cmd = latex_cmd.empty() ? lyx_cmd : latex_cmd; if (!alias.empty()) { vector const aliases = getVectorFromString(alias); - for (string const &s: aliases) + for (string const & s: aliases) cite_command_aliases_[s] = lyx_cmd; } vector const stardescs = getVectorFromString(stardesc, "!"); @@ -1554,11 +1550,9 @@ bool TextClass::readFloat(Lexer & lexrc) if (!usesfloat && listcommand.empty()) { // if this float uses the same auxfile as an existing one, // there is no need for it to provide a list command. - FloatList::const_iterator it = floatlist_.begin(); - FloatList::const_iterator en = floatlist_.end(); bool found_ext = false; - for (; it != en; ++it) { - if (it->second.ext() == ext) { + for (auto const & f : floatlist_) { + if (f.second.ext() == ext) { found_ext = true; break; } @@ -1645,8 +1639,8 @@ Layout const & TextClass::operator[](docstring const & name) const if (it == end()) { LYXERR0("We failed to find the layout '" << name << "' in the layout list. You MUST investigate!"); - for (const_iterator cit = begin(); cit != end(); ++cit) - lyxerr << " " << to_utf8(cit->name()) << endl; + for (auto const & lay : *this) + lyxerr << " " << to_utf8(lay.name()) << endl; // We require the name to exist static const Layout dummy; @@ -1667,8 +1661,8 @@ Layout & TextClass::operator[](docstring const & name) if (it == end()) { LYXERR0("We failed to find the layout '" << to_utf8(name) << "' in the layout list. You MUST investigate!"); - for (const_iterator cit = begin(); cit != end(); ++cit) - LYXERR0(" " << to_utf8(cit->name())); + for (auto const & lay : *this) + LYXERR0(" " << to_utf8(lay.name())); // we require the name to exist LATTEST(false); @@ -1745,14 +1739,13 @@ string DocumentClass::forcedLayouts() const { ostringstream os; bool first = true; - const_iterator const e = end(); - for (const_iterator i = begin(); i != e; ++i) { - if (i->forcelocal > 0) { + for (auto const & lay : *this) { + if (lay.forcelocal > 0) { if (first) { os << "Format " << LAYOUT_FORMAT << '\n'; first = false; } - i->write(os); + lay.write(os); } } return os.str(); @@ -1817,7 +1810,7 @@ bool TextClass::isPlainLayout(Layout const & layout) const Layout TextClass::createBasicLayout(docstring const & name, bool unknown) const { - static Layout * defaultLayout = NULL; + static Layout * defaultLayout = nullptr; if (defaultLayout) { defaultLayout->setUnknown(unknown); @@ -1853,17 +1846,14 @@ DocumentClassPtr getDocumentClass( { DocumentClassPtr doc_class = DocumentClassPtr(new DocumentClass(baseClass)); - LayoutModuleList::const_iterator it = modlist.begin(); - LayoutModuleList::const_iterator en = modlist.end(); - for (; it != en; ++it) { - string const modName = *it; - LyXModule * lm = theModuleList[modName]; + for (auto const & mod : modlist) { + LyXModule * lm = theModuleList[mod]; if (!lm) { docstring const msg = bformat(_("The module %1$s has been requested by\n" "this document but has not been found in the list of\n" "available modules. If you recently installed it, you\n" - "probably need to reconfigure LyX.\n"), from_utf8(modName)); + "probably need to reconfigure LyX.\n"), from_utf8(mod)); if (!clone) frontend::Alert::warning(_("Module not available"), msg); continue; @@ -1877,13 +1867,13 @@ DocumentClassPtr getDocumentClass( "Missing prerequisites:\n" "\t%2$s\n" "See section 3.1.2.3 (Modules) of the User's Guide for more information."), - from_utf8(modName), prereqs); + from_utf8(mod), prereqs); frontend::Alert::warning(_("Package not available"), msg, true); } FileName layout_file = libFileSearch("layouts", lm->getFilename()); if (!doc_class->read(layout_file, TextClass::MODULE)) { docstring const msg = - bformat(_("Error reading module %1$s\n"), from_utf8(modName)); + bformat(_("Error reading module %1$s\n"), from_utf8(mod)); frontend::Alert::warning(_("Read Error"), msg); } } @@ -1937,10 +1927,8 @@ DocumentClass::DocumentClass(LayoutFile const & tc) bool DocumentClass::hasLaTeXLayout(std::string const & lay) const { - LayoutList::const_iterator it = layoutlist_.begin(); - LayoutList::const_iterator end = layoutlist_.end(); - for (; it != end; ++it) - if (it->latexname() == lay) + for (auto const & l : layoutlist_) + if (l.latexname() == lay) return true; return false; } @@ -1961,17 +1949,15 @@ bool DocumentClass::hasTocLevels() const Layout const & DocumentClass::getTOCLayout() const { // we're going to look for the layout with the minimum toclevel - TextClass::LayoutList::const_iterator lit = begin(); - TextClass::LayoutList::const_iterator const len = end(); int minlevel = 1000; - Layout const * lay = NULL; - for (; lit != len; ++lit) { - int const level = lit->toclevel; + Layout const * lay = nullptr; + for (auto const & l : *this) { + int const level = l.toclevel; // we don't want Part or unnumbered sections if (level == Layout::NOT_IN_TOC || level < 0 - || level >= minlevel || lit->counter.empty()) + || level >= minlevel || l.counter.empty()) continue; - lay = &*lit; + lay = &l; minlevel = level; } if (lay) @@ -2030,13 +2016,10 @@ vector const DocumentClass::citeCommands( CiteEngineType const & type) const { vector const styles = citeStyles(type); - vector::const_iterator it = styles.begin(); - vector::const_iterator end = styles.end(); vector cmds; - for (; it != end; ++it) { - CitationStyle const cite = *it; - cmds.push_back(cite.name); - } + for (auto const & cs : styles) + cmds.push_back(cs.name); + return cmds; } diff --git a/src/insets/InsetFloatList.cpp b/src/insets/InsetFloatList.cpp index 6486b390a2..1ba8dea8e7 100644 --- a/src/insets/InsetFloatList.cpp +++ b/src/insets/InsetFloatList.cpp @@ -223,7 +223,7 @@ docstring InsetFloatList::xhtml(XHTMLStream &, OutputParams const & op) const { TextClass::LayoutList::const_iterator lit = dc.begin(); TextClass::LayoutList::const_iterator len = dc.end(); int minlevel = 1000; - Layout const * lay = NULL; + Layout const * lay = nullptr; for (; lit != len; ++lit) { int const level = lit->toclevel; if (level > 0 && (level == Layout::NOT_IN_TOC || level >= minlevel)) diff --git a/src/support/lassert.cpp b/src/support/lassert.cpp index 2634749480..dcf8acd011 100644 --- a/src/support/lassert.cpp +++ b/src/support/lassert.cpp @@ -109,7 +109,7 @@ docstring printCallStack() char** messages = backtrace_symbols(array, size); docstring bt; - for (size_t i = 1; i < size && messages != NULL; i++) { + for (size_t i = 1; i < size && messages != nullptr; i++) { const std::string orig(messages[i]); char* mangled = nullptr; for (char *p = messages[i]; *p; ++p) {