1d95e75fdSopenharmony_ci/* 2d95e75fdSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 3d95e75fdSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d95e75fdSopenharmony_ci * you may not use this file except in compliance with the License. 5d95e75fdSopenharmony_ci * You may obtain a copy of the License at 6d95e75fdSopenharmony_ci * 7d95e75fdSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d95e75fdSopenharmony_ci * 9d95e75fdSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d95e75fdSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d95e75fdSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d95e75fdSopenharmony_ci * See the License for the specific language governing permissions and 13d95e75fdSopenharmony_ci * limitations under the License. 14d95e75fdSopenharmony_ci */ 15d95e75fdSopenharmony_ci 16d95e75fdSopenharmony_ci#include "standardize_utils.h" 17d95e75fdSopenharmony_ci#include "telephony_log_wrapper.h" 18d95e75fdSopenharmony_ci 19d95e75fdSopenharmony_cinamespace OHOS { 20d95e75fdSopenharmony_cinamespace Telephony { 21d95e75fdSopenharmony_cistd::string StandardizeUtils::RemoveSeparatorsPhoneNumber(const std::string &phoneString) 22d95e75fdSopenharmony_ci{ 23d95e75fdSopenharmony_ci std::string newString; 24d95e75fdSopenharmony_ci 25d95e75fdSopenharmony_ci if (phoneString.empty()) { 26d95e75fdSopenharmony_ci TELEPHONY_LOGE("RemoveSeparatorsPhoneNumber return, phoneStr is empty."); 27d95e75fdSopenharmony_ci return newString; 28d95e75fdSopenharmony_ci } 29d95e75fdSopenharmony_ci 30d95e75fdSopenharmony_ci for (char c : phoneString) { 31d95e75fdSopenharmony_ci if ((c >= '0' && c <= '9') || c == '*' || c == '#' || c == '+' || c == 'N' || c == ',' || c == ';') { 32d95e75fdSopenharmony_ci newString += c; 33d95e75fdSopenharmony_ci } 34d95e75fdSopenharmony_ci } 35d95e75fdSopenharmony_ci 36d95e75fdSopenharmony_ci return newString; 37d95e75fdSopenharmony_ci} 38d95e75fdSopenharmony_ci 39d95e75fdSopenharmony_cistd::string StandardizeUtils::FormatNumberAndToa(const std::string &phoneNumber, const int32_t callToa) 40d95e75fdSopenharmony_ci{ 41d95e75fdSopenharmony_ci if (phoneNumber.empty()) { 42d95e75fdSopenharmony_ci TELEPHONY_LOGE("FormatNumberAndToa return, phoneNumber is empty."); 43d95e75fdSopenharmony_ci return phoneNumber; 44d95e75fdSopenharmony_ci } 45d95e75fdSopenharmony_ci std::string newString; 46d95e75fdSopenharmony_ci const int32_t TOA_INTER = 145; 47d95e75fdSopenharmony_ci if (callToa == TOA_INTER && !phoneNumber.empty() && phoneNumber.front() != '+') { 48d95e75fdSopenharmony_ci newString += '+'; 49d95e75fdSopenharmony_ci for (char c : phoneNumber) { 50d95e75fdSopenharmony_ci newString += c; 51d95e75fdSopenharmony_ci } 52d95e75fdSopenharmony_ci } else { 53d95e75fdSopenharmony_ci newString = phoneNumber; 54d95e75fdSopenharmony_ci } 55d95e75fdSopenharmony_ci return newString; 56d95e75fdSopenharmony_ci} 57d95e75fdSopenharmony_ci 58d95e75fdSopenharmony_civoid StandardizeUtils::ExtractAddressAndPostDial(const std::string &phoneString, std::string &networkAddress, 59d95e75fdSopenharmony_ci std::string &postDial) 60d95e75fdSopenharmony_ci{ 61d95e75fdSopenharmony_ci if (phoneString.empty()) { 62d95e75fdSopenharmony_ci TELEPHONY_LOGE("ExtractAddressAndPostDial return, phoneStr is empty."); 63d95e75fdSopenharmony_ci return; 64d95e75fdSopenharmony_ci } 65d95e75fdSopenharmony_ci 66d95e75fdSopenharmony_ci size_t postDialIndex = phoneString.length(); 67d95e75fdSopenharmony_ci for (size_t i = 0; i < phoneString.length(); i++) { 68d95e75fdSopenharmony_ci char c = phoneString.at(i); 69d95e75fdSopenharmony_ci if ((c >= '0' && c <= '9') || c == '*' || c == '#' || c == '+' || c == 'N') { 70d95e75fdSopenharmony_ci networkAddress += c; 71d95e75fdSopenharmony_ci } else if (c == ',' || c == ';') { 72d95e75fdSopenharmony_ci postDialIndex = i; 73d95e75fdSopenharmony_ci break; 74d95e75fdSopenharmony_ci } 75d95e75fdSopenharmony_ci } 76d95e75fdSopenharmony_ci 77d95e75fdSopenharmony_ci for (size_t i = postDialIndex; i < phoneString.length(); i++) { 78d95e75fdSopenharmony_ci char c = phoneString.at(i); 79d95e75fdSopenharmony_ci postDial += c; 80d95e75fdSopenharmony_ci } 81d95e75fdSopenharmony_ci} 82d95e75fdSopenharmony_ci 83d95e75fdSopenharmony_cistd::vector<std::string> StandardizeUtils::Split(const std::string &str, const std::string &flag) 84d95e75fdSopenharmony_ci{ 85d95e75fdSopenharmony_ci std::vector<std::string> vec; 86d95e75fdSopenharmony_ci std::string::size_type start = 0; 87d95e75fdSopenharmony_ci std::string::size_type pos = 0; 88d95e75fdSopenharmony_ci while ((pos = str.find(flag, start)) != str.npos) { 89d95e75fdSopenharmony_ci vec.push_back(str.substr(start, pos - start)); 90d95e75fdSopenharmony_ci start = pos + flag.size(); 91d95e75fdSopenharmony_ci } 92d95e75fdSopenharmony_ci if (start != str.size()) { 93d95e75fdSopenharmony_ci vec.push_back(str.substr(start, str.size() - start)); 94d95e75fdSopenharmony_ci } 95d95e75fdSopenharmony_ci return vec; 96d95e75fdSopenharmony_ci} 97d95e75fdSopenharmony_ci} // namespace Telephony 98d95e75fdSopenharmony_ci} // namespace OHOS 99