Partially revert "Remove unused stringutils methods"

This partially reverts commit 0c1a5a5c97.
This commit is contained in:
Lars Toenning
2022-06-05 22:47:41 +02:00
parent 684b2b1068
commit 1a1b5429d5
5 changed files with 35 additions and 2 deletions

View File

@@ -408,6 +408,13 @@ namespace BlackSample
} }
out << "Remove from 100,000 strings all uppercase letters: (regex) " << timer.elapsed() << "ms" << endl << Qt::endl; out << "Remove from 100,000 strings all uppercase letters: (regex) " << timer.elapsed() << "ms" << endl << Qt::endl;
timer.start();
{
auto lines = splitLines(bigString);
Q_UNUSED(lines);
}
out << "Split 100,000 line string into list of lines: (QStringList) " << timer.elapsed() << "ms" << Qt::endl;
timer.start(); timer.start();
{ {
auto lines = splitLinesRefs(bigString); auto lines = splitLinesRefs(bigString);

View File

@@ -569,7 +569,7 @@ namespace BlackMisc::Simulation::XPlane
{ {
++lineNum; ++lineNum;
QString line = in.readLine(); QString line = in.readLine();
auto tokens = line.split(' '); auto tokens = splitString(line, [](QChar c) { return c.isSpace(); });
if (!tokens.empty()) if (!tokens.empty())
{ {
auto it = commands.find(tokens[0]); auto it = commands.find(tokens[0]);
@@ -615,7 +615,7 @@ namespace BlackMisc::Simulation::XPlane
++lineNum; ++lineNum;
QString line = in.readLine(); QString line = in.readLine();
if (line.isEmpty() || line[0] == '#') continue; if (line.isEmpty() || line[0] == '#') continue;
auto tokens = line.split(' '); auto tokens = splitString(line, [](QChar c) { return c.isSpace(); });
if (!tokens.empty()) if (!tokens.empty())
{ {
auto it = commands.find(tokens[0]); auto it = commands.find(tokens[0]);

View File

@@ -29,6 +29,11 @@ namespace BlackMisc
return splitStringRefs(s, [](QChar c) { return c == '\n' || c == '\r'; }); return splitStringRefs(s, [](QChar c) { return c == '\n' || c == '\r'; });
} }
QStringList splitLines(const QString &s)
{
return splitString(s, [](QChar c) { return c == '\n' || c == '\r'; });
}
QByteArray utfToPercentEncoding(const QString& s, const QByteArray &allow, char percent) QByteArray utfToPercentEncoding(const QString& s, const QByteArray &allow, char percent)
{ {
QByteArray result; QByteArray result;

View File

@@ -12,6 +12,7 @@
#define BLACKMISC_STRINGUTILS_H #define BLACKMISC_STRINGUTILS_H
#include "blackmisc/blackmiscexport.h" #include "blackmisc/blackmiscexport.h"
#include "blackmisc/range.h"
#include "blackmisc/typetraits.h" #include "blackmisc/typetraits.h"
#include <QByteArray> #include <QByteArray>
@@ -116,6 +117,15 @@ namespace BlackMisc
//! It would be risky to call splitLinesRefs with an rvalue, so forbid it. //! It would be risky to call splitLinesRefs with an rvalue, so forbid it.
void splitLinesRefs(const QString &&) = delete; void splitLinesRefs(const QString &&) = delete;
//! Split a string into multiple strings, using a predicate function to identify the split points.
template <class F> QStringList splitString(const QString &s, F predicate)
{
return makeRange(splitStringRefs(s, predicate)).transform([](QStringRef sr) { return sr.toString(); });
}
//! Split a string into multiple lines. Blank lines are skipped.
BLACKMISC_EXPORT QStringList splitLines(const QString &s);
//! Extended percent encoding supporting UTF-16 //! Extended percent encoding supporting UTF-16
BLACKMISC_EXPORT QByteArray utfToPercentEncoding(const QString &s, const QByteArray &allow = {}, char percent = '%'); BLACKMISC_EXPORT QByteArray utfToPercentEncoding(const QString &s, const QByteArray &allow = {}, char percent = '%');

View File

@@ -33,6 +33,7 @@ namespace BlackMiscTest
void testRemove(); void testRemove();
void testContains(); void testContains();
void testIndexOf(); void testIndexOf();
void testSplit();
void testTimestampParsing(); void testTimestampParsing();
void testCodecs(); void testCodecs();
void testSimplify(); void testSimplify();
@@ -60,6 +61,16 @@ namespace BlackMiscTest
QVERIFY2(indexOfChar(s, [](QChar c) { return c.isNumber(); }) == -1, "Test not index of character by predicate"); QVERIFY2(indexOfChar(s, [](QChar c) { return c.isNumber(); }) == -1, "Test not index of character by predicate");
} }
void CTestStringUtils::testSplit()
{
const QString s = "line one\nline two\r\nline three\n";
QStringList lines = splitLines(s);
QVERIFY2(lines.size() == 3, "Test split string into lines: correct number of lines");
QVERIFY2(lines[0] == "line one", "Test split string into lines: correct first line");
QVERIFY2(lines[1] == "line two", "Test split string into lines: correct second line");
QVERIFY2(lines[2] == "line three", "Test split string into lines: correct third line");
}
void CTestStringUtils::testTimestampParsing() void CTestStringUtils::testTimestampParsing()
{ {
const QStringList dts( const QStringList dts(