From 1b554d4feaecf896b492ad98fdbd990b61aa7bee Mon Sep 17 00:00:00 2001 From: Debao Zhang Date: Wed, 12 Mar 2014 15:38:21 +0800 Subject: [PATCH] Code refactor, make use of benchmarks --- src/xlsx/xlsxsharedstrings.cpp | 9 +- src/xlsx/xlsxutility.cpp | 8 ++ src/xlsx/xlsxutility_p.h | 1 + src/xlsx/xlsxworksheet.cpp | 10 +- tests/benchmarks/benchmarks.pro | 3 + .../benchmarks/xmlspace/tst_xmlspacetest.cpp | 98 +++++++++++++++++++ tests/benchmarks/xmlspace/xmlspace.pro | 13 +++ 7 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 tests/benchmarks/benchmarks.pro create mode 100644 tests/benchmarks/xmlspace/tst_xmlspacetest.cpp create mode 100644 tests/benchmarks/xmlspace/xmlspace.pro diff --git a/src/xlsx/xlsxsharedstrings.cpp b/src/xlsx/xlsxsharedstrings.cpp index c2e3622..cb5f03d 100755 --- a/src/xlsx/xlsxsharedstrings.cpp +++ b/src/xlsx/xlsxsharedstrings.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include @@ -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 } diff --git a/src/xlsx/xlsxutility.cpp b/src/xlsx/xlsxutility.cpp index 33923fc..a432b58 100755 --- a/src/xlsx/xlsxutility.cpp +++ b/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 diff --git a/src/xlsx/xlsxutility_p.h b/src/xlsx/xlsxutility_p.h index 5cb266f..7c1c5e5 100755 --- a/src/xlsx/xlsxutility_p.h +++ b/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 diff --git a/src/xlsx/xlsxworksheet.cpp b/src/xlsx/xlsxworksheet.cpp index acd1931..1b98fb8 100755 --- a/src/xlsx/xlsxworksheet.cpp +++ b/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){ diff --git a/tests/benchmarks/benchmarks.pro b/tests/benchmarks/benchmarks.pro new file mode 100644 index 0000000..c67a39b --- /dev/null +++ b/tests/benchmarks/benchmarks.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs +SUBDIRS += \ + xmlspace diff --git a/tests/benchmarks/xmlspace/tst_xmlspacetest.cpp b/tests/benchmarks/xmlspace/tst_xmlspacetest.cpp new file mode 100644 index 0000000..6ab1bcb --- /dev/null +++ b/tests/benchmarks/xmlspace/tst_xmlspacetest.cpp @@ -0,0 +1,98 @@ +#include +#include +#include + +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("flag"); + QTest::addColumn("data"); + QTest::addColumn("res"); + + QTest::newRow("")<("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" diff --git a/tests/benchmarks/xmlspace/xmlspace.pro b/tests/benchmarks/xmlspace/xmlspace.pro new file mode 100644 index 0000000..7b3f2e9 --- /dev/null +++ b/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/\\\"