From 6ff0cb853d5c1b65c0f1b9bd0c1fb35d230d2c42 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 27 Aug 2021 17:28:00 +0300 Subject: [PATCH] Added ability to extract pattern groups from incomplete phone number. --- .../countries/countries_instance.cpp | 25 ++++++++++++++++--- .../countries/countries_instance.h | 1 + Telegram/SourceFiles/ui/special_fields.cpp | 18 ++++++++++--- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Telegram/SourceFiles/countries/countries_instance.cpp b/Telegram/SourceFiles/countries/countries_instance.cpp index 60ae2100d..218c5ba1d 100644 --- a/Telegram/SourceFiles/countries/countries_instance.cpp +++ b/Telegram/SourceFiles/countries/countries_instance.cpp @@ -343,8 +343,27 @@ FormatResult CountriesInstance::format(FormatArgs args) { return FormatResult{ .formatted = phoneNumber }; } - const auto formattedPart = phoneNumber.mid( - bestCallingCodePtr->callingCode.size()); + const auto codeSize = int(bestCallingCodePtr->callingCode.size()); + + if (args.onlyGroups && args.incomplete) { + auto groups = args.skipCode + ? QVector() + : QVector{ codeSize }; + auto groupSize = 0; + for (const auto &c : bestCallingCodePtr->patterns.front()) { + if (c == ' ') { + groups.push_back(base::take(groupSize)); + } else { + groupSize++; + } + } + if (groupSize) { + groups.push_back(base::take(groupSize)); + } + return FormatResult{ .groups = std::move(groups) }; + } + + const auto formattedPart = phoneNumber.mid(codeSize); auto formattedResult = formattedPart; auto groups = QVector(); auto maxMatchedDigits = size_t(0); @@ -408,7 +427,7 @@ FormatResult CountriesInstance::format(FormatArgs args) { if (!args.skipCode) { if (args.onlyGroups) { - groups.push_front(bestCallingCodePtr->callingCode.size()); + groups.push_front(codeSize); } else { formattedResult = '+' + bestCallingCodePtr->callingCode diff --git a/Telegram/SourceFiles/countries/countries_instance.h b/Telegram/SourceFiles/countries/countries_instance.h index a0875ca46..7c7e104e2 100644 --- a/Telegram/SourceFiles/countries/countries_instance.h +++ b/Telegram/SourceFiles/countries/countries_instance.h @@ -34,6 +34,7 @@ struct FormatArgs { QString phone; bool onlyGroups = false; bool skipCode = false; + bool incomplete = false; }; class CountriesInstance final { diff --git a/Telegram/SourceFiles/ui/special_fields.cpp b/Telegram/SourceFiles/ui/special_fields.cpp index ba474855c..dda8a16fe 100644 --- a/Telegram/SourceFiles/ui/special_fields.cpp +++ b/Telegram/SourceFiles/ui/special_fields.cpp @@ -9,7 +9,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "countries/countries_instance.h" // Countries::ValidPhoneCode -#include "numbers.h" #include @@ -205,7 +204,11 @@ void PhonePartInput::addedToNumber(const QString &added) { } void PhonePartInput::chooseCode(const QString &code) { - _pattern = phoneNumberParse(code); + _pattern = Countries::Instance().format({ + .phone = code, + .onlyGroups = true, + .incomplete = true, + }).groups; if (!_pattern.isEmpty() && _pattern.at(0) == code.size()) { _pattern.pop_front(); } else { @@ -284,7 +287,10 @@ void UsernameInput::correctValue( } QString ExtractPhonePrefix(const QString &phone) { - const auto pattern = phoneNumberParse(phone); + const auto pattern = Countries::Instance().format({ + .phone = phone, + .onlyGroups = true, + }).groups; if (!pattern.isEmpty()) { return phone.mid(0, pattern[0]); } @@ -343,7 +349,11 @@ void PhoneInput::correctValue( int &nowCursor) { auto digits = now; digits.replace(QRegularExpression("[^\\d]"), QString()); - _pattern = phoneNumberParse(digits); + _pattern = Countries::Instance().format({ + .phone = digits, + .onlyGroups = true, + .incomplete = true, + }).groups; QString newPlaceholder; if (_pattern.isEmpty()) {