forked from ciphrex/mSIGNA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordlistvalidator.cpp
87 lines (74 loc) · 2.1 KB
/
wordlistvalidator.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
///////////////////////////////////////////////////////////////////////////////
//
// mSIGNA
//
// wordlistvalidator.cpp
//
// Copyright (c) 2015 Eric Lombrozo
//
// All Rights Reserved.
#include "wordlistvalidator.h"
#include <string>
WordlistValidator::WordlistValidator(int minlen, int maxlen, QObject* parent) :
QValidator(parent), minlen_(minlen), maxlen_(maxlen)
{
}
QValidator::State WordlistValidator::validate(QString& input, int& pos) const
{
if (input.isEmpty()) return Intermediate;
input = input.toLower();
// Backspace over spaces if we're not at the end.
if ((pos < input.size()) && (input.size() + 1 == lastInput.size()) && (lastInput[pos] == ' '))
{
QString temp = input.left(pos) + ' ' + input.right(input.size() - pos);
if (temp == lastInput) input = lastInput;
}
QString newInput;
int newPos = pos;
std::string wordlist = input.toStdString();
bool final = true;
QString lastWord;
int i = 0;
for (auto c: wordlist)
{
i++;
if (c == ' ')
{
if (lastWord.isEmpty())
{
// Strip extra spaces
if (newPos > newInput.size()) newPos--;
}
else if (lastWord.size() >= minlen_)
{
newInput += lastWord + ' ';
lastWord.clear();
}
else if (i < (int)wordlist.size() && newPos > newInput.size())
{
// Remove words that are too short.
newPos = newInput.size() - 1;
lastWord.clear();
}
else
{
// Disallow adding a space that makes a word too short.
return Invalid;
}
}
else if (c >= 'a' && c <= 'z' && lastWord.size() < maxlen_)
{
lastWord += c;
}
else
{
return Invalid;
}
}
newInput += lastWord;
if (lastWord.size() < minlen_) final = false;
input = newInput;
pos = newPos;
lastInput = input;
return final ? Acceptable : Intermediate;
}