Skip to content

Commit

Permalink
matchPrefix is not working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ea4k committed Dec 15, 2024
1 parent 5583e35 commit 6a36b41
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 91 deletions.
174 changes: 100 additions & 74 deletions src/callsign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
* *
*****************************************************************************/
#include "callsign.h"
#include "qforeach.h"

Callsign::Callsign(const QString &callsign, QObject *parent) : QObject{parent},
fullCall(callsign.toUpper()), homeAreaNumber(0), valid(false), prefValid(false)
{
//qDebug() << Q_FUNC_INFO << ": " << callsign;
qDebug() << Q_FUNC_INFO << ": " << callsign;
hostAreaNumberExist = false;
homeAreaNumberExist = false;

Expand All @@ -37,92 +38,124 @@ Callsign::Callsign(const QString &callsign, QObject *parent) : QObject{parent},

QRegularExpression prefnRE = prefixRegEx();
QRegularExpressionMatch matchPrefix = prefnRE.match(fullCall);


if (matchPrefix.isValid())
{
qDebug() << Q_FUNC_INFO << ": matchPrefix IS VALID";

qDebug() << Q_FUNC_INFO << ": " << (matchPrefix.regularExpression()).pattern();

for (int i = 0; i <= matchPrefix.lastCapturedIndex(); ++i) {
QString captured = matchPrefix.captured(i);
qDebug() << Q_FUNC_INFO << QString(" Captured at(#i): ").arg(i) << captured;
}
}
else
{
qDebug() << Q_FUNC_INFO << ": matchPrefix IS NOT VALID";
}
if (matchPrefix.hasPartialMatch())
{
qDebug() << Q_FUNC_INFO << ": matchPrefix HAS A PARTIAL MATCH ";
}
else
{
qDebug() << Q_FUNC_INFO << ": matchPrefix HAS NOT A PARTIAL MATCH ";
}
//bool hasHost = false;
clear();
if ( match.hasMatch() )
{
//qDebug() << Q_FUNC_INFO << " - 10";
qDebug() << Q_FUNC_INFO << " - 10";
//it is a valid callsign
valid = true;
prefValid = true;
fullCall = match.captured("fullcall");
//qDebug() << Q_FUNC_INFO << " - fullcall: " << fullCall;
if (match.captured("hostprefix").length()>0)
{
//qDebug() << Q_FUNC_INFO << " - 20";
hostFullPref = match.captured("hostprefix") + match.captured("hostareanumber");
hostPref = match.captured("hostprefix");
hostAreaNumber = match.captured("hostareanumber").toInt(&hostAreaNumberExist);
//qDebug() << Q_FUNC_INFO << " - hostFullPref : " << hostFullPref;
//qDebug() << Q_FUNC_INFO << " - hostPref : " << hostPref;
//qDebug() << Q_FUNC_INFO << " - hostAreaNumner : " << QString::number(hostAreaNumber);
//hasHost = true;
}
else if (match.captured("").length()>0)
{
//qDebug() << Q_FUNC_INFO << " - 30";
hostFullPref = match.captured("suffixprefix") + match.captured("suffixareanumber");
hostPref = match.captured("suffixprefix");
hostAreaNumber = match.captured("suffixareanumber").toInt(&hostAreaNumberExist);

//qDebug() << Q_FUNC_INFO << " - hostFullPref : " << hostFullPref;
//qDebug() << Q_FUNC_INFO << " - hostPref : " << hostPref;
//qDebug() << Q_FUNC_INFO << " - hostAreaNumner : " << QString::number(hostAreaNumber);

}
//qDebug() << Q_FUNC_INFO << " - 40";
homeFullCall = match.captured("homeprefix")+match.captured("homeareanumber")+match.captured("homesuffix");
homeFullPref = match.captured("homeprefix")+match.captured("homeareanumber");
hostFullPref = match.captured("hostprefix");
hostPref = match.captured("hostprefix");
hostAreaNumber = match.captured("hostareanumber").toInt(&hostAreaNumberExist);
if (!hostAreaNumberExist)
hostAreaNumber = -1;

qDebug() << Q_FUNC_INFO << " - 40";
homeFullCall = match.captured("homefullcall");
homeFullPref = match.captured("homefullprefix");
homePref = match.captured("homeprefix");
homeAreaNumber = match.captured("homeareanumber").toInt(&homeAreaNumberExist);
if (!homeAreaNumberExist)
homeAreaNumber = -1;
homeSuffix = match.captured("homesuffix");
//if (!hasHost)
//{
// hostPref = homePref;
// hostFullPref = homeFullPref;
// hostAreaNumber = homeAreaNumber;
//}

additionalSuffix = match.captured("suffix");

//qDebug() << Q_FUNC_INFO << " - homeFullCall : " << homeFullCall;
//qDebug() << Q_FUNC_INFO << " - homeFullPref : " << homeFullPref;
//qDebug() << Q_FUNC_INFO << " - homePref : " << homePref;
//qDebug() << Q_FUNC_INFO << " - homeAreaNumner : " << QString::number(homeAreaNumber);
//qDebug() << Q_FUNC_INFO << " - homeSuffix : " << homeSuffix;
//qDebug() << Q_FUNC_INFO << " - AdditionalSuffix : " << additionalSuffix;

additionalSuffix = match.captured("suffix");
}
else if (matchPrefix.hasMatch())
else if ( matchPrefix.hasMatch() )
{
//qDebug() << Q_FUNC_INFO << " - 50";
//return QString("^(?<homefullprefix>(?<homeprefix>[A-Z]{1,2}|F|G|I|K|M|N|R|U|W|[A-Z][0-9]|[0-9][A-Z]|)(?<homeareanumber>[0-9]+)?)");
qDebug() << Q_FUNC_INFO << " - 50";
qDebug() << Q_FUNC_INFO << " - Captured: " << matchPrefix.captured();

QString aux;
foreach(aux, matchPrefix.capturedTexts())
{
qDebug() << Q_FUNC_INFO << " - Captured texts : " << aux;
}

prefValid = true;
hostPref = matchPrefix.captured("homeprefix");
hostFullPref = matchPrefix.captured("homefullprefix"); // The prefix without the area number
homePref = hostPref;
homeFullPref = hostFullPref;
homeFullPref = matchPrefix.captured("homefullprefix");
homePref = matchPrefix.captured("homeprefix"); // The prefix without the area number

homeAreaNumber = matchPrefix.captured("homeareanumber").toInt(&homeAreaNumberExist); // Just the area number (optional)
if (!homeAreaNumberExist)
homeAreaNumber = -1;


//qDebug() << Q_FUNC_INFO << " - hostFullPref : " << hostFullPref;
//qDebug() << Q_FUNC_INFO << " - hostPref : " << hostPref;
//qDebug() << Q_FUNC_INFO << " - homeFullPref : " << homeFullPref;
//qDebug() << Q_FUNC_INFO << " - homePref : " << homePref;
//qDebug() << Q_FUNC_INFO << " - homeAreaNumner : " << QString::number(homeAreaNumber);
qDebug() << Q_FUNC_INFO << " - @ homeFullPref : " << homeFullPref;
qDebug() << Q_FUNC_INFO << " - @ homePref : " << homePref;
qDebug() << Q_FUNC_INFO << " - @ homeAreaNumbner : " << QString::number(homeAreaNumber);
}
else
{
//qDebug() << Q_FUNC_INFO << " - 60";
qDebug() << Q_FUNC_INFO << " - 60";
//it is an invalid callsign
fullCall = QString();
}

qDebug() << Q_FUNC_INFO << " - ************************************************* ";

qDebug() << Q_FUNC_INFO << " - fullCall : " << fullCall;
if (valid)
qDebug() << Q_FUNC_INFO << " - valid : TRUE";
else
qDebug() << Q_FUNC_INFO << " - valid : FALSE";

if (prefValid)
qDebug() << Q_FUNC_INFO << " - prefValid : TRUE";
else
qDebug() << Q_FUNC_INFO << " - prefValid : FALSE";

qDebug() << Q_FUNC_INFO << " - hostFullPref : " << hostFullPref;
qDebug() << Q_FUNC_INFO << " - hostPref : " << hostPref;
qDebug() << Q_FUNC_INFO << " - hostAreaNumner : " << QString::number(hostAreaNumber);


qDebug() << Q_FUNC_INFO << " - homeFullCall : " << homeFullCall;
qDebug() << Q_FUNC_INFO << " - homeFullPref : " << homeFullPref;
qDebug() << Q_FUNC_INFO << " - homePref : " << homePref;
qDebug() << Q_FUNC_INFO << " - homeAreaNumner : " << QString::number(homeAreaNumber);
qDebug() << Q_FUNC_INFO << " - homeSuffix : " << homeSuffix;

qDebug() << Q_FUNC_INFO << " - AdditionalSuffix : " << additionalSuffix;
qDebug() << Q_FUNC_INFO << " - ##################################################";
//qDebug() << Q_FUNC_INFO << " - 100";
}

Callsign::~Callsign(){}

QRegularExpression Callsign::callsignRegEx()
{
// Returns a REGEX that matches a hamradio callsign
return QRegularExpression(callsignRegExString(), QRegularExpression::CaseInsensitiveOption);
}

Expand All @@ -133,24 +166,17 @@ QRegularExpression Callsign::prefixRegEx()
}

QString Callsign::callsignRegExString()
{ // This suffix regex matches QRP, MM... but also prefixes for remote operations.
// Suffix: ((?<suffix>QRP|MM)|((?<prefix>[A-Z0-9]{1,2})(?<areanumber>[0-9]+)?))

// Base callsign
// ^(?<basecall>(?<baseprefixwithnumber>(?<baseprefix>[A-Z][0-9]|[A-Z]|[A-Z]{1,2}|[0-9][A-Z]|E)(?<areanumber>[0-9]+))(?<basesuffix>[A-Z]+))
// REGEX created with Gemini:
//return QString("^(?<fullcall>(?<hostprefix>(?:[FGKNRWUGMI]|[A-Z]{2}|\\d[A-Z]|[A-Z]\\d))?\\/?(?<hostareanumber>\\d+)?\\/?(?<homeprefix>(?:[FGKNRWUGMI]|[A-Z]{2}|\\d[A-Z]|[A-Z]\\d))(?<homeareanumber>\\d+)(?<homesuffix>[A-Z]{0,3}))(?:\\/?(?:(?<suffixprefix>(?:[FGKNRWUGMI]|[A-Z]{2}|\\d[A-Z]|[A-Z]\\d))(?<suffixareanumber>\\d+)?|(?<suffix>(?!(?:[FGKNRWUGMI]|[A-Z]{2}|\\d[A-Z]|[A-Z]\\d)\\d)[^\\/\n]+)))$");
// Host prefix /must end in /
//^(?<fullcall>(?<hostfullcall>((?<hostpref>[A-Z0-9]{1,2}(?<hostareanumber>[0-9]|[0-9]+)?)\/)?(?<homefullpref>(?<homepref>[A-Z][0-9]|[A-Z]{1,2}|[0-9][A-Z])(?<homeareanumber>[0-9]|[0-9]+))(?<homesuffix>[A-Z]+))(\/(?<additionalsuffix>[A-Z0-9]+))?)
return QString("^(?<fullcall>((?<hostfullcall>(?<hostfullprefix>(?<hostprefix>[A-Z0-9]{1,2})(?<hostareanumber>[0-9]|[0-9]+)?)\\/)?(?<homefullcall>(?<homefullprefix>(?<homeprefix>[A-Z][0-9]|[A-Z]{1,2}|[0-9][A-Z])(?<homeareanumber>[0-9]|[0-9]+))(?<homesuffix>[A-Z]+)))(\\/(?<additionalsuffix>[A-Z0-9]+))?)");
{
// This suffix regex matches QRP, MM... but also prefixes for remote operations.
// Returns a REGEX string that matches a hamradio callsign
//return QString("^(?<fullcall>((?<hostfullcall>(?<hostfullprefix>(?<hostprefix>[A-Z0-9]{1,2})(?<hostareanumber>[0-9]|[0-9]+)?)\\/)?(?<homefullcall>(?<homefullprefix>(?<homeprefix>[A-Z][0-9]|[A-Z]{1,2}|[0-9][A-Z])(?<homeareanumber>[0-9]|[0-9]+))(?<homesuffix>[A-Z]+)))(\\/(?<additionalsuffix>[A-Z0-9]+))?)");
return QString("^(?<fullcall>((?<hostfullcall>(?<hostfullprefix>(?<hostprefix>[A-Z0-9]{1,2})(?<hostareanumber>[0-9]+)?)\\/)?(?<homefullcall>(?<homefullprefix>(?<homeprefix>[A-Z][0-9]|[A-Z]{1,2}|[0-9][A-Z])(?<homeareanumber>[0-9]+))(?<homesuffix>[A-Z]+)))(\\/(?<additionalsuffix>[A-Z0-9]+))?)");
}

QString Callsign::prefixRegExString()
{
//qDebug() << Q_FUNC_INFO;

// Matches prefix: ^(?<homefullprefix>(?<homeprefix>[A-Z]{1,2}|F|G|I|K|M|N|R|U|W|[A-Z][0-9]|[0-9][A-Z]|)(?<homeareanumber>[0-9]+)?)

// Returns a REGEX string that matches a hamradio prefix
return QString("^(?<homefullprefix>(?<homeprefix>[A-Z]{1,2}|F|G|I|K|M|N|R|U|W|[A-Z][0-9]|[0-9][A-Z]|)(?<homeareanumber>[0-9]+)?)");
}

Expand Down Expand Up @@ -199,17 +225,17 @@ void Callsign::clear()
hostAreaNumber = -1;
hostAreaNumberExist = false;

homeFullCall.clear(); // EA4K
homeFullPref.clear(); // EA4
homePref.clear(); // EA
homeAreaNumber = -1; // 4
homeFullCall.clear(); // EA4K
homeFullPref.clear(); // EA4
homePref.clear(); // EA
homeAreaNumber = -1; // 4
homeAreaNumberExist = false;
homeSuffix.clear();

additionalSuffix.clear();

valid = false; // The entered string is a correct callsign
prefValid = false; // The entered string is a correct prefix
valid = false; // The entered string is a correct callsign
prefValid = false; // The entered string is a correct prefix
}

// Based on wiki information
Expand Down
60 changes: 43 additions & 17 deletions tests/tst_callsign/tst_callsign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void tst_Callsign::test_prefixes_data()
QTest::newRow("EA4K") << "EA4K" << "" << "" << "" << -1 << "EA4K" << "EA4" << "EA" << 4 << "K" << "" << true << true;
QTest::newRow("EA4K/P") << "EA4K/P" << "" << "" << "" << -1 << "EA4K" << "EA4" << "EA" << 4 << "K" << "P" << true << true;
QTest::newRow("2E4K") << "2E4K" << "" << "" << "" << -1 << "2E4K" << "2E4" << "2E" << 4 << "K" << "" << true << true;
QTest::newRow("AM100") << "" << "" << "" << "" << -1 << "" << "AM100" << "AM" << 100 << "" << "" << true << false;
QTest::newRow("AM100") << "" << "" << "" << "" << -1 << "" << "AM100" << "AM" << 100 << "" << "" << true << false;

//QTest::newRow("AM100") << "AM100" << "AM100" << "AM" << 100; // AM100
//QTest::newRow("K1") << "K1" << "K1" << "K" << 1; // K1
Expand All @@ -133,33 +133,59 @@ void tst_Callsign::test_prefixes()
QFETCH(QString, fullcall);
QFETCH(QString, hostfullprefix);
QFETCH(QString, hostprefix);
QFETCH(int, hostareanumber);
QFETCH(QString, homefullprefix);
QFETCH(QString, homeprefix);
QFETCH(int, hostareanumber);
QFETCH(int, homeareanumber);
QFETCH(bool, isValidPrefix);
QFETCH(bool, isValid);

Callsign testCall(fullcall);

qDebug() << Q_FUNC_INFO << " - fullcall : " << fullcall;
qDebug() << Q_FUNC_INFO << " - hostfullprefix : " << hostfullprefix;
qDebug() << Q_FUNC_INFO << " - hostprefix : " << hostprefix;
qDebug() << Q_FUNC_INFO << " - hostareanumber : " << QString::number(hostareanumber);
//qDebug() << Q_FUNC_INFO << " - hostfullprefix : " << hostfullprefix;
//qDebug() << Q_FUNC_INFO << " - hostprefix : " << hostprefix;
//qDebug() << Q_FUNC_INFO << " - hostareanumber : " << QString::number(hostareanumber);

qDebug() << Q_FUNC_INFO << " - isValidPrefix";
QCOMPARE(testCall.isValidPrefix(), isValidPrefix);
qDebug() << Q_FUNC_INFO << " - isValid";
QCOMPARE(testCall.isValid(), isValid);
qDebug() << Q_FUNC_INFO << " - fullcall : " << testCall.getCallsign() << "/" << fullcall;
QCOMPARE(testCall.getCallsign(), fullcall);
qDebug() << Q_FUNC_INFO << " - hostfullprefix:" << testCall.getHostFullPrefix() << "/" << hostfullprefix;
QCOMPARE(testCall.getHostFullPrefix(), hostfullprefix);
qDebug() << Q_FUNC_INFO << " - hostprefix :" << testCall.getHostPrefix() << "/" << hostprefix;
QCOMPARE(testCall.getHostPrefix(), hostprefix);
qDebug() << Q_FUNC_INFO << " - hostareanumber:" << QString::number(testCall.getHostAreaNumber()) << "/" << QString::number(hostareanumber);
QCOMPARE(testCall.getHostAreaNumber(), hostareanumber);
qDebug() << Q_FUNC_INFO << " - homefullprefix :" << testCall.getHomeFullPrefix() << "/" << homefullprefix;
QCOMPARE(testCall.getHomeFullPrefix(), homefullprefix);
qDebug() << Q_FUNC_INFO << " - homeprefix :" << testCall.getHomePrefix() << "/" << homeprefix;
QCOMPARE(testCall.getHomePrefix(), homeprefix);


if (isValid)
{
qDebug() << Q_FUNC_INFO << " - fullcall : " << testCall.getCallsign() << "/" << fullcall;
QCOMPARE(testCall.getCallsign(), fullcall);

qDebug() << Q_FUNC_INFO << " - hostfullprefix:" << testCall.getHostFullPrefix() << "/" << hostfullprefix;
QCOMPARE(testCall.getHostFullPrefix(), hostfullprefix);

qDebug() << Q_FUNC_INFO << " - hostprefix :" << testCall.getHostPrefix() << "/" << hostprefix;
QCOMPARE(testCall.getHostPrefix(), hostprefix);

qDebug() << Q_FUNC_INFO << " - hostareanumber:" << QString::number(testCall.getHostAreaNumber()) << "/" << QString::number(hostareanumber);
QCOMPARE(testCall.getHostAreaNumber(), hostareanumber);

qDebug() << Q_FUNC_INFO << " - homefullprefix :" << testCall.getHomeFullPrefix() << "/" << homefullprefix;
QCOMPARE(testCall.getHomeFullPrefix(), homefullprefix);

qDebug() << Q_FUNC_INFO << " - homeprefix :" << testCall.getHomePrefix() << "/" << homeprefix;
QCOMPARE(testCall.getHomePrefix(), homeprefix);
}
if (isValidPrefix)
{
qDebug() << Q_FUNC_INFO << " - homefullprefix :" << testCall.getHomeFullPrefix() << "/" << homefullprefix;
QCOMPARE(testCall.getHomeFullPrefix(), homefullprefix);

qDebug() << Q_FUNC_INFO << " - homeprefix :" << testCall.getHomePrefix() << "/" << homeprefix;
QCOMPARE(testCall.getHomePrefix(), homeprefix);

qDebug() << Q_FUNC_INFO << " - homeareanumber:" << QString::number(testCall.getHomeAreaNumber()) << "/" << QString::number(homeareanumber);
QCOMPARE(testCall.getHomeAreaNumber(), hostareanumber);
}




//QCOMPARE(pref.getHostPrefixWithoutNumber(), prefix);
Expand Down

0 comments on commit 6a36b41

Please sign in to comment.