Added support of multiple phone pattern groups.

This commit is contained in:
23rd
2022-12-28 18:09:03 +03:00
parent be8aeb0d96
commit 8748265b00
3 changed files with 75 additions and 26 deletions

View File

@@ -356,24 +356,55 @@ FormatResult CountriesInstance::format(FormatArgs args) {
const auto codeSize = int(bestCallingCodePtr->callingCode.size());
if (args.onlyGroups && args.incomplete) {
auto groups = args.skipCode
auto initialGroups = args.skipCode
? QVector<int>()
: QVector<int>{ codeSize };
auto groupSize = 0;
auto initialGroupsSize = 0;
if (bestCallingCodePtr->patterns.empty()) {
return FormatResult{ .groups = std::move(groups) };
return FormatResult{ .groups = std::move(initialGroups) };
}
for (const auto &c : bestCallingCodePtr->patterns.front()) {
if (c == ' ') {
groups.push_back(base::take(groupSize));
} else {
groupSize++;
auto bestGroups = initialGroups;
auto bestGroupsSize = initialGroupsSize;
auto bestPatternMaxMatches = -1;
for (const auto &pattern : bestCallingCodePtr->patterns) {
auto groups = initialGroups;
auto groupSize = initialGroupsSize;
auto lastSpacesCount = 0;
auto maxMatchedDigits = 0;
auto isNotBestPattern = false;
for (auto i = 0; i < pattern.size(); i++) {
const auto c = pattern.at(i);
if (c.isDigit()) {
const auto n = (i - lastSpacesCount) + codeSize;
if (n < phoneNumber.size()) {
if (phoneNumber.at(n) == c) {
maxMatchedDigits++;
} else {
isNotBestPattern = true;
}
} else {
isNotBestPattern = true;
}
}
if (c.isSpace()) {
groups.push_back(base::take(groupSize));
lastSpacesCount++;
} else {
groupSize++;
}
}
if (maxMatchedDigits > bestPatternMaxMatches) {
bestPatternMaxMatches = isNotBestPattern
? -1
: maxMatchedDigits;
bestGroups = std::move(groups);
bestGroupsSize = groupSize;
}
}
if (groupSize) {
groups.push_back(base::take(groupSize));
if (bestGroupsSize) {
bestGroups.push_back(base::take(bestGroupsSize));
}
return FormatResult{ .groups = std::move(groups) };
return FormatResult{ .groups = std::move(bestGroups) };
}
const auto formattedPart = phoneNumber.mid(codeSize);