Skip to content

Commit e32bb70

Browse files
committed
Improvements to the overall speed and memory use of libCellML.
1 parent fce139c commit e32bb70

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/printer.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,19 @@ std::string Printer::PrinterImpl::printMath(const std::string &math)
130130

131131
XmlDocPtr xmlDoc = std::make_shared<XmlDoc>();
132132
xmlKeepBlanksDefault(0);
133-
// Remove any XML declarations from the string.
133+
// Remove a leading XML declaration from the string, but preserve other processing instructions such as
134+
// <?xml-stylesheet ...?>.
134135
std::string normalisedMath = math;
135136
size_t pos = 0;
136137
while ((pos = normalisedMath.find("<?xml", pos)) != std::string::npos) {
137-
auto end = normalisedMath.find("?>", pos + 5);
138-
if (end != std::string::npos) {
139-
normalisedMath.erase(pos, end + 2 - pos);
140-
} else {
141-
break;
138+
if ((pos + 5 < normalisedMath.size()) && std::isspace(static_cast<unsigned char>(normalisedMath[pos + 5]))) {
139+
auto end = normalisedMath.find("?>", pos + 5);
140+
if (end != std::string::npos) {
141+
normalisedMath.erase(pos, end + 2 - pos);
142+
continue;
143+
}
142144
}
145+
pos += 5;
143146
}
144147
xmlDoc->parse("<" + wrapElementName + ">" + normalisedMath + "</" + wrapElementName + ">");
145148
if (xmlDoc->xmlErrorCount() == 0) {

0 commit comments

Comments
 (0)