mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-30 22:29:13 +08:00
refs #710 Simple algorithms to replace several regular expressions.
This commit is contained in:
@@ -230,7 +230,7 @@ namespace BlackCore
|
|||||||
CLogMessage(this).info("VATSIM file has same content, skipped");
|
CLogMessage(this).info("VATSIM file has same content, skipped");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const QStringList lines = dataFileData.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
|
const QList<QStringRef> lines = splitLinesRefs(dataFileData);
|
||||||
if (lines.isEmpty()) { return; }
|
if (lines.isEmpty()) { return; }
|
||||||
|
|
||||||
// build on local vars for thread safety
|
// build on local vars for thread safety
|
||||||
@@ -243,7 +243,9 @@ namespace BlackCore
|
|||||||
|
|
||||||
QStringList clientSectionAttributes;
|
QStringList clientSectionAttributes;
|
||||||
Section section = SectionNone;
|
Section section = SectionNone;
|
||||||
for (const QString &cl : lines)
|
|
||||||
|
QString currentLine; // declared outside of the for loop, to amortize the cost of allocation
|
||||||
|
for (QStringRef clRef : lines)
|
||||||
{
|
{
|
||||||
if (this->isAbandoned())
|
if (this->isAbandoned())
|
||||||
{
|
{
|
||||||
@@ -253,7 +255,7 @@ namespace BlackCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parse lines
|
// parse lines
|
||||||
QString currentLine(cl.trimmed());
|
currentLine = clRef.toString().trimmed();
|
||||||
if (currentLine.isEmpty()) continue;
|
if (currentLine.isEmpty()) continue;
|
||||||
if (currentLine.startsWith(";"))
|
if (currentLine.startsWith(";"))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -102,14 +102,15 @@ namespace BlackCore
|
|||||||
nwReply->close(); // close asap
|
nwReply->close(); // close asap
|
||||||
|
|
||||||
if (dataFileData.isEmpty()) return;
|
if (dataFileData.isEmpty()) return;
|
||||||
const QStringList lines = dataFileData.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
|
const QList<QStringRef> lines = splitLinesRefs(dataFileData);
|
||||||
if (lines.isEmpty()) { return; }
|
if (lines.isEmpty()) { return; }
|
||||||
|
|
||||||
CUrlList dataFileUrls;
|
CUrlList dataFileUrls;
|
||||||
CUrlList serverFileUrls;
|
CUrlList serverFileUrls;
|
||||||
CUrlList metarFileUrls;
|
CUrlList metarFileUrls;
|
||||||
|
|
||||||
for (const QString &cl : lines)
|
QString currentLine; // declared outside of the for loop, to amortize the cost of allocation
|
||||||
|
for (QStringRef clRef : lines)
|
||||||
{
|
{
|
||||||
if (this->isAbandoned())
|
if (this->isAbandoned())
|
||||||
{
|
{
|
||||||
@@ -119,7 +120,7 @@ namespace BlackCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parse lines
|
// parse lines
|
||||||
const QString currentLine(cl.trimmed());
|
currentLine = clRef.toString().trimmed();
|
||||||
if (currentLine.isEmpty()) { continue; }
|
if (currentLine.isEmpty()) { continue; }
|
||||||
if (currentLine.startsWith(";")) { continue; }
|
if (currentLine.startsWith(";")) { continue; }
|
||||||
if (!currentLine.contains("=")) { continue; }
|
if (!currentLine.contains("=")) { continue; }
|
||||||
|
|||||||
@@ -450,11 +450,8 @@ namespace BlackMisc
|
|||||||
bool CAircraftIcaoCode::isValidDesignator(const QString &designator)
|
bool CAircraftIcaoCode::isValidDesignator(const QString &designator)
|
||||||
{
|
{
|
||||||
if (designator.length() < 2 || designator.length() > 5) { return false; }
|
if (designator.length() < 2 || designator.length() > 5) { return false; }
|
||||||
|
if (!designator[0].isUpper()) { return false; }
|
||||||
static QThreadStorage<QRegularExpression> tsRegex;
|
return !containsChar(designator, [](QChar c) { return !c.isUpper() && !c.isDigit(); });
|
||||||
if (! tsRegex.hasLocalData()) { tsRegex.setLocalData(QRegularExpression("^[A-Z]+[A-Z0-9]*$")); }
|
|
||||||
const QRegularExpression ®exp = tsRegex.localData();
|
|
||||||
return (regexp.match(designator).hasMatch());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CAircraftIcaoCode::isValidCombinedType(const QString &combinedType)
|
bool CAircraftIcaoCode::isValidCombinedType(const QString &combinedType)
|
||||||
@@ -462,11 +459,14 @@ namespace BlackMisc
|
|||||||
if (combinedType.length() != 3) { return false; }
|
if (combinedType.length() != 3) { return false; }
|
||||||
|
|
||||||
// Amphibian, Glider, Helicopter, Seaplane, Landplane, Tilt wing
|
// Amphibian, Glider, Helicopter, Seaplane, Landplane, Tilt wing
|
||||||
|
static const QString validDescriptions = "AGHSLT";
|
||||||
// Electric, Jet, Piston, Turpoprop
|
// Electric, Jet, Piston, Turpoprop
|
||||||
static QThreadStorage<QRegularExpression> tsRegex;
|
static const QString validEngines = "EJPT";
|
||||||
if (! tsRegex.hasLocalData()) { tsRegex.setLocalData(QRegularExpression("^[AGHSLT][0-9][EJPT]$")); }
|
|
||||||
const QRegularExpression ®exp = tsRegex.localData();
|
if (!validDescriptions.contains(combinedType[0])) { return false; }
|
||||||
return (regexp.match(combinedType).hasMatch());
|
if (!combinedType[1].isDigit()) { return false; }
|
||||||
|
if (!validEngines.contains(combinedType[2])) { return false; }
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CAircraftIcaoCode::isValidWtc(const QString &candidate)
|
bool CAircraftIcaoCode::isValidWtc(const QString &candidate)
|
||||||
@@ -491,16 +491,11 @@ namespace BlackMisc
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString CAircraftIcaoCode::normalizeDesignator(const QString candidate)
|
QString CAircraftIcaoCode::normalizeDesignator(const QString &candidate)
|
||||||
{
|
{
|
||||||
QString n(candidate.trimmed().toUpper());
|
QString n(candidate.trimmed().toUpper());
|
||||||
if (n.contains(' ')) { n = n.left(n.indexOf(' ')); } // cutoff as first space
|
n = n.left(indexOfChar(n, [](QChar c) { return c.isSpace(); }));
|
||||||
if (n.isEmpty()) { return n; }
|
return removeChars(n, [](QChar c) { return !c.isLetterOrNumber(); });
|
||||||
|
|
||||||
static QThreadStorage<QRegularExpression> tsRegex;
|
|
||||||
if (! tsRegex.hasLocalData()) { tsRegex.setLocalData(QRegularExpression("[^a-zA-Z\\d\\s]")); }
|
|
||||||
const QRegularExpression ®exp = tsRegex.localData();
|
|
||||||
return n.remove(regexp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList CAircraftIcaoCode::alternativeCombinedCodes(const QString &combinedCode)
|
QStringList CAircraftIcaoCode::alternativeCombinedCodes(const QString &combinedCode)
|
||||||
|
|||||||
@@ -259,7 +259,7 @@ namespace BlackMisc
|
|||||||
static const QStringList &getSpecialDesignators();
|
static const QStringList &getSpecialDesignators();
|
||||||
|
|
||||||
//! Normalize designator, remove illegal characters
|
//! Normalize designator, remove illegal characters
|
||||||
static QString normalizeDesignator(const QString candidate);
|
static QString normalizeDesignator(const QString &candidate);
|
||||||
|
|
||||||
//! Create relaxed combined codes, e.g "L2J" -> "L3J", ...
|
//! Create relaxed combined codes, e.g "L2J" -> "L3J", ...
|
||||||
static QStringList alternativeCombinedCodes(const QString &combinedCode);
|
static QStringList alternativeCombinedCodes(const QString &combinedCode);
|
||||||
|
|||||||
@@ -301,13 +301,8 @@ namespace BlackMisc
|
|||||||
QString CAirlineIcaoCode::normalizeDesignator(const QString candidate)
|
QString CAirlineIcaoCode::normalizeDesignator(const QString candidate)
|
||||||
{
|
{
|
||||||
QString n(candidate.trimmed().toUpper());
|
QString n(candidate.trimmed().toUpper());
|
||||||
if (n.contains(' ')) { n = n.left(n.indexOf(' ')); } // cutoff at first space
|
n = n.left(indexOfChar(n, [](QChar c) { return c.isSpace(); }));
|
||||||
if (n.isEmpty()) { return n; }
|
return removeChars(n, [](QChar c) { return !c.isLetterOrNumber(); });
|
||||||
|
|
||||||
static QThreadStorage<QRegularExpression> tsRegex;
|
|
||||||
if (! tsRegex.hasLocalData()) { tsRegex.setLocalData(QRegularExpression("[^a-zA-Z\\d\\s]")); }
|
|
||||||
const QRegularExpression ®exp = tsRegex.localData();
|
|
||||||
return n.remove(regexp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString CAirlineIcaoCode::getCombinedStringWithKey() const
|
QString CAirlineIcaoCode::getCombinedStringWithKey() const
|
||||||
|
|||||||
@@ -31,9 +31,8 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
QString code = icaoCode.trimmed().toUpper();
|
QString code = icaoCode.trimmed().toUpper();
|
||||||
if (code.length() != 4) return "";
|
if (code.length() != 4) return "";
|
||||||
QRegularExpression reg("^[A-Z0-9]{4}$");
|
if (containsChar(code, [](QChar c) { return !c.isLetterOrNumber(); })) { return ""; }
|
||||||
auto match = reg.match(code);
|
return code;
|
||||||
return match.hasMatch() ? code : QString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CAirportIcaoCode::isValidIcaoDesignator(const QString &icaoCode)
|
bool CAirportIcaoCode::isValidIcaoDesignator(const QString &icaoCode)
|
||||||
|
|||||||
@@ -32,10 +32,7 @@ namespace BlackMisc
|
|||||||
|
|
||||||
QString CCallsign::unifyCallsign(const QString &callsign)
|
QString CCallsign::unifyCallsign(const QString &callsign)
|
||||||
{
|
{
|
||||||
QString unified = callsign.toUpper();
|
return removeChars(callsign.toUpper(), [](QChar c) { return !c.isLetterOrNumber() && c != '_'; });
|
||||||
// allow A-Z, 0-9, _, but no spaces
|
|
||||||
unified = unified.remove(QRegExp("[^A-Z\\d_]"));
|
|
||||||
return unified;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const CIcon &CCallsign::convertToIcon(const CCallsign &callsign)
|
const CIcon &CCallsign::convertToIcon(const CCallsign &callsign)
|
||||||
@@ -227,12 +224,7 @@ namespace BlackMisc
|
|||||||
bool CCallsign::isValidAircraftCallsign(const QString &callsign)
|
bool CCallsign::isValidAircraftCallsign(const QString &callsign)
|
||||||
{
|
{
|
||||||
if (callsign.length() < 2 || callsign.length() > 10) { return false; }
|
if (callsign.length() < 2 || callsign.length() > 10) { return false; }
|
||||||
|
return !containsChar(callsign, [](QChar c) { return !c.isUpper() && !c.isDigit(); });
|
||||||
// We allow all number callsigns
|
|
||||||
static QThreadStorage<QRegularExpression> tsRegex;
|
|
||||||
if (! tsRegex.hasLocalData()) { tsRegex.setLocalData(QRegularExpression("^[A-Z0-9]*$")); }
|
|
||||||
const QRegularExpression ®exp = tsRegex.localData();
|
|
||||||
return (regexp.match(callsign).hasMatch());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CCallsign::isValidAircraftCallsign(const CCallsign &callsign)
|
bool CCallsign::isValidAircraftCallsign(const CCallsign &callsign)
|
||||||
@@ -243,12 +235,7 @@ namespace BlackMisc
|
|||||||
bool CCallsign::isValidAtcCallsign(const QString &callsign)
|
bool CCallsign::isValidAtcCallsign(const QString &callsign)
|
||||||
{
|
{
|
||||||
if (callsign.length() < 2 || callsign.length() > 10) { return false; }
|
if (callsign.length() < 2 || callsign.length() > 10) { return false; }
|
||||||
|
return !containsChar(callsign, [](QChar c) { return !c.isUpper() && !c.isDigit(); });
|
||||||
// We allow all number callsigns
|
|
||||||
static QThreadStorage<QRegularExpression> tsRegex;
|
|
||||||
if (! tsRegex.hasLocalData()) { tsRegex.setLocalData(QRegularExpression("^[A-Z0-9_]*$")); }
|
|
||||||
const QRegularExpression ®exp = tsRegex.localData();
|
|
||||||
return (regexp.match(callsign).hasMatch());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CCallsign::isValidAtcCallsign(const CCallsign &callsign)
|
bool CCallsign::isValidAtcCallsign(const CCallsign &callsign)
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
#include "blackmisc/simulation/xplane/aircraftmodelloaderxplane.h"
|
#include "blackmisc/simulation/xplane/aircraftmodelloaderxplane.h"
|
||||||
#include "blackmisc/simulation/xplane/xplaneutil.h"
|
#include "blackmisc/simulation/xplane/xplaneutil.h"
|
||||||
#include "blackmisc/statusmessage.h"
|
#include "blackmisc/statusmessage.h"
|
||||||
|
#include "blackmisc/stringutils.h"
|
||||||
#include "blackmisc/worker.h"
|
#include "blackmisc/worker.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -386,7 +387,7 @@ namespace BlackMisc
|
|||||||
// Version number.
|
// Version number.
|
||||||
QString versionLine = readLineFrom(ts);
|
QString versionLine = readLineFrom(ts);
|
||||||
if (versionLine.isNull()) { return false; }
|
if (versionLine.isNull()) { return false; }
|
||||||
QString version = versionLine.split(QRegularExpression("\\s"), QString::SkipEmptyParts).at(0);
|
QString version = splitStringRefs(versionLine, [](QChar c) { return c.isSpace(); }).value(0).toString();
|
||||||
|
|
||||||
// For version 7, there is another line 'obj'
|
// For version 7, there is another line 'obj'
|
||||||
if (version == "700") { readLineFrom(ts); }
|
if (version == "700") { readLineFrom(ts); }
|
||||||
@@ -394,7 +395,7 @@ namespace BlackMisc
|
|||||||
// Texture
|
// Texture
|
||||||
QString textureLine = readLineFrom(ts);
|
QString textureLine = readLineFrom(ts);
|
||||||
if (textureLine.isNull()) { return false; }
|
if (textureLine.isNull()) { return false; }
|
||||||
QString texture = textureLine.split(QRegularExpression("\\s"), QString::SkipEmptyParts).at(0);
|
QString texture = splitStringRefs(textureLine, [](QChar c) { return c.isSpace(); }).value(0).toString();
|
||||||
|
|
||||||
objFile.close();
|
objFile.close();
|
||||||
|
|
||||||
@@ -570,7 +571,7 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
++lineNum;
|
++lineNum;
|
||||||
QString line = in.readLine();
|
QString line = in.readLine();
|
||||||
auto tokens = line.split(QRegularExpression("\\s+"));
|
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]);
|
||||||
@@ -614,7 +615,7 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
++lineNum;
|
++lineNum;
|
||||||
QString line = in.readLine();
|
QString line = in.readLine();
|
||||||
auto tokens = line.split(QRegularExpression("\\s+"), QString::SkipEmptyParts);
|
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]);
|
||||||
|
|||||||
@@ -16,6 +16,16 @@
|
|||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
|
QList<QStringRef> splitLinesRefs(const QString &s)
|
||||||
|
{
|
||||||
|
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'; });
|
||||||
|
}
|
||||||
|
|
||||||
QString boolToOnOff(bool v, bool i18n)
|
QString boolToOnOff(bool v, bool i18n)
|
||||||
{
|
{
|
||||||
Q_UNUSED(i18n);
|
Q_UNUSED(i18n);
|
||||||
@@ -166,9 +176,7 @@ namespace BlackMisc
|
|||||||
|
|
||||||
QString simplifyNameForSearch(const QString &name)
|
QString simplifyNameForSearch(const QString &name)
|
||||||
{
|
{
|
||||||
static const QRegularExpression reg("[^A-Z]");
|
return removeChars(name.toUpper(), [](QChar c) { return !c.isUpper(); });
|
||||||
const QString r = name.toUpper().remove(reg);
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,22 +13,84 @@
|
|||||||
#define BLACKMISC_STRINGUTILS_H
|
#define BLACKMISC_STRINGUTILS_H
|
||||||
|
|
||||||
#include "blackmisc/blackmiscexport.h"
|
#include "blackmisc/blackmiscexport.h"
|
||||||
|
#include "blackmisc/range.h"
|
||||||
|
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <QList>
|
||||||
#include <QMapIterator>
|
#include <QMapIterator>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QStringRef>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
template <class T1, class T2> class QMap;
|
template <class T1, class T2> class QMap;
|
||||||
|
|
||||||
//! Free functions in BlackMisc
|
//! Free functions in BlackMisc
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
|
//! Return a string with characters removed that match the given predicate.
|
||||||
|
template <class F> QString removeChars(const QString &s, F predicate)
|
||||||
|
{
|
||||||
|
QString result;
|
||||||
|
std::copy_if(s.begin(), s.end(), std::back_inserter(result), [=](auto c) { return !predicate(c); });
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! True if any character in the string matches the given predicate.
|
||||||
|
template <class F> bool containsChar(const QString &s, F predicate)
|
||||||
|
{
|
||||||
|
return std::any_of(s.begin(), s.end(), predicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Index of first character in the string matching the given predicate, or -1 if not found.
|
||||||
|
template <class F> int indexOfChar(const QString &s, F predicate)
|
||||||
|
{
|
||||||
|
auto it = std::find_if(s.begin(), s.end(), predicate);
|
||||||
|
if (it == s.end()) { return -1; }
|
||||||
|
return std::distance(s.begin(), it);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Split a string into multiple strings, using a predicate function to identify the split points.
|
||||||
|
//! \warning The returned refs are only valid during the lifetime of the original string.
|
||||||
|
template <class F> QList<QStringRef> splitStringRefs(const QString &s, F predicate)
|
||||||
|
{
|
||||||
|
QList<QStringRef> result;
|
||||||
|
auto notPredicate = [=](auto c) { return !predicate(c); };
|
||||||
|
auto begin = s.begin();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
begin = std::find_if(begin, s.end(), notPredicate);
|
||||||
|
if (begin == s.end()) { return result; }
|
||||||
|
auto end = std::find_if(begin, s.end(), predicate);
|
||||||
|
result.push_back(QStringRef(&s, std::distance(s.begin(), begin), std::distance(begin, end)));
|
||||||
|
begin = end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//! Split a string into multiple lines. Blank lines are skipped.
|
||||||
|
//! \warning The returned refs are only valid during the lifetime of the original string.
|
||||||
|
BLACKMISC_EXPORT QList<QStringRef> splitLinesRefs(const QString &s);
|
||||||
|
|
||||||
|
//! It would be risky to call splitStringRefs with an rvalue, so forbid it.
|
||||||
|
template <class F> void splitStringRefs(const QString &&, F) = delete;
|
||||||
|
|
||||||
|
//! It would be risky to call splitLinesRefs with an rvalue, so forbid it.
|
||||||
|
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);
|
||||||
|
|
||||||
//! A map converted to string
|
//! A map converted to string
|
||||||
template<class K, class V> QString qmapToString(const QMap<K, V> &map)
|
template<class K, class V> QString qmapToString(const QMap<K, V> &map)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#include "testmath.h"
|
#include "testmath.h"
|
||||||
#include "testphysicalquantities.h"
|
#include "testphysicalquantities.h"
|
||||||
#include "testslot.h"
|
#include "testslot.h"
|
||||||
|
#include "teststringutils.h"
|
||||||
#include "testvaluecache.h"
|
#include "testvaluecache.h"
|
||||||
#include "testvariantandmap.h"
|
#include "testvariantandmap.h"
|
||||||
#include "testweather.h"
|
#include "testweather.h"
|
||||||
@@ -74,6 +75,10 @@ namespace BlackMiscTest
|
|||||||
CTestSlot slotTests;
|
CTestSlot slotTests;
|
||||||
status |= test.exec(&slotTests, "blackmisc_slot");
|
status |= test.exec(&slotTests, "blackmisc_slot");
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
CTestStringUtils stringUtilTests;
|
||||||
|
status |= test.exec(&stringUtilTests, "blackmisc_stringutils");
|
||||||
|
}
|
||||||
{
|
{
|
||||||
CTestValueCache valueCacheTests;
|
CTestValueCache valueCacheTests;
|
||||||
status |= test.exec(&valueCacheTests, "blackmisc_valuecache");
|
status |= test.exec(&valueCacheTests, "blackmisc_valuecache");
|
||||||
|
|||||||
61
tests/blackmisc/teststringutils.cpp
Normal file
61
tests/blackmisc/teststringutils.cpp
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
/* Copyright (C) 2013
|
||||||
|
* swift Project Community / Contributors
|
||||||
|
*
|
||||||
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||||
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||||
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||||
|
* contained in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! \cond PRIVATE_TESTS
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \file
|
||||||
|
* \ingroup testblackmisc
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "teststringutils.h"
|
||||||
|
#include "blackmisc/stringutils.h"
|
||||||
|
|
||||||
|
#include <QTest>
|
||||||
|
|
||||||
|
using namespace BlackMisc;
|
||||||
|
|
||||||
|
namespace BlackMiscTest
|
||||||
|
{
|
||||||
|
|
||||||
|
void CTestStringUtils::testRemove()
|
||||||
|
{
|
||||||
|
QString s = "loUwP PeERr69";
|
||||||
|
QVERIFY2(removeChars(s, [](QChar c) { return !c.isUpper(); }) == "UPPER", "Test removing characters by predicate");
|
||||||
|
}
|
||||||
|
|
||||||
|
void CTestStringUtils::testContains()
|
||||||
|
{
|
||||||
|
QString s = "string with a numb3r";
|
||||||
|
QVERIFY2(containsChar(s, [](QChar c) { return c.isNumber(); }), "Test contains character by predicate");
|
||||||
|
s = "string without a number";
|
||||||
|
QVERIFY2(!containsChar(s, [](QChar c) { return c.isNumber(); }), "Test not contains character by predicate");
|
||||||
|
}
|
||||||
|
|
||||||
|
void CTestStringUtils::testIndexOf()
|
||||||
|
{
|
||||||
|
QString s = "string with a numb3r";
|
||||||
|
QVERIFY2(indexOfChar(s, [](QChar c) { return c.isNumber(); }) == 18, "Test index of character by predicate");
|
||||||
|
s = "string without a number";
|
||||||
|
QVERIFY2(indexOfChar(s, [](QChar c) { return c.isNumber(); }) == -1, "Test not index of character by predicate");
|
||||||
|
}
|
||||||
|
|
||||||
|
void CTestStringUtils::testSplit()
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \endcond
|
||||||
45
tests/blackmisc/teststringutils.h
Normal file
45
tests/blackmisc/teststringutils.h
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/* Copyright (C) 2014
|
||||||
|
* swift project community / contributors
|
||||||
|
*
|
||||||
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||||
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||||
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||||
|
* contained in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BLACKMISCTEST_TESTSTRINGUTILS_H
|
||||||
|
#define BLACKMISCTEST_TESTSTRINGUTILS_H
|
||||||
|
|
||||||
|
//! \cond PRIVATE_TESTS
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \file
|
||||||
|
* \ingroup testblackmisc
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
namespace BlackMiscTest
|
||||||
|
{
|
||||||
|
|
||||||
|
//! Testing string utilities
|
||||||
|
class CTestStringUtils : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
//! Constructor
|
||||||
|
explicit CTestStringUtils(QObject *parent = nullptr) : QObject(parent) {}
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void testRemove();
|
||||||
|
void testContains();
|
||||||
|
void testIndexOf();
|
||||||
|
void testSplit();
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \endcond
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user