Added support of multiple phone pattern groups.
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user