1 /** 2 * Copyright (c) 2024 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 "order_name_generator.h" 17 18 namespace { 19 constexpr uint32_t CHAR_COUNT = 26; 20 constexpr uint32_t CHAR_CODE_A = 97; 21 } // namespace 22 OrderNameGenerator(std::set<std::string> reservedNames)23panda::guard::OrderNameGenerator::OrderNameGenerator(std::set<std::string> reservedNames) 24 : NameGenerator(std::move(reservedNames)) 25 { 26 /* The generation of sequential names is based on the order of a, b, c..., The initial size of reserveNames_ is the 27 * number of names already used in the name cache. The value of loopNumber_ is assigned here to reduce the number 28 * of recursion times of GetName */ 29 loopNumber_ = reservedNames_.size() / CHAR_COUNT; 30 } 31 GetName()32std::string panda::guard::OrderNameGenerator::GetName() 33 { 34 std::string name = FromCharCode(CHAR_CODE_A + charIndex_); 35 if (loopNumber_ > 0) { 36 name += std::to_string(loopNumber_); 37 } 38 charIndex_ = (charIndex_ + 1) % CHAR_COUNT; 39 if (charIndex_ == 0) { 40 loopNumber_ += 1; 41 } 42 if (reservedNames_.find(name) != reservedNames_.end()) { 43 return GetName(); 44 } 45 return name; 46 } 47 FromCharCode(uint32_t code)48std::string panda::guard::OrderNameGenerator::FromCharCode(uint32_t code) 49 { 50 return {static_cast<char>(code)}; 51 } 52