refs #848, added (utility) functions to parser

This commit is contained in:
Klaus Basan
2016-12-31 05:04:16 +01:00
committed by Mathew Sutcliffe
parent c20b54466f
commit 0c720c7c71
2 changed files with 43 additions and 14 deletions

View File

@@ -8,6 +8,7 @@
*/
#include "blackmisc/pq/pqstring.h"
#include "blackmisc/stringutils.h"
#include "blackmisc/simplecommandparser.h"
#include <QList>
@@ -18,7 +19,6 @@ using namespace BlackMisc::PhysicalQuantities;
namespace BlackMisc
{
CSimpleCommandParser::CSimpleCommandParser(const QStringList &knownCommands)
{
this->setCheckedCommandList(knownCommands);
@@ -55,7 +55,7 @@ namespace BlackMisc
QString CSimpleCommandParser::remainingStringAfter(int index) const
{
if (index < 0) { return this->m_originalLine.trimmed(); }
QString p = this->part(index);
const QString p = this->part(index);
int fi = this->m_originalLine.indexOf(p, 0, Qt::CaseInsensitive);
if (fi < 0) { return ""; }
return this->m_originalLine.mid(fi).trimmed();
@@ -66,6 +66,11 @@ namespace BlackMisc
return this->m_splitParts.count();
}
bool CSimpleCommandParser::hasPart(int index) const
{
return index >= 0 && index < this->countParts();
}
int CSimpleCommandParser::countPartsWithoutCommand() const
{
int c = countParts();
@@ -75,8 +80,8 @@ namespace BlackMisc
bool CSimpleCommandParser::isInt(int index) const
{
const QString p = this->part(index);
if (p.isEmpty()) return false;
bool ok;
if (p.isEmpty()) { return false; }
bool ok = false;
p.toInt(&ok);
return ok;
}
@@ -84,8 +89,8 @@ namespace BlackMisc
bool CSimpleCommandParser::isDouble(int index) const
{
const QString p = this->part(index);
if (p.isEmpty()) return false;
bool ok;
if (p.isEmpty()) { return false; }
bool ok = false;
CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess);
return ok;
}
@@ -93,21 +98,38 @@ namespace BlackMisc
int CSimpleCommandParser::toInt(int index, int def) const
{
const QString p = this->part(index);
if (p.isEmpty()) return def;
bool ok;
if (p.isEmpty()) { return def; }
bool ok = false;
int i = p.toInt(&ok);
return ok ? i : def;
}
bool CSimpleCommandParser::toBool(int index, bool def) const
{
const QString p = this->part(index);
if (p.isEmpty()) { return def; }
const bool b = stringToBool(p);
return b;
}
double CSimpleCommandParser::toDouble(int index, double def) const
{
const QString p = this->part(index);
if (p.isEmpty()) { return def; }
bool ok;
bool ok = false;
double d = CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess);
return ok ? d : def;
}
bool CSimpleCommandParser::matchesPart(int index, const QString &toMatch, Qt::CaseSensitivity cs) const
{
if (toMatch.isEmpty()) { return false; }
if (!this->hasPart(index)) { return false; }
const QString p(this->part(index));
if (p.isEmpty()) { return false; }
return (p.length() == toMatch.length() && p.startsWith(toMatch, cs));
}
QString CSimpleCommandParser::removeLeadingDot(const QString &candidate)
{
if (!candidate.startsWith('.')) { return candidate; }
@@ -147,10 +169,9 @@ namespace BlackMisc
void CSimpleCommandParser::setCheckedCommandList(const QStringList &commands)
{
foreach(QString c, commands)
for (const QString &c : commands)
{
this->m_knownCommands.append(formatCommand(c));
}
}
} // namespace