Code refactor, make use of benchmarks
This commit is contained in:
@@ -31,7 +31,6 @@
|
||||
#include <QXmlStreamReader>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QRegularExpression>
|
||||
#include <QDebug>
|
||||
#include <QBuffer>
|
||||
|
||||
@@ -217,10 +216,8 @@ void SharedStrings::saveToXmlFile(QIODevice *device) const
|
||||
writer.writeEndElement();// rPr
|
||||
}
|
||||
writer.writeStartElement(QStringLiteral("t"));
|
||||
if (string.fragmentText(i).contains(QRegularExpression(QStringLiteral("^\\s")))
|
||||
|| string.fragmentText(i).contains(QRegularExpression(QStringLiteral("\\s$")))) {
|
||||
if (isSpaceReserveNeeded(string.fragmentText(i)))
|
||||
writer.writeAttribute(QStringLiteral("xml:space"), QStringLiteral("preserve"));
|
||||
}
|
||||
writer.writeCharacters(string.fragmentText(i));
|
||||
writer.writeEndElement();// t
|
||||
|
||||
@@ -229,10 +226,8 @@ void SharedStrings::saveToXmlFile(QIODevice *device) const
|
||||
} else {
|
||||
writer.writeStartElement(QStringLiteral("t"));
|
||||
QString pString = string.toPlainString();
|
||||
if (pString.contains(QRegularExpression(QStringLiteral("^\\s")))
|
||||
|| pString.contains(QRegularExpression(QStringLiteral("\\s$")))) {
|
||||
if (isSpaceReserveNeeded(pString))
|
||||
writer.writeAttribute(QStringLiteral("xml:space"), QStringLiteral("preserve"));
|
||||
}
|
||||
writer.writeCharacters(pString);
|
||||
writer.writeEndElement();//t
|
||||
}
|
||||
|
||||
@@ -202,5 +202,13 @@ QString createSafeSheetName(const QString &nameProposal)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* whether the string s starts or ends with space
|
||||
*/
|
||||
bool isSpaceReserveNeeded(const QString &s)
|
||||
{
|
||||
QString spaces(QStringLiteral(" \t\n\r"));
|
||||
return !s.isEmpty() && (spaces.contains(s.at(0))||spaces.contains(s.at(s.length()-1)));
|
||||
}
|
||||
|
||||
} //namespace QXlsx
|
||||
|
||||
@@ -61,6 +61,7 @@ XLSX_AUTOTEST_EXPORT QString xl_rowcol_to_cell(int row, int col, bool row_abs=fa
|
||||
XLSX_AUTOTEST_EXPORT QString xl_rowcol_to_cell_fast(int row, int col);
|
||||
|
||||
XLSX_AUTOTEST_EXPORT QString createSafeSheetName(const QString &nameProposal);
|
||||
XLSX_AUTOTEST_EXPORT bool isSpaceReserveNeeded(const QString &string);
|
||||
|
||||
} //QXlsx
|
||||
#endif // XLSXUTILITY_H
|
||||
|
||||
@@ -1369,13 +1369,19 @@ void WorksheetPrivate::saveXmlCellData(QXmlStreamWriter &writer, int row, int co
|
||||
writer.writeEndElement();// rPr
|
||||
}
|
||||
writer.writeStartElement(QStringLiteral("t"));
|
||||
if (isSpaceReserveNeeded(string.fragmentText(i)))
|
||||
writer.writeAttribute(QStringLiteral("xml:space"), QStringLiteral("preserve"));
|
||||
writer.writeCharacters(string.fragmentText(i));
|
||||
writer.writeEndElement();// t
|
||||
writer.writeEndElement(); // r
|
||||
}
|
||||
} else {
|
||||
writer.writeTextElement(QStringLiteral("t"), cell->value().toString());
|
||||
writer.writeStartElement(QStringLiteral("t"));
|
||||
QString string = cell->value().toString();
|
||||
if (isSpaceReserveNeeded(string))
|
||||
writer.writeAttribute(QStringLiteral("xml:space"), QStringLiteral("preserve"));
|
||||
writer.writeCharacters(string);
|
||||
writer.writeEndElement(); // t
|
||||
}
|
||||
writer.writeEndElement();//is
|
||||
} else if (cell->dataType() == Cell::Numeric){
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS += \
|
||||
xmlspace
|
||||
@@ -0,0 +1,98 @@
|
||||
#include <QString>
|
||||
#include <QRegularExpression>
|
||||
#include <QtTest>
|
||||
|
||||
bool startsWithOrEndsWithSpace(const QString &s, int flag)
|
||||
{
|
||||
if (flag == 0) {
|
||||
return (s.contains(QRegularExpression("^\\s")) || s.contains(QRegularExpression("\\s$")));
|
||||
} else if (flag == 1) {
|
||||
return (s.contains(QRegularExpression("^\\s|\\s$")));
|
||||
} else if (flag == 2) {
|
||||
static QRegularExpression re("^\\s|\\s$");
|
||||
return s.contains(re);
|
||||
} else if (flag == 3) {
|
||||
return s.startsWith(QLatin1Char(' ')) || s.endsWith(QLatin1Char(' '))
|
||||
|| s.startsWith(QLatin1Char('\t')) || s.endsWith(QLatin1Char('\t'))
|
||||
|| s.startsWith(QLatin1Char('\r')) || s.endsWith(QLatin1Char('\r'))
|
||||
|| s.startsWith(QLatin1Char('\n')) || s.endsWith(QLatin1Char('\n'));
|
||||
} else if (flag == 4) {
|
||||
//static QString spaces(" \t\n\r");
|
||||
QString spaces(QStringLiteral(" \t\n\r"));
|
||||
return !s.isEmpty() && (spaces.contains(s.at(0))||spaces.contains(s.at(s.length()-1)));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class XmlspaceTest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
XmlspaceTest();
|
||||
|
||||
private Q_SLOTS:
|
||||
void teststartsWithOrEndsWithSpace();
|
||||
void teststartsWithOrEndsWithSpace_data();
|
||||
|
||||
void testCase1();
|
||||
void testCase1_data();
|
||||
};
|
||||
|
||||
XmlspaceTest::XmlspaceTest()
|
||||
{
|
||||
}
|
||||
|
||||
void XmlspaceTest::teststartsWithOrEndsWithSpace()
|
||||
{
|
||||
QFETCH(QString, data);
|
||||
QFETCH(bool, res);
|
||||
|
||||
for (int f=0; f<5; ++f) {
|
||||
QCOMPARE(startsWithOrEndsWithSpace(data, f), res);
|
||||
}
|
||||
}
|
||||
|
||||
void XmlspaceTest::teststartsWithOrEndsWithSpace_data()
|
||||
{
|
||||
//QTest::addColumn<int>("flag");
|
||||
QTest::addColumn<QString>("data");
|
||||
QTest::addColumn<bool>("res");
|
||||
|
||||
QTest::newRow("")<<QString()<<false;
|
||||
QTest::newRow("")<<""<<false;
|
||||
QTest::newRow("")<<" "<<true;
|
||||
QTest::newRow("")<<"A B"<<false;
|
||||
QTest::newRow("")<<" A B"<<true;
|
||||
QTest::newRow("")<<"A B\t"<<true;
|
||||
QTest::newRow("")<<" \tA B\t"<<true;
|
||||
QTest::newRow("")<<" A B "<<true;
|
||||
}
|
||||
|
||||
void XmlspaceTest::testCase1()
|
||||
{
|
||||
QFETCH(int, flag);
|
||||
|
||||
QStringList list;
|
||||
list<<""<<" "<<"A"<<"A B"<<" A"<<"B\t"<<" "<<" A B ";
|
||||
|
||||
QBENCHMARK {
|
||||
foreach(QString s, list)
|
||||
startsWithOrEndsWithSpace(s, flag);
|
||||
}
|
||||
}
|
||||
|
||||
void XmlspaceTest::testCase1_data()
|
||||
{
|
||||
QTest::addColumn<int>("flag");
|
||||
QTest::newRow("0") << 0;
|
||||
QTest::newRow("1") << 1;
|
||||
QTest::newRow("2") << 2;
|
||||
QTest::newRow("3") << 3;
|
||||
QTest::newRow("4") << 4;
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(XmlspaceTest)
|
||||
|
||||
#include "tst_xmlspacetest.moc"
|
||||
@@ -0,0 +1,13 @@
|
||||
QT += testlib #xlsx # xlsx-private
|
||||
CONFIG += testcase
|
||||
DEFINES += XLSX_TEST
|
||||
|
||||
TARGET = tst_xmlspacetest
|
||||
CONFIG += console
|
||||
CONFIG -= app_bundle
|
||||
|
||||
TEMPLATE = app
|
||||
|
||||
|
||||
SOURCES += tst_xmlspacetest.cpp
|
||||
DEFINES += SRCDIR=\\\"$$PWD/\\\"
|
||||
Reference in New Issue
Block a user