10#include "private/qobject_p.h"
11#include "private/qlocale_p.h"
12#include "private/qnumeric_p.h"
13#include "private/qstringiterator_p.h"
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
88
89
90
91
92
93
94
95
96
97
100
101
102
103
104
107
108
109
110
111
112
113
116
117
118
119
120
121
122
125
126
127
128
129
130
131
134
135
136
137
138
139
140
143
144
145
146
147
148
151
152
153
154
155
156
157
158
159
161class QValidatorPrivate :
public QObjectPrivate{
162 Q_DECLARE_PUBLIC(QValidator)
164 QValidatorPrivate() : QObjectPrivate()
173
174
175
177QValidator::QValidator(QObject * parent)
178 : QValidator(*
new QValidatorPrivate, parent)
183
184
185
187QValidator::~QValidator()
192
193
194
195
196
197QLocale QValidator::locale()
const
199 Q_D(
const QValidator);
204
205
206
207
208
209
210
211void QValidator::setLocale(
const QLocale &locale)
214 if (d->locale != locale) {
221
222
223
224
225
226
227
228
229
230
231
232
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
252void QValidator::fixup(QString &)
const
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
294
295
296
298QIntValidator::QIntValidator(QObject * parent)
299 : QIntValidator(INT_MIN, INT_MAX, parent)
305
306
307
309QIntValidator::QIntValidator(
int minimum,
int maximum,
319
320
322QIntValidator::~QIntValidator()
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
357 while (bound >= factor)
359 return value >= factor;
362template <
typename T>
static inline
364 const QLocaleData::ParsingResult &result)
367 using ParsingResult = QLocaleData::ParsingResult;
368 if (result.state == ParsingResult::Invalid)
369 return QValidator::Invalid;
371 const QLocaleData::CharBuff &buff = result.buff;
373 return QValidator::Intermediate;
376 const bool signConflicts = (min >= 0 && ch ==
'-') || (max < 0 && ch ==
'+');
378 return QValidator::Invalid;
380 if (result.state == ParsingResult::Intermediate)
381 return QValidator::Intermediate;
386QValidator::State QIntValidator::validate(QString & input,
int&)
const
388 QLocaleData::ParsingResult result =
389 locale().d->m_data->validateChars(input, QLocaleData::IntegerMode, -1,
390 locale().numberOptions());
392 std::optional<State> opt = initialResultCheck(b, t, result);
396 const QLocaleData::CharBuff &buff = result.buff;
397 QSimpleParsedNumber r = QLocaleData::bytearrayToLongLong(buff, 10);
401 qint64 entered = r.result;
402 if (entered >= b && entered <= t) {
404 locale().toInt(input, &ok);
405 return ok ? Acceptable : Intermediate;
414 int buffLength = buff.size();
417 const int tLength = t != 0 ?
static_cast<
int>(std::log10(qAbs(t))) + 1 : 1;
419 return (entered > t && -entered < b && buffLength > tLength) ? Invalid : Intermediate;
421 return (entered < b) ? Invalid : Intermediate;
426void QIntValidator::fixup(QString &input)
const
428 auto [parseState, buff] =
429 locale().d->m_data->validateChars(input, QLocaleData::IntegerMode, -1,
430 locale().numberOptions());
431 if (parseState == QLocaleData::ParsingResult::Invalid)
434 QSimpleParsedNumber r = QLocaleData::bytearrayToLongLong(buff, 10);
436 input = locale().toString(r.result);
440
441
442
444void QIntValidator::setRange(
int bottom,
int top)
446 bool rangeChanged =
false;
450 emit bottomChanged(b);
465
466
467
468
469
470
471
472
473void QIntValidator::setBottom(
int bottom)
475 setRange(bottom, top());
479
480
481
482
483
484
485
486
487void QIntValidator::setTop(
int top)
489 setRange(bottom(), top);
493
494
495QValidator::QValidator(QObjectPrivate &d, QObject *parent)
501
502
503QValidator::QValidator(QValidatorPrivate &d, QObject *parent)
510 Q_DECLARE_PUBLIC(QDoubleValidator)
522 const QLocale &locale)
const;
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
579
580
581
583QDoubleValidator::QDoubleValidator(QObject *parent)
584 : QDoubleValidator(-HUGE_VAL, HUGE_VAL, -1, parent)
590
591
592
593
595QDoubleValidator::QDoubleValidator(
double bottom,
double top,
int decimals,
597 : QValidator(*
new QDoubleValidatorPrivate , parent)
606
607
609QDoubleValidator::~QDoubleValidator()
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
637# define LLONG_MAX Q_INT64_C(0x7fffffffffffffff
)
640QValidator::State QDoubleValidator::validate(QString & input,
int &)
const
642 Q_D(
const QDoubleValidator);
644 QLocaleData::NumberMode numMode = QLocaleData::DoubleStandardMode;
645 switch (d->notation) {
646 case StandardNotation:
647 numMode = QLocaleData::DoubleStandardMode;
649 case ScientificNotation:
650 numMode = QLocaleData::DoubleScientificMode;
654 return d->validateWithLocale(input, numMode, locale());
657QValidator::State
QDoubleValidatorPrivate::validateWithLocale(QString &input, QLocaleData::NumberMode numMode,
const QLocale &locale)
const
659 Q_Q(
const QDoubleValidator);
660 QLocaleData::ParsingResult result =
661 locale.d->m_data->validateChars(input, numMode, q->dec, locale.numberOptions());
663 std::optional<QValidator::State> opt = initialResultCheck(q->b, q->t, result);
668 double i = locale.toDouble(input, &ok);
669 Q_ASSERT(!qIsNaN(i));
671 return QValidator::Intermediate;
673 if (i >= q->b && i <= q->t)
674 return QValidator::Acceptable;
676 if (notation == QDoubleValidator::StandardNotation) {
677 const double max = qMax(qAbs(q->b), qAbs(q->t));
678 if (hasMoreIntegerDigits(qAbs(i), max))
679 return QValidator::Invalid;
682 return QValidator::Intermediate;
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715void QDoubleValidator::fixup(QString &input)
const
717 Q_D(
const QDoubleValidator);
718 const auto numberMode = d->notation == StandardNotation ? QLocaleData::DoubleStandardMode
719 : QLocaleData::DoubleScientificMode;
721 d->fixupWithLocale(input, numberMode, locale());
725 const QLocale &locale)
const
727 Q_Q(
const QDoubleValidator);
730 auto [parseState, buff] =
731 locale.d->m_data->validateChars(input, numMode, -1, locale.numberOptions());
732 if (parseState == QLocaleData::ParsingResult::Invalid)
737 const double entered = QByteArrayView(buff).toDouble(&ok);
741 if (numMode == QLocaleData::DoubleStandardMode) {
745 const QString exp = locale.exponential();
746 bool preferUpper =
false;
747 QStringIterator scan(exp);
748 while (!preferUpper && scan.hasNext()) {
749 const char32_t ch = scan.next();
750 if (QChar::isUpper(ch))
754 mode = input.contains(exp.toLower()) ?
'e' :
'E';
756 mode = input.contains(exp.toUpper()) ?
'E' :
'e';
760 precision = QLocale::FloatingPointShortest;
763 const auto decimalPointIndex = buff.indexOf(
'.');
764 precision = decimalPointIndex >= 0 ? buff.size() - decimalPointIndex - 1 : 0;
766 auto eIndex = buff.indexOf(
'e');
770 eIndex = buff.size();
771 precision = eIndex - (buff.contains(
'.') ? 1 : 0)
772 - (buff[0] ==
'-' || buff[0] ==
'+' ? 1 : 0);
776 precision = qMin(precision, q->dec);
778 input = locale.toString(entered, mode, precision);
783
784
785
786
787
788
789
791void QDoubleValidator::setRange(
double minimum,
double maximum,
int decimals)
793 bool rangeChanged =
false;
797 emit bottomChanged(b);
806 if (dec != decimals) {
809 emit decimalsChanged(dec);
816
817
818
819
820
821void QDoubleValidator::setRange(
double minimum,
double maximum)
823 setRange(minimum, maximum, decimals());
827
828
829
830
831
832
833
835void QDoubleValidator::setBottom(
double bottom)
837 setRange(bottom, top(), decimals());
842
843
844
845
846
847
848
850void QDoubleValidator::setTop(
double top)
852 setRange(bottom(), top, decimals());
856
857
858
859
860
861
862
863
865void QDoubleValidator::setDecimals(
int decimals)
867 setRange(bottom(), top(), decimals);
871
872
873
874
875
876
877
878
880void QDoubleValidator::setNotation(Notation newNotation)
882 Q_D(QDoubleValidator);
883 if (d->notation != newNotation) {
884 d->notation = newNotation;
885 emit notationChanged(d->notation);
890QDoubleValidator::Notation QDoubleValidator::notation()
const
892 Q_D(
const QDoubleValidator);
896#if QT_CONFIG(regularexpression)
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
930class QRegularExpressionValidatorPrivate :
public QValidatorPrivate
932 Q_DECLARE_PUBLIC(QRegularExpressionValidator)
935 QRegularExpression origRe;
936 QRegularExpression usedRe;
937 void setRegularExpression(
const QRegularExpression &re);
941
942
943
945QRegularExpressionValidator::QRegularExpressionValidator(QObject *parent)
946 : QValidator(*
new QRegularExpressionValidatorPrivate, parent)
953
954
955
957QRegularExpressionValidator::QRegularExpressionValidator(
const QRegularExpression &re, QObject *parent)
958 : QRegularExpressionValidator(parent)
960 Q_D(QRegularExpressionValidator);
961 d->setRegularExpression(re);
966
967
969QRegularExpressionValidator::~QRegularExpressionValidator()
974
975
976
977
978
979
980
981
982
983
984
985
986
987
989QValidator::State QRegularExpressionValidator::validate(QString &input,
int &pos)
const
991 Q_D(
const QRegularExpressionValidator);
996 if (d->origRe.pattern().isEmpty())
999 const QRegularExpressionMatch m = d->usedRe.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
1002 }
else if (input.isEmpty() || m.hasPartialMatch()) {
1003 return Intermediate;
1011
1012
1013
1014
1015
1016
1018QRegularExpression QRegularExpressionValidator::regularExpression()
const
1020 Q_D(
const QRegularExpressionValidator);
1024void QRegularExpressionValidator::setRegularExpression(
const QRegularExpression &re)
1026 Q_D(QRegularExpressionValidator);
1027 d->setRegularExpression(re);
1031
1032
1033
1034
1035
1036void QRegularExpressionValidatorPrivate::setRegularExpression(
const QRegularExpression &re)
1038 Q_Q(QRegularExpressionValidator);
1041 usedRe = origRe = re;
1042 usedRe.setPattern(QRegularExpression::anchoredPattern(re.pattern()));
1043 emit q->regularExpressionChanged(re);
1052#include "moc_qvalidator.cpp"
void fixupWithLocale(QString &input, QLocaleData::NumberMode numMode, const QLocale &locale) const
static std::optional< QValidator::State > initialResultCheck(T min, T max, const QLocaleData::ParsingResult &result)
static bool hasMoreIntegerDigits(double value, double bound)