| 339 | } |
| 340 | |
| 341 | FormatResult CountriesInstance::format(FormatArgs args) const { |
| 342 | // Ported from TDLib. |
| 343 | if (args.phone.isEmpty()) { |
| 344 | return FormatResult(); |
| 345 | } |
| 346 | const auto &phoneNumber = args.phone; |
| 347 | |
| 348 | const Info *bestCountryPtr = nullptr; |
| 349 | const CallingCodeInfo *bestCallingCodePtr = nullptr; |
| 350 | auto bestLength = size_t(0); |
| 351 | [[maybe_unused]] auto isPrefix = false; |
| 352 | for (const auto &country : list()) { |
| 353 | for (auto &callingCode : country.codes) { |
| 354 | if (phoneNumber.startsWith(callingCode.callingCode)) { |
| 355 | const auto codeSize = callingCode.callingCode.size(); |
| 356 | for (const auto &prefix : callingCode.prefixes) { |
| 357 | if (prefix.startsWith(base::StringViewMid(phoneNumber, codeSize))) { |
| 358 | isPrefix = true; |
| 359 | } |
| 360 | if ((codeSize + prefix.size()) > bestLength && |
| 361 | base::StringViewMid(phoneNumber, codeSize).startsWith(prefix)) { |
| 362 | bestCountryPtr = &country; |
| 363 | bestCallingCodePtr = &callingCode; |
| 364 | bestLength = codeSize + prefix.size(); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | if (callingCode.callingCode.startsWith(phoneNumber)) { |
| 369 | isPrefix = true; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | if (bestCountryPtr == nullptr) { |
| 374 | return FormatResult{ .formatted = phoneNumber }; |
| 375 | } |
| 376 | if (args.onlyCode) { |
| 377 | return FormatResult{ .code = bestCallingCodePtr->callingCode }; |
| 378 | } |
| 379 | |
| 380 | const auto codeSize = int(bestCallingCodePtr->callingCode.size()); |
| 381 | |
| 382 | if (args.onlyGroups && args.incomplete) { |
| 383 | auto initialGroups = args.skipCode |
| 384 | ? QVector<int>() |
| 385 | : QVector<int>{ codeSize }; |
| 386 | auto initialGroupsSize = 0; |
| 387 | if (bestCallingCodePtr->patterns.empty()) { |
| 388 | return FormatResult{ .groups = std::move(initialGroups) }; |
| 389 | } |
| 390 | auto bestGroups = initialGroups; |
| 391 | auto bestGroupsSize = initialGroupsSize; |
| 392 | auto bestPatternMaxMatches = -1; |
| 393 | for (const auto &pattern : bestCallingCodePtr->patterns) { |
| 394 | auto groups = initialGroups; |
| 395 | auto groupSize = initialGroupsSize; |
| 396 | auto lastSpacesCount = 0; |
| 397 | auto maxMatchedDigits = 0; |
| 398 | auto isNotBestPattern = false; |
no test coverage detected