1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "utils/anonymous.h" 17 namespace OHOS { 18 namespace DistributedData { 19 20 constexpr int32_t CONTINUOUS_DIGITS_MINI_SIZE = 6; 21 AnonyDigits(const std::string & fileName)22std::string AnonyDigits(const std::string &fileName) 23 { 24 std::string::size_type digitsNum = fileName.size(); 25 if (digitsNum < CONTINUOUS_DIGITS_MINI_SIZE) { 26 return fileName; 27 } 28 std::string::size_type endDigitsNum = 4; 29 std::string::size_type shortEndDigitsNum = 3; 30 std::string name = fileName; 31 std::string last = ""; 32 if (digitsNum == CONTINUOUS_DIGITS_MINI_SIZE) { 33 last = name.substr(name.size() - shortEndDigitsNum); 34 } else { 35 last = name.substr(name.size() - endDigitsNum); 36 } 37 38 return "***" + last; 39 } 40 Change(const std::string & name)41std::string Anonymous::Change(const std::string &name) 42 { 43 std::vector<std::string> alnum; 44 std::vector<std::string> noAlnum; 45 std::string alnumStr; 46 std::string noAlnumStr; 47 for (const auto &letter : name) { 48 if (isxdigit(letter)) { 49 if (!noAlnumStr.empty()) { 50 noAlnum.push_back(noAlnumStr); 51 noAlnumStr.clear(); 52 alnum.push_back(""); 53 } 54 alnumStr += letter; 55 } else { 56 if (!alnumStr.empty()) { 57 alnum.push_back(alnumStr); 58 alnumStr.clear(); 59 noAlnum.push_back(""); 60 } 61 noAlnumStr += letter; 62 } 63 } 64 if (!alnumStr.empty()) { 65 alnum.push_back(alnumStr); 66 noAlnum.push_back(""); 67 } 68 if (!noAlnumStr.empty()) { 69 noAlnum.push_back(alnumStr); 70 alnum.push_back(""); 71 } 72 std::string res = ""; 73 for (size_t i = 0; i < alnum.size(); ++i) { 74 res += (AnonyDigits(alnum[i]) + noAlnum[i]); 75 } 76 return res; 77 } 78 } // namespace DistributedData 79 } // namespace OHOS 80