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
356 return (
int)
std::log10(
double(n)) + 1;
361 qlonglong result = 1;
362 for (
int i = 0; i < exp; ++i)
367template <
typename T>
static inline
369 const QLocaleData::ParsingResult &result)
372 using ParsingResult = QLocaleData::ParsingResult;
373 if (result.state == ParsingResult::Invalid)
374 return QValidator::Invalid;
376 const QLocaleData::CharBuff &buff = result.buff;
378 return QValidator::Intermediate;
381 const bool signConflicts = (min >= 0 && ch ==
'-') || (max < 0 && ch ==
'+');
383 return QValidator::Invalid;
385 if (result.state == ParsingResult::Intermediate)
386 return QValidator::Intermediate;
391QValidator::State QIntValidator::validate(QString & input,
int&)
const
393 QLocaleData::ParsingResult result =
394 locale().d->m_data->validateChars(input, QLocaleData::IntegerMode, -1,
395 locale().numberOptions());
397 std::optional<State> opt = initialResultCheck(b, t, result);
401 const QLocaleData::CharBuff &buff = result.buff;
402 QSimpleParsedNumber r = QLocaleData::bytearrayToLongLong(buff, 10);
406 qint64 entered = r.result;
407 if (entered >= b && entered <= t) {
409 locale().toInt(input, &ok);
410 return ok ? Acceptable : Intermediate;
419 int buffLength = buff.size();
422 const int tLength = t != 0 ?
static_cast<
int>(std::log10(qAbs(t))) + 1 : 1;
424 return (entered > t && -entered < b && buffLength > tLength) ? Invalid : Intermediate;
426 return (entered < b) ? Invalid : Intermediate;
431void QIntValidator::fixup(QString &input)
const
433 auto [parseState, buff] =
434 locale().d->m_data->validateChars(input, QLocaleData::IntegerMode, -1,
435 locale().numberOptions());
436 if (parseState == QLocaleData::ParsingResult::Invalid)
439 QSimpleParsedNumber r = QLocaleData::bytearrayToLongLong(buff, 10);
441 input = locale().toString(r.result);
445
446
447
449void QIntValidator::setRange(
int bottom,
int top)
451 bool rangeChanged =
false;
455 emit bottomChanged(b);
470
471
472
473
474
475
476
477
478void QIntValidator::setBottom(
int bottom)
480 setRange(bottom, top());
484
485
486
487
488
489
490
491
492void QIntValidator::setTop(
int top)
494 setRange(bottom(), top);
498
499
500QValidator::QValidator(QObjectPrivate &d, QObject *parent)
506
507
508QValidator::QValidator(QValidatorPrivate &d, QObject *parent)
515 Q_DECLARE_PUBLIC(QDoubleValidator)
527 const QLocale &locale)
const;
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
584
585
586
588QDoubleValidator::QDoubleValidator(QObject *parent)
589 : QDoubleValidator(-HUGE_VAL, HUGE_VAL, -1, parent)
595
596
597
598
600QDoubleValidator::QDoubleValidator(
double bottom,
double top,
int decimals,
602 : QValidator(*
new QDoubleValidatorPrivate , parent)
611
612
614QDoubleValidator::~QDoubleValidator()
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
642# define LLONG_MAX Q_INT64_C(0x7fffffffffffffff
)
645QValidator::State QDoubleValidator::validate(QString & input,
int &)
const
647 Q_D(
const QDoubleValidator);
649 QLocaleData::NumberMode numMode = QLocaleData::DoubleStandardMode;
650 switch (d->notation) {
651 case StandardNotation:
652 numMode = QLocaleData::DoubleStandardMode;
654 case ScientificNotation:
655 numMode = QLocaleData::DoubleScientificMode;
659 return d->validateWithLocale(input, numMode, locale());
662QValidator::State
QDoubleValidatorPrivate::validateWithLocale(QString &input, QLocaleData::NumberMode numMode,
const QLocale &locale)
const
664 Q_Q(
const QDoubleValidator);
665 QLocaleData::ParsingResult result =
666 locale.d->m_data->validateChars(input, numMode, q->dec, locale.numberOptions());
668 std::optional<QValidator::State> opt = initialResultCheck(q->b, q->t, result);
673 double i = locale.toDouble(input, &ok);
674 Q_ASSERT(!qIsNaN(i));
676 return QValidator::Intermediate;
678 if (i >= q->b && i <= q->t)
679 return QValidator::Acceptable;
681 if (notation == QDoubleValidator::StandardNotation) {
682 double max = qMax(qAbs(q->b), qAbs(q->t));
688 if (convertDoubleTo(qFloor(max), &v)) {
689 qlonglong n = pow10(numDigits(v));
698 if (qAbs(i) > (n - std::pow(10, -q->dec)))
699 return QValidator::Invalid;
703 return QValidator::Intermediate;
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736void QDoubleValidator::fixup(QString &input)
const
738 Q_D(
const QDoubleValidator);
739 const auto numberMode = d->notation == StandardNotation ? QLocaleData::DoubleStandardMode
740 : QLocaleData::DoubleScientificMode;
742 d->fixupWithLocale(input, numberMode, locale());
746 const QLocale &locale)
const
748 Q_Q(
const QDoubleValidator);
751 auto [parseState, buff] =
752 locale.d->m_data->validateChars(input, numMode, -1, locale.numberOptions());
753 if (parseState == QLocaleData::ParsingResult::Invalid)
758 const double entered = QByteArrayView(buff).toDouble(&ok);
762 if (numMode == QLocaleData::DoubleStandardMode) {
766 const QString exp = locale.exponential();
767 bool preferUpper =
false;
768 QStringIterator scan(exp);
769 while (!preferUpper && scan.hasNext()) {
770 const char32_t ch = scan.next();
771 if (QChar::isUpper(ch))
775 mode = input.contains(exp.toLower()) ?
'e' :
'E';
777 mode = input.contains(exp.toUpper()) ?
'E' :
'e';
781 precision = QLocale::FloatingPointShortest;
784 const auto decimalPointIndex = buff.indexOf(
'.');
785 precision = decimalPointIndex >= 0 ? buff.size() - decimalPointIndex - 1 : 0;
787 auto eIndex = buff.indexOf(
'e');
791 eIndex = buff.size();
792 precision = eIndex - (buff.contains(
'.') ? 1 : 0)
793 - (buff[0] ==
'-' || buff[0] ==
'+' ? 1 : 0);
797 precision = qMin(precision, q->dec);
799 input = locale.toString(entered, mode, precision);
804
805
806
807
808
809
810
812void QDoubleValidator::setRange(
double minimum,
double maximum,
int decimals)
814 bool rangeChanged =
false;
818 emit bottomChanged(b);
827 if (dec != decimals) {
830 emit decimalsChanged(dec);
837
838
839
840
841
842void QDoubleValidator::setRange(
double minimum,
double maximum)
844 setRange(minimum, maximum, decimals());
848
849
850
851
852
853
854
856void QDoubleValidator::setBottom(
double bottom)
858 setRange(bottom, top(), decimals());
863
864
865
866
867
868
869
871void QDoubleValidator::setTop(
double top)
873 setRange(bottom(), top, decimals());
877
878
879
880
881
882
883
884
886void QDoubleValidator::setDecimals(
int decimals)
888 setRange(bottom(), top(), decimals);
892
893
894
895
896
897
898
899
901void QDoubleValidator::setNotation(Notation newNotation)
903 Q_D(QDoubleValidator);
904 if (d->notation != newNotation) {
905 d->notation = newNotation;
906 emit notationChanged(d->notation);
911QDoubleValidator::Notation QDoubleValidator::notation()
const
913 Q_D(
const QDoubleValidator);
917#if QT_CONFIG(regularexpression)
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
951class QRegularExpressionValidatorPrivate :
public QValidatorPrivate
953 Q_DECLARE_PUBLIC(QRegularExpressionValidator)
956 QRegularExpression origRe;
957 QRegularExpression usedRe;
958 void setRegularExpression(
const QRegularExpression &re);
962
963
964
966QRegularExpressionValidator::QRegularExpressionValidator(QObject *parent)
967 : QValidator(*
new QRegularExpressionValidatorPrivate, parent)
974
975
976
978QRegularExpressionValidator::QRegularExpressionValidator(
const QRegularExpression &re, QObject *parent)
979 : QRegularExpressionValidator(parent)
981 Q_D(QRegularExpressionValidator);
982 d->setRegularExpression(re);
987
988
990QRegularExpressionValidator::~QRegularExpressionValidator()
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1010QValidator::State QRegularExpressionValidator::validate(QString &input,
int &pos)
const
1012 Q_D(
const QRegularExpressionValidator);
1017 if (d->origRe.pattern().isEmpty())
1020 const QRegularExpressionMatch m = d->usedRe.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
1023 }
else if (input.isEmpty() || m.hasPartialMatch()) {
1024 return Intermediate;
1032
1033
1034
1035
1036
1037
1039QRegularExpression QRegularExpressionValidator::regularExpression()
const
1041 Q_D(
const QRegularExpressionValidator);
1045void QRegularExpressionValidator::setRegularExpression(
const QRegularExpression &re)
1047 Q_D(QRegularExpressionValidator);
1048 d->setRegularExpression(re);
1052
1053
1054
1055
1056
1057void QRegularExpressionValidatorPrivate::setRegularExpression(
const QRegularExpression &re)
1059 Q_Q(QRegularExpressionValidator);
1062 usedRe = origRe = re;
1063 usedRe.setPattern(QRegularExpression::anchoredPattern(re.pattern()));
1064 emit q->regularExpressionChanged(re);
1073#include "moc_qvalidator.cpp"
void fixupWithLocale(QString &input, QLocaleData::NumberMode numMode, const QLocale &locale) const
static qlonglong pow10(int exp)
static std::optional< QValidator::State > initialResultCheck(T min, T max, const QLocaleData::ParsingResult &result)
static int numDigits(qlonglong n)