Browse Source

Code refactor, make use of benchmarks

master
Debao Zhang 11 years ago
parent
commit
1b554d4fea
  1. 9
      src/xlsx/xlsxsharedstrings.cpp
  2. 8
      src/xlsx/xlsxutility.cpp
  3. 1
      src/xlsx/xlsxutility_p.h
  4. 10
      src/xlsx/xlsxworksheet.cpp
  5. 3
      tests/benchmarks/benchmarks.pro
  6. 98
      tests/benchmarks/xmlspace/tst_xmlspacetest.cpp
  7. 13
      tests/benchmarks/xmlspace/xmlspace.pro

9
src/xlsx/xlsxsharedstrings.cpp

@ -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
}

8
src/xlsx/xlsxutility.cpp

@ -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

1
src/xlsx/xlsxutility_p.h

@ -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

10
src/xlsx/xlsxworksheet.cpp

@ -1369,13 +1369,19 @@ void WorksheetPrivate::saveXmlCellData(QXmlStreamWriter &writer, int row, int co
writer.writeEndElement();// rPr
}
writer.writeStartElement(QStringLiteral("t"));
writer.writeAttribute(QStringLiteral("xml:space"), QStringLiteral("preserve"));
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){

3
tests/benchmarks/benchmarks.pro

@ -0,0 +1,3 @@
TEMPLATE = subdirs
SUBDIRS += \
xmlspace

98
tests/benchmarks/xmlspace/tst_xmlspacetest.cpp

@ -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"

13
tests/benchmarks/xmlspace/xmlspace.pro

@ -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/\\\"
Loading…
Cancel
Save