summaryrefslogtreecommitdiff
path: root/validators/CallsignValidator.cpp
blob: 5fbebfe4cc4470368dfc136cdd3558ab6a937bf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "CallsignValidator.hpp"

CallsignValidator::CallsignValidator (QObject * parent, bool allow_compound)
  : QValidator {parent}
  , re_ {allow_compound ? R"(^[A-Z0-9/]+$)" : R"(^[A-Z0-9]+$)"}
{
}

QString CallsignValidator::normalizeStoredInput (QString input, int max_length,
                                                 bool allow_compound)
{
  input = input.trimmed ();
  input.truncate (max_length);
  CallsignValidator validator {nullptr, allow_compound};
  auto pos = input.size ();
  if (Invalid == validator.validate (input, pos)) input.clear ();
  return input;
}

auto CallsignValidator::validate (QString& input, int& pos) const -> State
{
  input = input.toUpper ();
  while (input.size () && input[0].isSpace ())
    {
      input.remove (0, 1);
      if (pos > 0) --pos;
    }
  while (input.size () && input[input.size () - 1].isSpace ())
    {
      if (pos > input.size ()) --pos;
      input.chop (1);
    }
  auto match = re_.match (input, 0, QRegularExpression::PartialPreferCompleteMatch);
  if (match.hasMatch ()) return Acceptable;
  if (!input.size () || match.hasPartialMatch ()) return Intermediate;
  pos = input.size ();
  return Invalid;
}