MCPcopy Create free account
hub / github.com/TDesktop-x64/tdesktop / format

Method format

Telegram/SourceFiles/countries/countries_instance.cpp:341–514  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

339}
340
341FormatResult 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;

Callers 15

ConvertIconToBlackFunction · 0.45
GoodImageFormatAndSizeFunction · 0.45
LoadImageFromFileFunction · 0.45
EncodeWebpFunction · 0.45
ComputeImageColorFunction · 0.45
ExtractPhoneCodeFunction · 0.45
GroupsFunction · 0.45
ValidatePhotoImageFunction · 0.45
writeBackgroundFunction · 0.45
GoodStorageForFrameFunction · 0.45

Calls 5

FormatResultClass · 0.85
QStringClass · 0.50
isEmptyMethod · 0.45
sizeMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected