Added ability to extract pattern groups from incomplete phone number.

This commit is contained in:
23rd 2021-08-27 17:28:00 +03:00
parent 10c8162575
commit 6ff0cb853d
3 changed files with 37 additions and 7 deletions

View file

@ -343,8 +343,27 @@ FormatResult CountriesInstance::format(FormatArgs args) {
return FormatResult{ .formatted = phoneNumber }; return FormatResult{ .formatted = phoneNumber };
} }
const auto formattedPart = phoneNumber.mid( const auto codeSize = int(bestCallingCodePtr->callingCode.size());
bestCallingCodePtr->callingCode.size());
if (args.onlyGroups && args.incomplete) {
auto groups = args.skipCode
? QVector<int>()
: QVector<int>{ 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 formattedResult = formattedPart;
auto groups = QVector<int>(); auto groups = QVector<int>();
auto maxMatchedDigits = size_t(0); auto maxMatchedDigits = size_t(0);
@ -408,7 +427,7 @@ FormatResult CountriesInstance::format(FormatArgs args) {
if (!args.skipCode) { if (!args.skipCode) {
if (args.onlyGroups) { if (args.onlyGroups) {
groups.push_front(bestCallingCodePtr->callingCode.size()); groups.push_front(codeSize);
} else { } else {
formattedResult = '+' formattedResult = '+'
+ bestCallingCodePtr->callingCode + bestCallingCodePtr->callingCode

View file

@ -34,6 +34,7 @@ struct FormatArgs {
QString phone; QString phone;
bool onlyGroups = false; bool onlyGroups = false;
bool skipCode = false; bool skipCode = false;
bool incomplete = false;
}; };
class CountriesInstance final { class CountriesInstance final {

View file

@ -9,7 +9,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "countries/countries_instance.h" // Countries::ValidPhoneCode #include "countries/countries_instance.h" // Countries::ValidPhoneCode
#include "numbers.h"
#include <QtCore/QRegularExpression> #include <QtCore/QRegularExpression>
@ -205,7 +204,11 @@ void PhonePartInput::addedToNumber(const QString &added) {
} }
void PhonePartInput::chooseCode(const QString &code) { 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()) { if (!_pattern.isEmpty() && _pattern.at(0) == code.size()) {
_pattern.pop_front(); _pattern.pop_front();
} else { } else {
@ -284,7 +287,10 @@ void UsernameInput::correctValue(
} }
QString ExtractPhonePrefix(const QString &phone) { QString ExtractPhonePrefix(const QString &phone) {
const auto pattern = phoneNumberParse(phone); const auto pattern = Countries::Instance().format({
.phone = phone,
.onlyGroups = true,
}).groups;
if (!pattern.isEmpty()) { if (!pattern.isEmpty()) {
return phone.mid(0, pattern[0]); return phone.mid(0, pattern[0]);
} }
@ -343,7 +349,11 @@ void PhoneInput::correctValue(
int &nowCursor) { int &nowCursor) {
auto digits = now; auto digits = now;
digits.replace(QRegularExpression("[^\\d]"), QString()); digits.replace(QRegularExpression("[^\\d]"), QString());
_pattern = phoneNumberParse(digits); _pattern = Countries::Instance().format({
.phone = digits,
.onlyGroups = true,
.incomplete = true,
}).groups;
QString newPlaceholder; QString newPlaceholder;
if (_pattern.isEmpty()) { if (_pattern.isEmpty()) {