]> git.lyx.org Git - features.git/commitdiff
DocBook: avoid using isspace in StartTag::writeTag.
authorThibaut Cuvelier <tcuvelier@lyx.org>
Wed, 2 Sep 2020 23:23:20 +0000 (01:23 +0200)
committerThibaut Cuvelier <tcuvelier@lyx.org>
Sat, 19 Sep 2020 18:43:40 +0000 (20:43 +0200)
This is the cause of crashes (on both Windows and Linux).

src/output_docbook.cpp
src/xml.cpp
src/xml.h

index addf24a99d9e97a500419e08e4327d84a410084b..ce7e641a2d9fd32c346e0af1be88a9aee5f9acfa 100644 (file)
@@ -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);
 
index dd9790f2e85efdfa66a7d20d2e87355b6392517c..8e936f817e883f2d7e0fd8769536bad176a2f06b 100644 (file)
@@ -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.
index 52eeb22f973bc2f008e7ee98b25297a51e57f709..d5b42332cad0e88762edc9bbead246b0b3c45730 100644 (file)
--- 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;