From c5a2105b614ea1d3e2acf073a0f47f37e568e348 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 30 Aug 2021 17:11:45 +0300 Subject: [PATCH] Improve getPhoneNumberInfo. --- td/generate/scheme/td_api.tl | 2 +- td/telegram/CountryInfoManager.cpp | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 908a55c9d..e5d469b46 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -1302,7 +1302,7 @@ countries countries:vector = Countries; //@description Contains information about a phone number //@country Information about the country to which the phone number belongs; may be null //@country_calling_code The part of the phone number denoting country calling code or its part -//@formatted_phone_number The phone number without country calling code formatted accordingly to local rules +//@formatted_phone_number The phone number without country calling code formatted accordingly to local rules. Expected digits are returned as '-', but even more digits might be entered by the user phoneNumberInfo country:countryInfo country_calling_code:string formatted_phone_number:string = PhoneNumberInfo; diff --git a/td/telegram/CountryInfoManager.cpp b/td/telegram/CountryInfoManager.cpp index ca3c0779c..61c27d3af 100644 --- a/td/telegram/CountryInfoManager.cpp +++ b/td/telegram/CountryInfoManager.cpp @@ -199,7 +199,7 @@ void CountryInfoManager::do_get_phone_number_info(string phone_number_prefix, st const CountryInfo *best_country = nullptr; const CallingCodeInfo *best_calling_code = nullptr; size_t best_length = 0; - bool is_prefix = false; + bool is_prefix = false; // is phone number a prefix of a valid country_code + prefix for (auto &country : list->countries_) { for (auto &calling_code : country.calling_codes) { if (begins_with(phone_number, calling_code.calling_code)) { @@ -240,7 +240,7 @@ void CountryInfoManager::do_get_phone_number_info(string phone_number_prefix, st result += pattern[current_pattern_pos++]; } if (current_pattern_pos == pattern.size()) { - result += ' '; + // result += ' '; } if (current_pattern_pos >= pattern.size() || pattern[current_pattern_pos] == 'X') { result += c; @@ -257,8 +257,22 @@ void CountryInfoManager::do_get_phone_number_info(string phone_number_prefix, st } } } + for (size_t i = current_pattern_pos; i < pattern.size(); i++) { + if (is_digit(pattern[i])) { + is_failed_match = true; + } + } if (!is_failed_match && matched_digits >= max_matched_digits) { max_matched_digits = matched_digits; + while (current_pattern_pos < pattern.size()) { + if (pattern[current_pattern_pos] == 'X') { + result.push_back('-'); + } else { + CHECK(!is_digit(pattern[current_pattern_pos])); + result.push_back(' '); + } + current_pattern_pos++; + } formatted_phone_number = std::move(result); } }