From: Thibaut Cuvelier Date: Wed, 2 Sep 2020 23:23:20 +0000 (+0200) Subject: DocBook: avoid using isspace in StartTag::writeTag. X-Git-Tag: lyx-2.4.0dev-acb2ca7b~181^2~23 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=1a76fb9658cd26db9c224c0560905e6150b10efb;p=features.git DocBook: avoid using isspace in StartTag::writeTag. This is the cause of crashes (on both Windows and Linux). --- diff --git a/src/output_docbook.cpp b/src/output_docbook.cpp index addf24a99d..ce7e641a2d 100644 --- a/src/output_docbook.cpp +++ b/src/output_docbook.cpp @@ -429,16 +429,6 @@ void makeBibliography( } -bool isNotOnlySpace(docstring const & str) -{ - for (auto const & c: str) { - if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' && c != '\r') - return true; - } - return false; -} - - void makeParagraph( Text const & text, Buffer const & buf, @@ -531,7 +521,7 @@ void makeParagraph( ++nextpar; auto pars = par->simpleDocBookOnePar(buf, runparams, text.outerFont(distance(begin, par)), 0, nextpar == end, special_case); for (docstring const & parXML : pars) { - if (isNotOnlySpace(parXML)) { + if (xml::isNotOnlySpace(parXML)) { if (open_par) openParTag(xs, &*par, prevpar); diff --git a/src/xml.cpp b/src/xml.cpp index dd9790f2e8..8e936f817e 100644 --- a/src/xml.cpp +++ b/src/xml.cpp @@ -108,12 +108,9 @@ docstring StartTag::writeTag() const { docstring output = '<' + tag_; if (!attr_.empty()) { - docstring attributes = xml::escapeString(attr_, XMLStream::ESCAPE_NONE); - attributes.erase(attributes.begin(), std::find_if(attributes.begin(), attributes.end(), - [](int c) {return !std::isspace(c);})); - if (!attributes.empty()) { + docstring attributes = xml::trimLeft(xml::escapeString(attr_, XMLStream::ESCAPE_NONE)); + if (!attributes.empty()) output += ' ' + attributes; - } } output += ">"; return output; @@ -601,6 +598,28 @@ docstring xml::uniqueID(docstring const & label) } +bool xml::isNotOnlySpace(docstring const & str) +{ + for (auto const & c: str) { + if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' && c != '\r') + return true; + } + return false; +} + + +docstring xml::trimLeft(docstring const & str) +{ + size_t i = 0; + for (auto const & c: str) { + if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' && c != '\r') + return str.substr(i, docstring::npos); + i++; + } + return str; +} + + docstring xml::cleanID(docstring const & orig) { // The standard xml:id only allows letters, digits, '-' and '.' in a name. diff --git a/src/xml.h b/src/xml.h index 52eeb22f97..d5b42332ca 100644 --- a/src/xml.h +++ b/src/xml.h @@ -160,6 +160,12 @@ docstring cleanID(docstring const &orig); /// returns a unique numeric ID docstring uniqueID(docstring const & label); +/// determines whether a string only contains space characters +bool isNotOnlySpace(docstring const & str); + +/// trims the string to the left, i.e. remove any space-like character at the beginning of the string +docstring trimLeft(docstring const & str); + struct FontTag; struct EndFontTag;